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.

9
  • That's an array (with no known size) of pointers.
    – chris
    Apr 1, 2013 at 5:41
  • Yes, I want a dynamic array.
    – asloob
    Apr 1, 2013 at 5:43
  • 1
    use a std::vector
    – jkerian
    Apr 1, 2013 at 5:44
  • std::vector<Structure*> or if you want to do C way, Structure**
    – Karthik T
    Apr 1, 2013 at 5:44
  • 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. Apr 1, 2013 at 6:00

1 Answer 1

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.

5
  • 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++. Apr 1, 2013 at 6:18
  • @BenjaminLindley: What nasty hack to specifically allow in the language... had never heard of that nonsense before.
    – jkerian
    Apr 1, 2013 at 6: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 1, 2013 at 7: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 2, 2013 at 18:42
  • Marking this as correct for resolving the compilation errors. Thanks :)
    – asloob
    Apr 3, 2013 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.