1

I am using C++ on Mac OSX Lion and I have created the following code:

float* floatArray = new float[10];

for(int i = 0; i < 10; i++)
{
    floatArray[i] = 0.0 ;
}

std::cout<< "Test size of a float " << sizeof(floatArray[0]) << std::endl;
// The value is 4 byte which is what I would expect.

std::cout<< "Test the size of the whole array" << sizeof(floatArray) <<std::endl;
// the value is 8. I would have expected the value to be 40 bytes.

What am I not understanding?

Thanks in Advance

Miek
  • 1,127
  • 4
  • 20
  • 35
  • 4
    You are using c++. Why not use `std::vector` if you need a dynamically allocated array? – Grizzly Aug 30 '12 at 16:37
  • Opps that was a typo. I just threw that together as an example. should have been i like the rest of them. – Miek Aug 31 '12 at 17:22
  • @Grizzly I am processing numerical float data(sometimes by the gigs). #1 I'm not convinced a vector is as fast as a float array. #2 I need to index the array(not iterate), #3 I'm using C File to write binary files. C File much faster than iostream and C File allows passing a float array right into the stream operator. #4 I've been told before that even though you can access a vector index using .at() ,it's not proper. – Miek Aug 31 '12 at 17:32
  • 1
    `vector` is _sometimes_ (very rarely) measurably slower, but `vector` prevents a lot of mistakes, such as the one in your question. `vector` can index with `[]` just like array, You can send the data to the stream with `&myvector[0]`. So there goes all of your counterarguments. – Mooing Duck Aug 31 '12 at 18:06
  • #1 it shouldn't be slower (if used correctly). Always remember the first rule of performance optimizations: Always profile, never assume #2 so? #3 Doesn't matter for using `vector` or `float*` #4 You can index either using `.at()` or `[]` and there is nothing improper about it. Using `[]` on a `vector` shouldn't be any slower then using it on a pointer (at least in release mode, otherwise there might be bounds checking, but who cares about performance in debug mode). Most importantly `vector` is continous, so you can get a `float*`which can be used like your floatArray using `&vec[0]`. – Grizzly Aug 31 '12 at 19:49
  • Thanks for the added info. I'll try those vector techniques in the future. I'm still not clear how I can pass a vector to a C File fwrite() function though. – Miek Sep 05 '12 at 16:35

5 Answers5

7

The expression sizeof(floatArray) returns the size of the pointer, not what it points to.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

In your system, size of the pointer in memory is 8 bytes. Sizeof() operator just looks at the size of that variable in memory. So it prints the size of the float pointer.

You can find more detail here. How to find the 'sizeof' (a pointer pointing to an array)?

Community
  • 1
  • 1
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
2

Compare it with this (which actually is an array):

float floatArray[10] = {0.0};
std::cout<< "sizeof a float " << sizeof(floatArray[0]) << std::endl;
std::cout<< "sizeof the whole array " << sizeof(floatArray) << std::endl;

and then, if you need to use dynamically-sized or allocated arrays, consider using std::vector instead.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Ahh! Thats the difference. Thats a static array. I believe it allocates it's memory from the stack. Its not the same thing, but you just pointed out the answer. sizeof(static array) will return 40 bytes. The data I deal with is so large it would cause a stack overflow. I need my arrays to come from the heap. – Miek Aug 30 '12 at 16:58
  • 3
    @Miek: Term-wise, that's just an "array". It is not static (it is statically-*sized*); this is a static array: `static int arr[5];` in the same way this is a static float: `static float f;`. Yes, the memory will be allocated on the stack, this is called *automatic storage duration*. So `sizeof(anyarray)` is the size of that array, static or not. What your question has is a *dynamically-allocated array*. It's still an array, but the type you use to access it is just a pointer to the first element. `sizeof` operates on types, so the size information is lost. You should use `std::vector`. – GManNickG Aug 30 '12 at 17:10
1

Your second sizeof(floatArray) is actually returning the size of the pointer, not the size of the array. See here

Soturi
  • 1,486
  • 1
  • 14
  • 30
  • Yeah. I dont need the size(length) of my array, I need to know the total bytes for file writing. There are other ways to get the same info, but I though I could use sizeof to get it. – Miek Aug 30 '12 at 16:35
  • Understood. But sizeof(floatArray) is returning the size of the pointer (memory address). You could use: int length = 10; int totalMemory = length * sizeof(float); – Soturi Aug 30 '12 at 16:39
  • @Miek: Or you could use a std::vector. – Martin York Aug 30 '12 at 18:27
0

The sizeof operator has no knowledge what and how many elements a pointer points to (except when you feed it an array, but that's not a pointer, again). So it returns sizeof (float *) - and as you're probably on 64-bit, the size of a pointer is 8 bytes long.