-3

I am a Java programmer working on my first project in C++ and I am struggling to understand how arrays are handled. I am trying to write a program that is structurally equivalent to the following code. In my actual project however, class A and struct B are declared in a header file. The way I understand this is that the statement b.arr[2] instantiates the array b.arr to be of length 2. However when I run the following code as is, I get the error exited with non-zero status. When I comment out the line that is intended to set b.arr[1] equal to a2, it runs fine. Why is this and how might I do this? Also, is there a way I can redefine the b.arr array to be of a different length? I've looked around but nothing I can find seems to address this specific problem.

class A {};

struct B {   
    A *arr[];
};

int main() {
    B b;
    b.arr[2]; // instantiates array to be of length 2?
    A a1;
    A a2;
    b.arr[0] = &a1;
    b.arr[1] = &a2; // RUNS FINE WITHOUT THIS LINE
}

Thanks!

By the way I am writing this for an arduino library, so I cannot use the std namespace.

drew.neely
  • 126
  • 1
  • 13
  • 3
    Please read a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and dont just try random stuff. There is this thing called undefined behaviour in C++ that might cause a velociraptor jumping through your window if you arent careful – 463035818_is_not_an_ai Jul 21 '16 at 18:52
  • `b.arr[2]` is undefined behaviour. Accesses element 3 of an array that has not been allocated. – Richard Critten Jul 21 '16 at 18:52
  • If you want to create a dynamic array whose size you can set or change at run-time, you should use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). If you want an array with a size fixed a the time of compilation I recommend [`std::array`](http://en.cppreference.com/w/cpp/container/array). – Some programmer dude Jul 21 '16 at 18:52
  • For one thing, you are not working with an array of objects, but `arr` is an array of *pointers* to objects. Besides allocating the array itself, you would have to allocate each object separately. Agree with previous commenter, you should look at `std::vector` and do some basic C++ tutorials to get familiar. – Colin D Bennett Jul 21 '16 at 18:56
  • Arduino does not support the std namespace. – drew.neely Jul 21 '16 at 18:58
  • If you come from java, don't just map 'arrays to arrays', that's like apples to oranges. Try with `std::vector<>` first - or, better yet, learn the very details of C++ arrays. – lorro Jul 21 '16 at 19:01
  • No standard library means you have to be a lot more careful. Recommend doing some reading on the [C programming language](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) because in this case it might be more helpful. You should still have classes and templates, but a lot of C++ thinking is based around using the library. – user4581301 Jul 21 '16 at 19:04

2 Answers2

1
b.arr[2]; // instantiates array to be of length 2?

That is not correct.

It references the third element of the array and does nothing with the value of the element.

The program invokes undefined behavior since you are accessing the elements of b.arr when it is not initialized to point to a valid memory.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0
b.arr[2];

This is already undefined behaviour. Accessing something that has not been allocated yet. This means that all bets are off and you can't rely on any code to behave in any particular way.

If you want to store A pointers try something like a std::vector<A*> or std::array<A*> for static length containers.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • I am writing this for arduino, so I cannot use the std namespace. I forgot to mention this originally but I made an edit. Thanks though! – drew.neely Jul 21 '16 at 18:55