• ### What is your GameDev Story?

Public Group

#### Archived

This topic is now archived and is closed to further replies.

# malloc function explanation

This topic is 5374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, I had a question about what the exact purpose of malloc() does, but I searched the forums and got my answer. But, I still have one thing left that I wanted to get clarified. When you have a pointer, that pointer just points to an address that you specify of being as the same size variable as declared. But, when you use malloc, does that then set aside the space of an int? As in, you don''t have to point it to another variable, because you have the space allocated? Or am I missing something? Second, real quick, why when you use malloc, is it written like this: int *p = (int *)malloc(sizeof(int)); I understand the function arguements, but why do you need the (int*)? Thanks for the help, Glenn Murphy

##### Share on other sites
A pointer always points to an address regardless of whether it was malloc'ed or is a variable. This is a good read.

int *p = (int *)malloc(sizeof(int)); - is a typecast, malloc returns a void* and that piece of code is casting to an int*.

[edited by - tiffany_smith on May 8, 2004 3:44:41 PM]

##### Share on other sites
quote:
Original post by gyrmnix
When you have a pointer, that pointer just points to an address that you specify of being as the same size variable as declared.
Uh, no.

A pointer contains an address. Period. All addresses are the same size. Period. The pointer knows nothing about buffers or their lengths. Period.

quote:
Second, real quick, why when you use malloc, is it written like this:
int *p = (int *)malloc(sizeof(int));
I understand the function arguements, but why do you need the (int*)?
You only need it in C++. C casts automatically, and the benefits of that are debatable (particularly with knuckleheads on #c on irc.freenode.net). C++ strives for typesafety, so pointer types are not automatically converted, with the exception of the special value zero (0). The return type of malloc is void *, so you need to cast to your appropriate type.

It is recommended to use new over malloc in C++, partly because casting is unnecessary (new is type-aware and type-safe), and partly because new calls constructors (and the corresponding delete calls destructors).

##### Share on other sites
quote:
Original post by gyrmnix
Okay, I had a question about what the exact purpose of malloc() does, but I searched the forums and got my answer. But, I still have one thing left that I wanted to get clarified. When you have a pointer, that pointer just points to an address that you specify of being as the same size variable as declared. But, when you use malloc, does that then set aside the space of an int? As in, you don''t have to point it to another variable, because you have the space allocated? Or am I missing something?

Yeah, I think you have the right idea. In C there are two ways to get a pointer to an "int''s worth" of memory:

1) int i; &i;
2) malloc(sizeof(int));

The difference is how long the memory stays allocated. The first stays allocated until you leave the block (i.e. reach the } of its scope). The second stays allocated until you use free() on the pointer.

quote:

Second, real quick, why when you use malloc, is it written like this:

int *p = (int *)malloc(sizeof(int));

I understand the function arguements, but why do you need the (int*)?

In C, pointers can be automatically converted to and from void* without casts. Thus, in C, it only makes your code more fragile to cast malloc. In C++, void* cannot be automatically converted to another pointer type, so you need to use the cast. However, C++ has the "new" operator, which will actually return a pointer of the "correct" type.

By the way, a better way to write your malloc line would be:

int *p = malloc(sizeof *p);

this is much less fragile since you only have to change the type in one place.

##### Share on other sites
Okay, I think I''m getting the point. But, there seems to be some contradictions (or I''m not understanding quite all of it). Say I have the following code:

int i;
int *p = &i;

So, p points to the address of i. So, if you use the malloc function (and I''m guessing new as well? I haven''t gotten to that yet) like this:

int *p = (int *)malloc(sizeof(int));

That will, basically, initialize the pointer but not have it pointing to another variable like in the example above? And will it set aside the size of an integer in memory? I''m sorry if I''m not understanding this, and thanks for being patient with me.

And, Way Walker, the way you have it, it will automatically return a size int? Tiffany_Smith, I will start reading that article right away, thanks.

Glenn Murphy

##### Share on other sites
quote:
int *p = (int *)malloc(sizeof(int));

That will, basically, initialize the pointer but not have it pointing to another variable like in the example above? And will it set aside the size of an integer in memory?

It will point to a freshly allocated, int-sized piece of memory, which does not have its own name. You will only be able to access it through the pointer.

With the ''new'' operator, the equivalent action is spelled:

int *p = new int;

You can also initialize the value of the pointed-to integer:

int *p = new int(42);

This is actually calling a constructor for the integer type (as I understand it), even though it isn''t really a class. (And because there is a constructor call here, without the brackets the default constructor will be called instead; so your allocated memory is zeroed-out by default - not so with malloc(). I think I have that right anyway.) Note that this is worlds different from

int *p = new int[42];

which allocates an array of 42 integers and sets p to point to the beginning. Unfortunately you can''t combine the [] and () (except I thought I heard something about the g++ compiler supporting it as a non-standard extension?).

You can see why people generally prefer to use new/delete in C++.

##### Share on other sites
quote:
Original post by gyrmnix
int i;
int *p = &i;

So, p points to the address of i.
No. p points to i. The value of p is the address of i. The semantics are subtly different, but important.

quote:
int *p = (int *)malloc(sizeof(int));

That will, basically, initialize the pointer but not have it pointing to another variable like in the example above?
It will have it pointing to the first element in a region of memory. Since the region you''ve allocated is a single integer, the effect is nearly the same as int *p = &i;, except for the need to clean up.

quote:
And will it set aside the size of an integer in memory?
Yes.

##### Share on other sites
Okay, I finally get it. Thanks everybody. I really appreciate all the help.

Glenn Murphy

##### Share on other sites
quote:
Original post by gyrmnix
And, Way Walker, the way you have it, it will automatically return a size int?

int *p = malloc(sizeof *p);

thing? Yeah, that''ll allocate an int''s worth of memory.

int *p = malloc(N * sizeof *p);

this will allocate N int''s worth of memory, which can be used as an array of N ints.

int *p = NULL;
p = malloc(N * sizeof *p);

this will do the same as the last example. See the advantage? If the two lines are seperated it''s easy to forget to change the type in both places. Heck, it''s easy to forget when they''re right by each other.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 12
• 15
• 11
• 12
• ### Forum Statistics

• Total Topics
634153
• Total Posts
3015844
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!