473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3075
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: BCC | last post by:
Hi, A colleague has some code like this: class CMyObject { // Bunch of Member functions } class CMyObjectList: public std::vector<CMyObject> {
3
by: Pelle Beckman | last post by:
Hi all, I have a few - beginners - questions: * Why can't I put object references in a std::vector, i.e std::vector<MyClass&> ? At least in doesnt work in gcc (mingw, win32) * What's the...
8
by: Simon Elliott | last post by:
#include <vector> #include <iostream> int main (int argc, char *argv) { std::vector<int> vi; vi.push_back(1); vi.push_back(2); vi.push_back(3); int* pi = vi.begin(); std::cout << "result:"...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
7
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
2
by: zl2k | last post by:
hi, all I need to use gsl_vector pointer with std::vector but not sure how to free the memory when I don't need it. Here is a piece of the code. =================== std::vector<gsl_vector *...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.