Handmade Hero » Forums » Code » Question about memory allocation in C
C_Worm
27 posts
#21119 Question about memory allocation in C
3 months, 2 weeks ago Edited by C_Worm on May 30, 2019, 9:39 p.m. Reason: Initial post

Hello i was wondering if it is possible to get the size of some dynamicaly allocated memory like Bmem here.


1
2
3
4
5
void *Bmem = malloc(50000);

char *Pixel = (char*)Bmem;

int BmemSize = sizeof((char*)Bmem) * 50000; <--- (=400000)


And is it dangerous to iterate over a larger size than given back by malloc like this?

like writing to 1 beyond the memory allocated.

1
2
3
4
5
6
	for(int i  = 0;
		i < 400001;
		i++)
	{
		printf("%c", *Pixel++);
	}


i dont seem to get any errors and it does work
marcc
Marc Costa
43 posts
#21120 Question about memory allocation in C
3 months, 2 weeks ago

You ask malloc for an amount of memory, so you already know the size.

Having said that, malloc will have internal housekeeping to track memory blocks and sizes, but there's no way to access that housekeeping data.

If you want a way to keep the allocated amount next to the pointer, take a look at Sean Barrett's stretchy buffer implementation, in which the capacity and element count of the array are stored right before the array.

When doing this: int BmemSize = sizeof((char*)Bmem) * 50000; <--- (=400000), what you're computing is [m]sizeof(char*) * 50000[\m], which in a 64bit architecture will result in 8 * 50000. This [m]sizeof[\m] won't return the size of the allocation.

You can write past the end of the array because at the OS level, memory allocation granularity is at the page size (e.g. 4KB, 2MB, 1GB), so even if malloc gives you a 2KB block of memory, you can write past the end of the array without a memory protection fault.

Casey does a more detailed explanation in Handmade Hero while implementing the memory arena.
mmozeiko
Mārtiņš Možeiko
1971 posts / 1 project
#21121 Question about memory allocation in C
3 months, 2 weeks ago Edited by Mārtiņš Možeiko on May 31, 2019, 12:47 a.m.

If you are OK using OS specific functions, then you can use _msize (MSVC), malloc_usable_size (Linux), or malloc_size (macOS).
Although, as already marcc mentions - if you are calling malloc, then you already know the size. Use the same value as you passed to malloc. Either as global constant, or store it somewhere and pass to code that needs it.
marcc
Marc Costa
43 posts
#21124 Question about memory allocation in C
3 months, 2 weeks ago

If you are OK using OS specific functions, then you can use _msize (MSVC), malloc_usable_size (Linux), or malloc_size (macOS).


Thanks, mmozeiko! TIL :)
ClearZ
John Cleary
3 posts
#21242 Question about memory allocation in C
2 months, 1 week ago

I think you have got everything backwards

Malloc takes the number of bytes you need allocated so bMem contains 50,000 bytes

You then cast this to a char* which is 8 bytes on 64bit. This means the number of *pixels that can be stored is (50000 / 8) which is 6250 not the 400000 you get when you multiply instead

Inside your loop pixel++ will increment by 8 bytes so you will get a buffer overflow once i gets to 6251

If you where after 50000 pointers then you should use malloc(50000 * sizeof(char*))

Regards,
John.