-7

Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)

I am creating an array by using following code

float *A;
A = (float *) malloc(100*sizeof(float));
float *B;
B = (float *) malloc(100*sizeof(float));

but after these when I type an print the size of the A and B by the following, I get 2 as a result as I expect to see 100.

sizeof(A)/sizeof(float)
Community
  • 1
  • 1
erogol
  • 13,156
  • 33
  • 101
  • 155

2 Answers2

11

Your expectation is wrong. A is a float*, so its size will be sizeof(float*), regardless of how you actually allocate it.

If you had a static array - i.e. float A[100], then this would work.

Since this is C++, use std::array or std::vector.

Worst case, use new[]. Definitely don't use malloc.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4

This works only for static arrays, defined in the current scope.

All you get in your example is the size of a pointer to float divided by the size of float.

nameless
  • 1,969
  • 11
  • 34