simple malloc question

R

relient

Hi, I have a question about malloc.

When I do this:

char **p = malloc(12);

does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
explicitly do it?

If it does not implicitly do it, then what is returned?

- is it 3 pointers to work with since pointers take up 4 bytes each on
32-bit systems?

- or is it twelve pointers, which would mean malloc implicitly did 12 *
sizeof(char*), which equals: 48 bytes..?

Thanks in advanced,
relient.
 
N

Nelu

Hi, I have a question about malloc.

When I do this:

char **p = malloc(12);

does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
explicitly do it?
No. malloc takes one argument that is a size_t
that represents the number of bytes to allocate.
It can't automatically tell what to allocate based on the
type of the variable on the left side of =.
If it does not implicitly do it, then what is returned?
It returns the same thing whether it does that or not:
the address of the allocated object if the allocation was succesful
and a null pointer otherwise.
- is it 3 pointers to work with since pointers take up 4 bytes each on
32-bit systems?

- or is it twelve pointers, which would mean malloc implicitly did 12 *
sizeof(char*), which equals: 48 bytes..?
12 bytes. Use sizeof(type *), forget about the 4 bytes/pointer if
you want to have portable code.
sizeof(char) is always 1.
 
I

Ico

relient said:
When I do this:

char **p = malloc(12);

does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
explicitly do it?

If it does not implicitly do it, then what is returned?

No, it allocates exactly 12 bytes, and returns a pointer to this freshly
allocated memory.
- is it 3 pointers to work with since pointers take up 4 bytes each on
32-bit systems?

It might, but don't count on that.
- or is it twelve pointers, which would mean malloc implicitly did 12 *
sizeof(char*), which equals: 48 bytes..?

No. As you can probably find in your compiler's documentation,
malloc(size) allocates size *bytes* and returns a pointer to the
allocated memory.

To be safe, use something like

size_t count = 12;
char **p = malloc(sizeof(char *) * count);

or

size_t count = 12;
char **p = calloc(sizeof(char *), count);
 
R

relient

So my max index range with my example above would be 2, correct?:

p[2] = "strin literal";

Thanks,
relient.
 
R

relient

So my max index range with my example above would be 2, correct?:

p[2] = "string literal";

Thanks,
relient.
 
K

Keith Thompson

Ico said:
No, it allocates exactly 12 bytes, and returns a pointer to this freshly
allocated memory.

Or it returns a null pointer value if the allocation failed (you
should always check for this).

[...]
No. As you can probably find in your compiler's documentation,
malloc(size) allocates size *bytes* and returns a pointer to the
allocated memory.

To be safe, use something like

size_t count = 12;
char **p = malloc(sizeof(char *) * count);

Better:

char **p = malloc(count * sizeof *p);

so you don't have to change the call of the type of p changes. (It's
not a big deal in this case, since the initialization is on the same
line as the declaration, but it can matter if the assignment is
separate from the declaration.)
or

size_t count = 12;
char **p = calloc(sizeof(char *), count);

calloc() has the overhead of setting the allocated memory to
all-bits-zero, which isn't particularly useful in this case (a null
pointer isn't necessarily represented as all-bits-zero).
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

relient said:
Hi, I have a question about malloc.

When I do this:

char **p = malloc(12);

does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
explicitly do it?
The idiom is
T *var = malloc(count * sizeof *var); /* and whenever count is 1 or
*var is of type (unsigned) char, you normally omit them*/

You probably want char **p = malloc(12 * sizeof *p);
which makes room (unless it fails) for 12 char pointers.
 
C

Christopher Benson-Manica

(WRT calling malloc() with an argument of 12:)
12 bytes.

At least 12 bytes, possibly more, although there's no portable way to
determine how many bytes were actually allocated.
 
E

Eric Sosman

Christopher said:
(WRT calling malloc() with an argument of 12:)




At least 12 bytes, possibly more, although there's no portable way to
determine how many bytes were actually allocated.

In other words, only 12 bytes are allocated, where
"allocated" means "made available for use." No bytes
beyond 12 can be considered "allocated" from the point
of view of the caller of malloc().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top