How to find the size of an array defined by a pointer?

float *m;
m=new float[4];

is there any way that i get the size of memory block allocated to pointer m so that i can find out the size of the array m?

Comments

  • : float *m;
    : m=new float[4];
    :
    : is there any way that i get the size of memory block allocated to pointer m so that i can find out the size of the array m?
    :
    The sizeof( ) operator will return the size in bytes of the array pointed to by m.

    size = sizeof(*m);
  • : : float *m;
    : : m=new float[4];
    : :
    : : is there any way that i get the size of memory block allocated to pointer m so that i can find out the size of the array m?
    : :
    : The sizeof( ) operator will return the size in bytes of the array pointed to by m.
    :
    : size = sizeof(*m);

    No, that will tell you the size of the 'float*'. Remember, that 'sizeof' is a compile-time operator - it tells you the size of a type - it has no knowlege of 'new' or 'malloc' or any of that.

    Cheers,
    Eric
  • : : : float *m;
    : : : m=new float[4];
    : : :
    : : : is there any way that i get the size of memory block allocated to pointer m so that i can find out the size of the array m?
    : : :
    : : The sizeof( ) operator will return the size in bytes of the array pointed to by m.
    : :
    : : size = sizeof(*m);
    :
    : No, that will tell you the size of the 'float*'. Remember, that 'sizeof' is a compile-time operator - it tells you the size of a type - it has no knowlege of 'new' or 'malloc' or any of that.
    :
    : Cheers,
    : Eric
    :

    I know the sizeof() is not the answer to my problem...
    So how can i get the size of memory that is allocated to m?
  • : I know the sizeof() is not the answer to my problem...
    : So how can i get the size of memory that is allocated to m?

    You 'remember' it. Your program obviously knows how much memory it allocated, just keep that number handy. If you're looking for standard function that will tell you how much space is allocated given a particular pointer, there is none.

    Cheers,
    Eric

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories