473,698 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.pus h_back(new MyClass)
2) myClassList.pus h_back(new MyClass())
3) MyClass *Temp = new MyClass;
myClassList.pus h_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 3097
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.pus h_back(new MyClass)
2) myClassList.pus h_back(new MyClass())
3) MyClass *Temp = new MyClass;
myClassList.pus h_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*******@hotm ail.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*******@aust in.rr.com> wrote in message
news:37******** *****@individua l.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.pus h_back(new MyClass)
2) myClassList.pus h_back(new MyClass())
3) MyClass *Temp = new MyClass;
myClassList.pus h_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*******@hotm ail.com.nothere > skrev i en meddelelse
news:Gx******** ********@twiste r.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
10721
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
1748
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 difference between passing member inits in the c-tor funtion from
8
2611
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:" << *pi << std::endl;
8
5108
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
9
2306
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 CMyClass()); //???? Required ??????????????//
32
69678
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: std::vector<int> fun(){ //build the vector v; return v; }
9
8895
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; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
7
2530
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
3960
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 * v; gsl_vector * a = gsl_vector_alloc(3); gsLvector_set(a, 0, 7); v.push_back(a);
8
3611
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 class simple_vector<that is a hand-rolled substitute for array<>. With the latter I have code that tracks all allocations and destructions, so I can account for all the memory. The question is about std::vector<-- how can I track memory usage
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.