1

How can i know the size of the array using a pointer that is allocated using malloc?

#include <stdio.h>

int main(){
    int *ptr = (int *)malloc(sizeof(int * 10));
    printf("Size:%d",sizeof(ptr));
    free(ptr_one);
    return 0;
}

I get only the size of the pointer in this case which is 8.How to modify the code to get the size of array which will be 40.

Praburaj
  • 613
  • 8
  • 21
  • 1
    new is not there in c,use malloc. – Gangadhar Sep 02 '13 at 09:57
  • `printf("10")` Apart from that you're going to have to hit platform-dependent (read: non-standard) memory apis, and even those likely only work on dynamic allocations like you're allocation here. If you want portability, you'll have to manage this yourself. – WhozCraig Sep 02 '13 at 10:00
  • `sizeof' cannot be used here since its computation happens at compile time. it is not aware of run time allocations. – Manoj Awasthi Sep 02 '13 at 10:00
  • 1
    [Don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Sep 02 '13 at 10:21

2 Answers2

7

You cannot.
You will need to do the bookkeeping and keep track of it yourself. With new you allocate dynamic memory and while deallocating the memory you just call delete, which knows how much memory it has deallocate because the language takes care of it internally so that users do not need to bother about the bookkeeping. If you still need it explicitly then you need to track it through separate variable.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • +1 for actually reading the question as well as the code (and obviously for the correct answer). – WhozCraig Sep 02 '13 at 10:03
  • _"You will need to do the bookkeeping and keep track of it yourself."_ I'd rather use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) (or [`std::array`](http://en.cppreference.com/w/cpp/container/array)). _Edit_: oh, the question was retagged from `c++` to `c`... – gx_ Sep 02 '13 at 10:06
  • @gx_: I won't advice that upright without knowing the purpose of the program. Perhaps, the OP just wants to understand learn dynamic memory allocation. – Alok Save Sep 02 '13 at 10:08
0

if your machine is 32 bit you will get pointer size always 4 bytes of any data type

if your machine is 64 bit you will get pointer size always 8 bytes of any data type

if you declare static array you will get size by using sizeof

int a[10];

printf("Size:%lu",sizeof(a));

But you did not get the size of array which is blocked by pointer. where the memory to the block is allocated dynamically using malloc .

see this below code:

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

int main()
{
  int i;
  int *ptr = (int *)malloc(sizeof(int) * 10);
  //  printf("Size:%lu",sizeof(ptr)); 
  // In the above case sizeof operater returns size of pointer only.   
    for(i=1;ptr && i<13 ;i++,ptr++)
       {
    printf("Size:%d  %p\n",((int)sizeof(i))*i,ptr);

       }

    return 0;
}

output:

Size:4  0x8ec010
Size:8  0x8ec014
Size:12  0x8ec018
Size:16  0x8ec01c
Size:20  0x8ec020
Size:24  0x8ec024
Size:28  0x8ec028
Size:32  0x8ec02c
Size:36  0x8ec030
Size:40  0x8ec034  //after this there is no indication that block ends. 
Size:44  0x8ec038
Size:48  0x8ec03c
Gangadhar
  • 10,248
  • 3
  • 31
  • 50