-1

I have dynamically allocated 2D array. Here is the code

int **arrofptr ;
arrofptr = (int **)malloc(sizeof(int *) * 2);
arrofptr[0] = (int *)malloc(sizeof(int)*6144);
arrofptr[1] = (int *)malloc(sizeof(int)*4800);

Now i have to know that how many bytes are allocated in arrofptr,arrofptr[0],arrofptr[1]? is there any way to know the size?

if we will print

sizeof(arrofptr);
sizeof(arrofptr[0]);
sizeof(arrofptr[1]);

then it will print 4.

P.P
  • 117,907
  • 20
  • 175
  • 238
Dixit Singla
  • 293
  • 2
  • 6
  • 16
  • 1
    No - you just have to keep track of this yourself. – Paul R Sep 28 '12 at 06:34
  • the size of the pointers is usually `4`, this is why you got `4`. the bytes which allocated are in the parameter of the `malloc(...)`, in your case these are `sizeof(int) * 6144` and `sizeof(int) * 4800`... ... ... – holex Sep 28 '12 at 07:15
  • 1
    This is not a 2d array, it is a bunch of segmented arrays with an array of pointers to keep track of them. For info of how to properly allocate 2d arrays dynamically, [read this](http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c). Also, you should [never typecast the result of malloc](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc). – Lundin Sep 28 '12 at 07:25

4 Answers4

2

You can't find size of arrofptr, because it is only a pointer to pointer. You are defining an array of arrays using that. There's no way to tell the size information with only a pointer, you need to maintain the size information yourself.

P.P
  • 117,907
  • 20
  • 175
  • 238
1

The only return value you get from malloc() is a pointer to the first byte of the allocated region (or NULL on failure). There is no portable, standard, way of getting the associated allocation size from such a pointer, so in general the answer is no.

The C way is to represent arrays and buffers in general with a pair of values: a base address and a size. The latter is typically of the type size_t, the same as the argument to malloc(), by the way.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

if you want to keep track of the size of an allocated block of code you would need to store that information in the memory block that you allocate e.g.

// allocate 1000 ints plus one int to store size

int* p = malloc(1000*sizeof(int) + sizeof(int)); 
*p = (int)(1000*sizeof(int));
p += sizeof(int);

...

void foo(int *p)
{
  if (p)
  {
    --p;
    printf( "p size is %d bytes", *p );
  }
}

alt. put in a struct

struct
{
  int size;
  int *array;
} s;
AndersK
  • 35,813
  • 6
  • 60
  • 86
0

You can't get the length of dynamically allocated arrays in C (2D or otherwise). If you need that information save it to a variable (or at least a way to calculate it) when the memory is initially allocated and pass the pointer to the memory and the size of the memory around together.

In your test case above sizeof is returning the size of the pointer, and thus your calculation the size of the pointers is usually 4, this is why you got 4 and is likely to have the trivial result of 4, always.

Abhinay Reddy Keesara
  • 9,763
  • 2
  • 18
  • 28