Advertisement

void *malloc(size);

Started by October 11, 2000 11:47 AM
19 comments, last by Muerte 24 years, 3 months ago
I come across a problem when using the "malloc" function to allocate memory for loading a texture image, mainly because ''malloc'' returns no value. I have heard that the problem only occurs in C++. Is there an alternative way to allocate memory for texture images? Thanks, -brian *keep it real* -Brian
there are many ways to allocate memory. however, if you are getting a "NULL" from the "malloc" function then there is probably a reason. try using "new" or "GlobalAlloc".
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Advertisement
hmm, that''s unusual, it should return void* (void pointer). my compiler MSVS6 doesn''t seem to have this problem (even with C++) so i''d assume it some sort of software problem. try getting another copy of stdlib.h it may just be a typo in the declaration.
Brought to you by: [email=ogapo@ithink.net]O.G.A.P.O.[/email] +----------------------------------------+| Surgeon General's Warning - || OGAPO can cause serious mental damage || if taken in large doses. |+----------------------------------------+/* Never underestimate the power of stupid people in large groups */
malloc is used like this
[source]
myTextureStructure* myTexture;

malloc(myTexture, amountOfMemoryYouWant);



"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

nothing? malloc returns a pointer. If it returns nothing, it failed.

myPtr = (CMob*) malloc(sizeof(CMob));
Thanks guys, I''ve solved the problem by using "new" instead of "malloc". Who started the rumor that malloc returned NULL in my case? I said "no value", meaning i had an error from the compiler complaining that it can''t read a value from a "void" or something similar. Normally i use MingW, but i refer to the helpfile of Borland Turbo C++ 2.1 when saying "i''ve heard that it only occurs in C++". =) Thanks again-

*keep it real*
-Brian
Advertisement
most likely the reason you were getting a compile error using malloc is because you probably forgot to type cast the "void *" to whatever pointer type you were using.

int *p = malloc(4);

will cause an error because in cannot resolve the pointer type: you would need to explicitly type cast.

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

will work correctly.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
most likely the reason you were getting a compile error using malloc is because you probably forgot to type cast the "void *" to whatever pointer type you were using.

int *p = malloc(4);

will cause an error because in cannot resolve the pointer type: you would need to explicitly type cast.

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

will work correctly.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Im using MSVS 6, and i can always get by without casting. I always do it like int *p = malloc(40 * sizeof(int)); Remember to have that * sizeof(int) in there, or it will allocate 40 bytes, and not space for 40 ints. Strangely though, with GlobalAlloc() Ive always had to cast the pointer.

~demosh
Some of the posters in this thread need to go to remedial C school!

This topic is closed to new replies.

Advertisement