0

I am trying to use a pointer to an array of custom objects in C++. The following code compiles and runs fine on eclipse which uses a gnu compiler from cygwin. But the code gives compilation errors in Visual Studio.

Error

class 'Levels' has an illegal zero-sized array

on line

Structure *mStructres[];

COMPLETE CODE

/*
 * Levels.h
 */

#include "objects/Structure.h"

#ifndef LEVELS_H_
#define LEVELS_H_

class Levels{

public:

    //other public members

    void reInitialize();
    Levels();
    ~Levels();


private:
    //other private members
    Structure *mStructres[];
};

#endif /* LEVELS_H_ */

/////////////////////////////////////
/*
 * Levels.cpp
 */
#include "Levels.h"

Levels::Levels() {
}

Levels::~Levels() {

}

void Levels::reInitialize() {
    mStructres[size];
                for (int i = 0; i < jStructeresArr.size(); i++) {
                mStructres[i] = new Structure(obj1, obj2,
                            obj3);
                }
}

I tried changing the line to

Structure *mStructres;

but then I got errors on these lines in reinitialize method

mStructres[size];
            for (int i = 0; i < jStructeresArr.size(); i++) {
            mStructres[i] = new Structure(obj1, obj2,
                        obj3);
            }

What am I doing wrong? Is this the correct way to do it for cross-platform developement?

UPDATE I would prefer not using vectors or std templates at this stage.

asloob
  • 1,308
  • 20
  • 34
  • That's an array (with no known size) of pointers. – chris Apr 01 '13 at 05:41
  • Yes, I want a dynamic array. – asloob Apr 01 '13 at 05:43
  • 1
    use a [std::vector](http://en.cppreference.com/w/cpp/container/vector) – jkerian Apr 01 '13 at 05:44
  • `std::vector` or if you want to do C way, `Structure**` – Karthik T Apr 01 '13 at 05:44
  • Looks like it doesn't even have to have pointers in the first place. A vector of objects is ideal, but since you want to make it unnecessarily hard, you have the rule of three/five to worry about as well when dealing with the memory management of that dynamic array, let alone unnecessary pointers inside it. – chris Apr 01 '13 at 05:48
  • @jkerian I would prefer not using vector at this stage. Any other way out? – asloob Apr 01 '13 at 05:48
  • Sure there is... but it'll be dozens of lines of memory-management code, rather than simply using built-in tools. – jkerian Apr 01 '13 at 05:49
  • But can someone tell me why it works in eclipse but not visual studio? – asloob Apr 01 '13 at 05:53
  • 1
    It's not legal C++. It works with GCC because the GCC developers have decided to allow it as a non-standard extension. It doesn't work in Visual Studio because VS does not support that particular non-standard extension. – Benjamin Lindley Apr 01 '13 at 06:00

1 Answers1

0

Structure *mStructres[]; is probably being interpreted as Structure **mStructres by one of the compilers.(EDIT: This is probably incorrect, see correction in comments) It's simply a pointer to a pointer to a structure. Note that there isn't actually any storage assigned for it to point to (aside from the single pointer), so you're just writing off into random memory when you assign anything to it.

mStructres[size];       // This does nothing, it _could_ cause a crash... 
                        //  but is otherwise the same as the next statement
42;
for (int i = 0; i < jStructeresArr.size(); i++)
    mStructres[i] = new Structure(obj1, obj2, obj3);

What I suspect you wanted to do was to just recreate your array of pointers.

void Levels::reInitialize() {
    for (int i=0; i< jStructeresArr.size(); i++) {
        delete mStructres[i];      // Don't leak memory
        mStructres[i] = new Structure(obj1, obj2, obj3);
    }
}

You also need the following line in your constructor. (Inspired from here)

    mStructres = new Structure*[jStructeresArr.size()];

If jStructeresArr.size() ever changes, you have quite a bit of work to do. So if that's a possibility, I'd highly advise you to ditch this and just go with std::vector or std::list.

Community
  • 1
  • 1
jkerian
  • 16,497
  • 3
  • 46
  • 59
  • 3
    *"Structure *mStructres[]; is probably being interpreted as Structure **mStructres"* -- No, I don't believe that is the case. I believe it is a feature from C99 called "flexible arrays" that GCC allows to be used even when compiling as C++. – Benjamin Lindley Apr 01 '13 at 06:18
  • @BenjaminLindley: What nasty hack to specifically allow in the language... had never heard of that nonsense before. – jkerian Apr 01 '13 at 06:29
  • I made the changes you have specified. The compilation errors are gone, but now its throwing linker error at other places "LNK2019 unresolved symbol" in a completely diff location. I think the error is because of the changes I have made. Any ideas? Thanks. – asloob Apr 01 '13 at 07:12
  • @user603125: Given the changes involved here, the linking error is almost certainly unrelated. Many more specific details are needed. (perhaps as a separate question... but linking errors are usually very localized to your specific code, so often they stray into offtopic-space) – jkerian Apr 02 '13 at 18:42
  • Marking this as correct for resolving the compilation errors. Thanks :) – asloob Apr 03 '13 at 04:11