472,122 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

std::Vector, classes and pointer questions

I've searched all the forums but cannot find an answer to this question.

I do the following:

vector<MyClass*> myClassList;

Later in the program I try to add to myClassList with a .push_back(...)
I get an "out of memory" runtime error. I know I'm not out of memory
because normal vectors such as vector<int> a, still work, and still work
fine.
I've tried the following .push_back's

1) myClassList.push_back(new MyClass)
2) myClassList.push_back(new MyClass())
3) MyClass *Temp = new MyClass;
myClassList.push_back(Temp)

I have also tried changing the vector to
vector<MyClass> myClassList;

And have tried .push_back(..) in many different ways with it also. No luck,
still "out of memory" runtime error.

MyClass is defined as follows:

class MyClass
{
AnotherClass* AClass;
int a;
int b;
char * a;
char *b;

MyClass() {};
~MyClass() {};
void Create() {AClass = new AnotherClass;};
void Destroy() {if(AClass) delete(AClass); AClass = NULL};

}
I've also tried changing AnotherClass* AClass to AnotherClass AClass and
still the same problem.

Questions:
1) What could be causing the vector to show an "out of memory" error?
2) All the tutorials I've read use simple types in their vectors... what is
the proper way to use vectors with created classes and pointers?

Best Regards,
Venn
Jul 23 '05 #1
4 2980
Venn Syii wrote:
I've searched all the forums but cannot find an answer to this question.

I do the following:

vector<MyClass*> myClassList;

Later in the program I try to add to myClassList with a .push_back(...)
I get an "out of memory" runtime error. I know I'm not out of memory
because normal vectors such as vector<int> a, still work, and still work
fine.
I've tried the following .push_back's

1) myClassList.push_back(new MyClass)
2) myClassList.push_back(new MyClass())
3) MyClass *Temp = new MyClass;
myClassList.push_back(Temp)

I have also tried changing the vector to
vector<MyClass> myClassList;

And have tried .push_back(..) in many different ways with it also. No luck,
still "out of memory" runtime error.

MyClass is defined as follows:

class MyClass
{
AnotherClass* AClass;
int a;
int b;
char * a;
char *b;

MyClass() {};
~MyClass() {};
void Create() {AClass = new AnotherClass;};
void Destroy() {if(AClass) delete(AClass); AClass = NULL};

}
I've also tried changing AnotherClass* AClass to AnotherClass AClass and
still the same problem.

Questions:
1) What could be causing the vector to show an "out of memory" error?
2) All the tutorials I've read use simple types in their vectors... what is
the proper way to use vectors with created classes and pointers?

Best Regards,
Venn

Post minimal *real* (compilable, runnable) code that exhibits the problem.

Using my crystal ball, however, leads me to believe that you're
corrupting your free store, probably by deleting the contained pointer
to `AnotherClass' twice.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #2
"Venn Syii" <ve*******@hotmail.com.nothere> wrote...
I've searched all the forums but cannot find an answer to this question.
The answer (or at least the beginning of the answer) is presented in
the FAQ 5.8.
[...]

Jul 23 '05 #3
Well, considering that I've tried on the magnitude of about 35 things...
which "compilable" set of code would you like? You'd probably have a
problem with that also...

The fact is that I haven't deleted the contained pointer 'AnotherClass'
twice... I haven't been able to get to that point yet. Try these three
lines of 'compilable code' and you'll get the same effect:

vector<int*> a;
a.push_back(new int(1));
//or
a.push_back(new int);
//or

Now when I do
vector<int> a;
a.push_back(10);
//All is good....

On further inpsection,
vector<MyClass*> MyList;
int a = MyList.size();
// a in the debugger comes out to be -100137 or something in that
//area... not "0"

If, "Using my crystal ball, however, leads me to believe that you're
corrupting your free store, probably by deleting the contained pointer to
`AnotherClass' twice." is the best post you could come up with... why even
post? Use your 'crystal' ball to find another post or better yet.... use it
to find the 'compilable code' and it's solution. Thanks. :-)

Regards,
Venn

"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:37*************@individual.net...
Venn Syii wrote:
I've searched all the forums but cannot find an answer to this question.

I do the following:

vector<MyClass*> myClassList;

Later in the program I try to add to myClassList with a .push_back(...)
I get an "out of memory" runtime error. I know I'm not out of memory
because normal vectors such as vector<int> a, still work, and still work
fine.
I've tried the following .push_back's

1) myClassList.push_back(new MyClass)
2) myClassList.push_back(new MyClass())
3) MyClass *Temp = new MyClass;
myClassList.push_back(Temp)

I have also tried changing the vector to
vector<MyClass> myClassList;

And have tried .push_back(..) in many different ways with it also. No
luck, still "out of memory" runtime error.

MyClass is defined as follows:

class MyClass
{
AnotherClass* AClass;
int a;
int b;
char * a;
char *b;

MyClass() {};
~MyClass() {};
void Create() {AClass = new AnotherClass;};
void Destroy() {if(AClass) delete(AClass); AClass = NULL};

}
I've also tried changing AnotherClass* AClass to AnotherClass AClass and
still the same problem.

Questions:
1) What could be causing the vector to show an "out of memory" error?
2) All the tutorials I've read use simple types in their vectors... what
is the proper way to use vectors with created classes and pointers?

Best Regards,
Venn

Post minimal *real* (compilable, runnable) code that exhibits the problem.

Using my crystal ball, however, leads me to believe that you're corrupting
your free store, probably by deleting the contained pointer to
`AnotherClass' twice.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays

Jul 23 '05 #4

"Venn Syii" <ve*******@hotmail.com.nothere> skrev i en meddelelse
news:Gx****************@twister.rdc-kc.rr.com...
I've searched all the forums but cannot find an answer to this question.

I do the following:

vector<MyClass*> myClassList;
[snip]

And have tried .push_back(..) in many different ways with it also. No
luck, still "out of memory" runtime error.

MyClass is defined as follows:

class MyClass
{
AnotherClass* AClass;

[snip]
Questions:
1) What could be causing the vector to show an "out of memory" error? corrupted heap. 2) All the tutorials I've read use simple types in their vectors... what
is the proper way to use vectors with created classes and pointers? Same as for a simple type.

When learning C++ wait playing with pointers before you know the language.
This is most surely the correct way to learn C++, and most surely you do not
need to know about pointers at this stage in your education.

Kind regards
Peter
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by BCC | last post: by
3 posts views Thread by Pelle Beckman | last post: by
8 posts views Thread by Simon Elliott | last post: by
8 posts views Thread by Ross A. Finlayson | last post: by
9 posts views Thread by kathy | last post: by
32 posts views Thread by zl2k | last post: by
9 posts views Thread by aaragon | last post: by
7 posts views Thread by Thomas | last post: by
8 posts views Thread by jacek.dziedzic | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.