[LCP]sizeof question.

Greg Black gjb at gbch.net
Fri Sep 20 16:08:01 UTC 2002


David wrote:

| 	struct _Options *ptr;
| 
| 	ptr = (struct _Options *) calloc (1, sizeof *ptr);

Lose the cast ^^^^^^^^^^^^^^^^^^^ -- it's wrong.

| Question:
|   Am I using the sizeof correctly in the calloc call?  Sould I be 
| using ptr instead of *ptr?

You need to ask yourself what it is that you're allocating (as
an English statement).  Then say out loud what it is you've
asked for.  If they match, you've got it right.  If not, not.

Put more obviously, if you want to allocate a pointer, then you
need something the size of a pointer; if you want to allocate
space for a struct, you need the size of the struct (which can
be expressed as the size of something pointed to by a suitable
pointer).

Greg



More information about the linuxCprogramming mailing list