Connecting Tech Pros Worldwide Help | Site Map

std::Vector, classes and pointer questions

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:49 AM
Venn Syii
Guest
 
Posts: n/a
Default 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



  #2  
Old July 23rd, 2005, 12:49 AM
Artie Gold
Guest
 
Posts: n/a
Default Re: std::Vector, classes and pointer questions

Venn Syii wrote:[color=blue]
> 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
>
>[/color]
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
  #3  
Old July 23rd, 2005, 12:49 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: std::Vector, classes and pointer questions

"Venn Syii" <venn_syii@hotmail.com.nothere> wrote...[color=blue]
> I've searched all the forums but cannot find an answer to this question.[/color]

The answer (or at least the beginning of the answer) is presented in
the FAQ 5.8.
[color=blue]
> [...][/color]


  #4  
Old July 23rd, 2005, 12:49 AM
Venn Syii
Guest
 
Posts: n/a
Default Re: std::Vector, classes and pointer questions

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" <artiegold@austin.rr.com> wrote in message
news:3774i2F5a32c0U1@individual.net...[color=blue]
> Venn Syii wrote:[color=green]
>> 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[/color]
> 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[/color]


  #5  
Old July 23rd, 2005, 12:51 AM
Peter Koch Larsen
Guest
 
Posts: n/a
Default Re: std::Vector, classes and pointer questions


"Venn Syii" <venn_syii@hotmail.com.nothere> skrev i en meddelelse
news:GxsPd.6565$Sq5.367@twister.rdc-kc.rr.com...[color=blue]
> I've searched all the forums but cannot find an answer to this question.
>
> I do the following:
>
> vector<MyClass*> myClassList;
>[/color]


[snip]
[color=blue]
>
> 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;
>[/color]

[snip]
[color=blue]
> Questions:
> 1) What could be causing the vector to show an "out of memory" error?[/color]
corrupted heap.[color=blue]
> 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?[/color]
Same as for a simple type.[color=blue]
>
>[/color]
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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.