Partially filled array memory question

My understanding is when I declare an array of size "5" then the computer locates a consecutive group of 5 memory blocks to hold the elements of the array but only records the first memory location as all elements can be found based on the first address since the elements are stored in consecutive blocks.

My question is, how is this handled if I dont declare a size for my array? If I create an array using "int temp[]" what happens in memory?
Don't be afraid to try it yourself :)

Thankfully, if you declare an array without a size, it gives you a compile-time error, error: storage size of 'arr' isn't known .
As Ganado has said, the compiler has to know the array size up front (when defining an array.)

But you can leave the value out if there's an initializer list following the declaration definition, e.g.

int temp[] = {1, 2, 3};

As you have not said otherwise, the compiler will take you mean

int temp[3] = {1, 2, 3};

i.e. provide just enough storage for the values you've provided, whereas if you'd written

int temp[8] = {1, 2, 3};

it would create an array of 8 elements with the first three elements set to 1, 2, and 3 with the rest set to zero.

The temp[] form is quite useful if you're too "lazy" to count the elements yourself as you can get the compiler to work this out for you, e.g.

1
2
const int pi_digits[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
const int pi_digit_count = sizeof(pi_digits)/sizeof(pi_digits[0]);


If you decide to add more digits to Pi you shouldn't have to do more to your code than add the digits to the initializer list.

Andy

Edit: attempted to fix confusion between declaration and definition...
Last edited on
I guess I was thinking that partially filled arrays dont have a size but they actually do get a size somewhere else in the code so they act like other arrays as far as memory goes.

Thanks!
Sorry, you're kind of right.

You can declare an array without a size.

But you can't define it without a size; I was talking about defining an array.

But an array always has a size, irrespective of how may elements have been set explicitly.

And they're always filled with something, even if it's just junk!

(Unless you're talking about the zero-sized arrays that are allowed in C as the last member of a struct? These aren't part of C++ but both MSVC and GCC allow their use when compiling C++ code as part of their respective extensions. No storage is set aside from them in the struct; it's assued the struct is going to be used as a header for a bigger block of data.)

Andy

1
2
3
4
5
6
7
8
9
10
11
12
// main.cpp

#include <iostream>
using namespace std;

#include "test.h"
#include "data.h"

int main() {
    test(data, data_size);
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
// data.h

#ifndef Included_Data_H
#define Included_Data_H

int data[]; // declaration
// you could use extern a[]; but the compiler assumes
// this as it can only be a definition if the size is given

extern int data_size;
// extern is needed here

#endif // Included_Data_H 


1
2
3
4
5
6
7
// example.cpp

#include "data.h"

int data[] = {1, 2, 3}; // definition

int data_size = sizeof(data)/sizeof(data[0]); // definition 


1
2
3
4
5
6
7
8
// test.h

#ifndef Included_Test_H
#define Included_Test_H

void test(int a[], int a_size);

#endif // Included_Test_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
// test.cpp

#include <iostream>
using namespace std;

#include "test.h"

// here int data[] means a pointer (int*)
void test(int a[], int a_size) {
    for(int i = 0; a_size > i; ++i) {
        cout << "a[" << i << "] = " << a[i] << "\n";
    }
}


Last edited on
Topic archived. No new replies allowed.