Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Core dumped with Vector in C++

Status
Not open for further replies.

PhatThuy

Programmer
Feb 1, 2003
2
0
0
CA
Hello all. I have a question for you. Please help.

Here's part of my code;

struct A_Key {
char* key;
long total;
};

struct A_Bucket {
vector<A_Key> storage;
int count;
int totalLen;
}bucket;

vector <A_Bucket*> myList; // vector element points to the address that bucket resides.

int main() {
....
bucket.count = 0;
myList.push_back(&bucket);
....
return 0;
}

---------------
My code compiles but when it runs to the line myList.push_back(&bucket) it give a core dumped. Can you please let me know what I did wrong and why core dumped occurs? Thanks for the help.

Sincerely
 
I am not very sure if I will answer correctly ... but here there is my first answers of all.

I think you forgot to feed your vector in the struc A_Bucket

this what I tested :

#include<vector>
#include <iostream>

using namespace std;

struct A_key {
char *key;
long total;
};

struct A_Bucket {
vector<A_key> storage;
int count;
int totalLen;
} bucket;

vector<A_Bucket> myList;

int main()
{
bucket.count=0;
myList.push_back(&bucket);

cout << &quot;count = &quot; << myList[0]->count << endl;
cout << &quot;totalLen = &quot; << myList[0]->totalLen << endl;

A_key tmp;

(myList[0]->storage).push_back(tmp);

cout << &quot;a_key total = &quot; << myList[0]->storage[0].total << endl;
cout << &quot;a_key key = &quot; << myList[0]->storage[0].key << endl;

return 0;

}


by this way it works ...

But it don't really anderstand what you want to do, so my advise will be :

It is better to don't use global variables, if you use de STL then I think it is better to stay in C++ mode and use class instead of struct.

By putting a pointer, and a vector in your struct you have to initialise it. So you have to write you initialise function. By using the class, it will be automatically the defaults contructors.


hope it helps

lcout
 
Thank you very much lcout. You're great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top