Re: Array and Pointer Tutorial
- From: "Rod Pemberton" <do_not_have@xxxxxxxxxxx>
- Date: Fri, 12 May 2006 17:54:50 -0400
"Richard Heathfield" <invalid@xxxxxxxxxxxxxxx> wrote in message
news:S_2dnfO7YaslYP7ZRVny0A@xxxxxxxxx
Chad said:of
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
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
.
- Follow-Ups:
- Re: Array and Pointer Tutorial
- From: Richard Bos
- Re: Array and Pointer Tutorial
- From: Richard Heathfield
- Re: Array and Pointer Tutorial
- References:
- Re: Array and Pointer Tutorial
- From: Richard Bos
- Re: Array and Pointer Tutorial
- From: Chad
- Re: Array and Pointer Tutorial
- From: Flash Gordon
- Re: Array and Pointer Tutorial
- From: Chad
- Re: Array and Pointer Tutorial
- From: Richard Heathfield
- Re: Array and Pointer Tutorial
- Prev by Date: Re: PLEASE HELP - How do I include OpenSSL in my code?
- Next by Date: Re: Compiler error for "wrong" sized type
- Previous by thread: Re: Array and Pointer Tutorial
- Next by thread: Re: Array and Pointer Tutorial
- Index(es):
Relevant Pages
|