Re: Array and Pointer Tutorial




"Richard Heathfield" <invalid@xxxxxxxxxxxxxxx> wrote in message
news:S_2dnfO7YaslYP7ZRVny0A@xxxxxxxxx
Chad said:

I can't really put me pinpoint the exact part, but I know I'm missing
some kind of underlying concept when I see the construction:

int *q = malloc(sizeof *q);

The canonical way to allocate space for n objects of type T is:

T *p = malloc(n * sizeof *p);

or, if p is already declared, simply this:

p = malloc(n * sizeof *p);

The reason this is the canonical way is that it doesn't rely on the type
of
p, except that it must be an object type, not an incomplete type or
function type. If the type of p changes during maintenance, you don't have
to hunt down this line and hack it about. It will automagically work
correctly with the new type.


You should point out the negatives too. If p is already declared, and their
is no comment telling him what file p is declared in. He'll spend forever
trying to answer this question: "What the HELL is p?"

p = malloc(n * sizeof *p); /* p orignially was of type T and declared in
file somedecl.h */


Rod Pemberton



.



Relevant Pages

  • Re: Array and Pointer Tutorial
    ... I can't really put me pinpoint the exact part, but I know I'm missing ... some kind of underlying concept when I see the construction: ... p, except that it must be an object type, not an incomplete type or ...
    (comp.lang.c)
  • Re: loops and 4:1 baluns
    ... batch to another and the variations that one human will introduce to ... construction versus another human also induce characteristics that do not ... few things are exact. ... the class of BalUn. ...
    (rec.radio.amateur.antenna)
  • Re: Option Strict and late binding problem
    ... It was the 'As ICloneable' that was missing. ... Thanks Samuel, I has been educational to say the least. ... ICloneable.Cloneand Copyare the exact same method. ... This is called explicit interface ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VLOOKUP
    ... You're missing the 4th argument to the function, which directs it to look ... for an *exact* match. ... irrespective of what value I input into K2, I get a result in cell K3 ...
    (microsoft.public.excel.newusers)
  • Re: Two Windows XP Professional Errors
    ... 'Sound like' is not specific enough; exact name of file/process req. ... > First, when I try to install SP2, it gives me the error message after ... I have no idea what I could be missing, ...
    (microsoft.public.windowsxp.help_and_support)