sizeof()
in memory allocation CXX-S1006212 __last_block_freed();
213 __check_defragmentation();
214 __check_fragmentation();
215 __malloc_failed();216 __test_realloc();
217 __realloc_defragmentation();
218 return (0);
180
181 void *ptr1 = malloc(16);
182 void *ptr2 = malloc(128);
183 void *ptr3 = malloc(16);184
185 void *ptr;
186
179 ft_putstr("--------------------------------------\n");
180
181 void *ptr1 = malloc(16);
182 void *ptr2 = malloc(128);183 void *ptr3 = malloc(16);
184
185 void *ptr;
178 ft_putstr("\n\nTEST 9: realloc (de)fragmentation\n");
179 ft_putstr("--------------------------------------\n");
180
181 void *ptr1 = malloc(16);182 void *ptr2 = malloc(128);
183 void *ptr3 = malloc(16);
184
158 ft_putstr("\n\nTEST 8: test basic realloc\n");
159 ft_putstr("--------------------------------------\n");
160
161 void *ptr = malloc(32);162
163 strcpy(ptr, "hello word test toto titi tata");
164 show_alloc_mem_ex();
The malloc
function usually expects a memory size value in units (usually byte
)
when allocating any type. Use sizeof(type) * number_of_values
as the size argument
for malloc
to avoid making mistakes.
It is a good practice to use sizeof(type)
with malloc
and other memory
allocation functions because size (in bytes) varies for the given type,
depending on the platform. Using sizeof
takes care of such dependencies.
For instance, the integer types in C & C++, such as 'int', are platform-dependent.
Allocating incorrect buffer size without considering the size of the type could lead to a buffer overflow.
int* ints = (int*)malloc(64);
int* ints = (int*)malloc(sizeof(int) * 16);