Size of dynamic array in C doesn't change -
i getting realloc(): invalid next size
program. coded understand what's happening.
#include <stdio.h> #include <stdlib.h> int main() { char *inp; printf("%lu ",sizeof(inp)); char *res = (char*)malloc(15*sizeof(char*)); printf("%lu ",sizeof(res)); res = "hello world"; printf("%lu\n",sizeof(res)); return 0; }
and surprisingly outputs 8 8 8
. can explain why that? why 8 default? , how malloc()
effects size of inp
?
you using sizeof
.returns size in bytes of object representation of type. remember sizeof operator.
now sizeof returning here 8 constant type of size_t
why isn't changing? because in tha cases using same type char *
.
here 8 bytes size of character pointer 64 bit.
you can have @ this-
printf("size of array of 10 int: %d\n" sizeof(int[10]));
this give output:40
. 40 bytes.
and malloc
never affect size of char*
. still needs 64 bits store address of dynamically allocated space heap.
is possible determine size of dynamically allocated memory in c?
there no standard way this. have take overhead on own. may find extensions compiler may accomplish this.
Comments
Post a Comment