Handmade Hero » Forums » Code » Question about memory allocation in C
C_Worm
7 posts
#21119 Question about memory allocation in C
3 weeks, 5 days 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
40 posts
#21120 Question about memory allocation in C
3 weeks, 5 days 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
1935 posts / 1 project
#21121 Question about memory allocation in C
3 weeks, 5 days 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
40 posts
#21124 Question about memory allocation in C
3 weeks, 5 days 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 :)