0

I want to use foo array of objects in all of Spam methods.

#include "spam.h"
class Foo: public Bar
{
public:
   Foo(Layer* layer)
   int getNumber() const;
   // Something else
};

class Spam: public Layer
{
  Spam();
  // some methods
private:
  Bar** foo;  //here is the problem
};

This method (of course with one *) worked for me when I was creating one object.

void Spam::fun1()
{
  Bar **foo = new Bar*[1];
  foo[0] = new Foo(this);
  //foo[1] = new Foo(this);

  //foo[1]->getNumber(); // works correctly
}

void Spam::fun2()
{
  //foo[1]->getNumber(); // foo[1] and foo[2] are NULL
  foo[0]->getNumber():   // not working at all

}

But even I use Bar** foo or Bar** foo[2], Xcode shows me that I created new pointer to object.

Xcode screenShoot

[edit]I commented out wrong code example, my oversight, thanks guys.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
chriss
  • 31
  • 6
  • 1
    You're not using "works correctly" in a healthy way. Clearly `foo[1]` is an out-of-bounds access. – Kerrek SB Jan 05 '17 at 02:17
  • Any statement in your example containing `foo[1]` that "works correctly" only seems to work. It may stop working at any time for no reason. This is because foo contains only 1 element (because of `new Bar*[1]') which means only the index 0 is legal to access (for an array of size N you can access the indices 0 to N-1). See [this answer](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) for more information on undefined behavior. – François Andrieux Jan 05 '17 at 02:19
  • Okey, I agree, that's my fault it's mean even foo[0], working in Spam::fun1(), but i look for correct way to point on this and use the same object in Spam::fun2(), that's my problem. – chriss Jan 05 '17 at 02:33

3 Answers3

2

In your fun1, the statement Bar **foo = new Bar*[1]; defines a new, local variable foo, which hides the class member name.

If you want to refer to the class member instead, say foo = new Bar*[1];.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

It looks like you have a buffer overflow here:

Bar **foo = new Bar*[1];   // allocate one-element array of pointers to Bar
foo[0] = new Foo(this);    // valid assignment
foo[1] = new Foo(this);    // off the end of the array.
geofftnz
  • 9,954
  • 2
  • 42
  • 50
0

In method Spam::fun1() you allocated memory to a local variable "foo" of type "Bar **". Instead you have to allocated memory for your class member variable "foo". Two things you can do to solve this issue,

  1. Remove the local variable declaration of "foo" for this use the below code instead of "Bar **foo = new Bar*[1];" inside "Spam::fun1()".

    foo = new Bar*[1];

  2. If you wanted to keep the local variable due to some reasons, use "this" pointer to indicate the class member,

    this->foo = new Bar*[1];

Just a suggestion, you can avoid these kind of confusions, if you follow proper naming conventions in your source code like below.

class CSpam: public CLayer
{
private:
Bar ** mFoo;
};

void Spam::fun1()
{
  mFoo = new Bar*[1]; 
}
Nitheesh George
  • 1,357
  • 10
  • 13
  • [Google C++ Style Guide][2] [C++ Programming Style Guidelines][3] [GCC C++ Coding Conventions][1] [1]:https://gcc.gnu.org/wiki/CppConventions [2]: https://google.github.io/styleguide/cppguide.html [3]: http://geosoft.no/development/cppstyle.html – Nitheesh George Jan 05 '17 at 03:53