473,382 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

simple malloc question

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.

Jan 9 '06 #1
9 2642
On 2006-01-09, relient <xl***************@gmail.com> wrote:
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.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jan 9 '06 #2
Ico
relient <xl***************@gmail.com> wrote:
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);
--
:wq
^X^Cy^K^X^C^C^C^C
Jan 9 '06 #3
So my max index range with my example above would be 2, correct?:

p[2] = "string literal";

Thanks,
relient.

Jan 9 '06 #4
So my max index range with my example above would be 2, correct?:

p[2] = "strin literal";

Thanks,
relient.

Jan 9 '06 #5
On 2006-01-09, relient <xl***************@gmail.com> wrote:
So my max index range with my example above would be 2, correct?:

p[2] = "strin literal";

If the size of the pointer is 4 bytes then that should
be the case.

Please go to: http://cfaj.freeshell.org/google/

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jan 9 '06 #6
Ico <us****@zevv.nl> writes:
relient <xl***************@gmail.com> wrote:
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.


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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 9 '06 #7
relient wrote:
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.

Jan 9 '06 #8
Nelu <pl****@do.not.spam.me> wrote:

(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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jan 9 '06 #9
Christopher Benson-Manica wrote:
Nelu <pl****@do.not.spam.me> wrote:

(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.


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().

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
12
by: gooch | last post by:
I originally posted the following code in a group discussing threads but after further research I think I have a c question about the code. I know there are a couple of non standard c includes here...
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.