#### Archived

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

# malloc() troubles!!!!!!!!!

## Recommended Posts

Ok, here''s the problem. I am trying to allocate some memory. Here is the code:

int *MAP;
unsigned int mapsize;

mapsize = (width * height * layers) * 4;  // this is ok, i checked...contains 960

MAP = (int *)malloc(mapsize * sizeof(int));


Now, when I display sizeof(MAP) it *always* returns 4 bytes! No matter what, MAP is always 4 bytes... Anoyone know what I am doing wrong? BTW, I am using Borland C++ Builder if that helps. Thanks! -Coleco

~ c o l ec o ~

Rock the cradle of love! You stupid WANKER! S i g n a l D E V .com --HASBRO SUCKS--

##### Share on other sites
MAP is a pointer to an int, which has the constant size of 4 bytes. You aren''t doing anything wrong

~~~~~~~~~~
Martee

##### Share on other sites
Side note: sizeof() is normally used only with the names of data types (e.g. sizeof(int)), not with variables themselves.

~~~~~~~~~~
Martee

##### Share on other sites
try using _msize() (or is it msize()) to get the size of the memory-chunk allocated. This size might be slightly larger than the number of bytes you asked for, depending on alignment issues and whether you''re using the debug heap-alloc functions etc...

##### Share on other sites
For malloc() in bcb, this works when I try it:

#define ALLOCATED_SIZE(x) (*(((unsigned int*)x)-1)-6)

The amount you initially specify in malloc seems to be rounded up to the nearest multiple of 4, and at least 8 bytes.

• ### Forum Statistics

• Total Topics
628370
• Total Posts
2982300

• 10
• 9
• 13
• 24
• 11