-1

I'm trying to learn c and implementing some basic memory allocations.

When I either malloc, calloc or realloc a pointer to int, every time I end up with the same chunk of memory of 8 bytes even when I'm initializing it with malloc with 20 bytes.

How to solve this?

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{    
    int* ptr = (int*) malloc(sizeof(int) * 5);    
    printf("%d\n", sizeof(ptr) / sizeof(ptr[0]));    
    ptr = (int*) realloc(ptr, 10 * sizeof(int));    
    printf("%d\n", sizeof(ptr) / sizeof(ptr[0]));    
    return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Sayed Omar
  • 55
  • 3
  • 4
    You already know the size, because you just specified it in the second argument of `realloc`. If you're asking how you can reap that size from just the pointer, the answer is *you can't*. `sizeof` is an operator that delivers the size of the argument provided. In this case, that's the size of *a pointer*, which on your platform is 8 bytes. That's how `sizeof` works – WhozCraig Oct 09 '20 at 10:01
  • 3
    Does this answer your question? [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – UnholySheep Oct 09 '20 at 10:02
  • Please remove the C++ tag. – JHBonarius Oct 09 '20 at 10:02
  • [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Oct 09 '20 at 10:06
  • You already know the size when you call malloc, no need to calculate anything. – Lundin Oct 09 '20 at 10:08

1 Answers1

2

The variable ptr has the type int *.

So the expression sizeof(ptr) gives the size of an object of the type int * that is in the used by you system it seems is equal to 8.

Thus this expression

sizeof(ptr) / sizeof(ptr[0])

is equivalent to the expression

sizeof( int * ) / sizeof( int )

and does not depend on how many elements were dynamically allocated.

If you are allocating dynamically an array you should store somewhere (in a variable) its size. Otherwise having only a pointer you are unable to determine the size of the allocated memory extent if the extent does not have a sentinel value.

There is one possibility when you will declare a pointer to an array like for example

int ( *ptr )[5] = malloc(sizeof(int) * 5);

In this case dereferencing the pointer you will get the lvalue of the allocated array and can determine its size like

sizeof( *ptr ) / sizeof( **ptr );

Pay attention to that you have to use the conversion specifier zu with expressions of the type size_t. Instead of for example this call

printf("%d\n", sizeof(ptr) / sizeof(ptr[0]));

you have to write

printf("%zu\n", sizeof(ptr) / sizeof(ptr[0]));
        ^^^ 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • thank you for your answer..is there anyway to know the amount of memory does this pointer point to? – Sayed Omar Oct 09 '20 at 10:11
  • 1
    @SayedOmar no, there is no standard way to do so and you don't need it. Some implementations have special functions which allows this, but you should just forget about them. – Jabberwocky Oct 09 '20 at 10:13
  • @SayedOmar In your example the pointer has the type int *. So it points to a scalar object of the type int. – Vlad from Moscow Oct 09 '20 at 10:20