Malloc in C: same size before and after?

I have a problem. I am trying to get the number of elements in an int array, without passing a single explicit parameter, but only a pointer to an int array. Now my curiosity:

int * set;
printf("-- %d --", sizeof(set)); //4
set=(int *) malloc(n*sizeof(int));
printf("-- %d --", sizeof(set)); //4  

Why the values ​​are the same, since before malloc it is not initialized even after it. Thanks
UPDATE:
Is there a way to get the length of an int array?

+3
source share
5 answers

Because it sizeofis evaluated at compile time and gives the type size set, a pointer to int.

+8
source

There is no general way to measure the size of memory pointed to by a pointer to C, except in the special case where lines have zero termination by convention.

sizeof (4 32- , 8 64- ), .

, :

+6

, , set, (int*). - 4 .

sizeof , . "" ( ).

+3
source

A pointer is 32 bits or 64 bits.

The value is 4 or 8 bytes.

0
source
int * set;
printf("-- %d --", sizeof(set)); //4
set=(int *) malloc(n*sizeof(int));
printf("-- %d --", n*sizeof(int)); //n*sizeof(int) is the size of your malloc'd memory

hope this helps. in your last statement, you still set the size of the individual integer pointer, which was 4 bytes on your computer. to get the size of your memory area, you need to use the same expression as inside the malloc function.

and the integer pointer doesn't necessarily have the same size as the int itself, since Mat corrected me to

0
source

All Articles