data structure: (=) assignment operator causes segfault?!


Alright. I have written a class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef string newtype;

class blabla{

public:
    //constructors, argument is a string... sets the private var.
    //1 destructor, does nothing, because no memory allocation

    const string info_getter(); //returns a string...

private:

    newtype returned_by_getter;
};


In a header file somwhere else...

1
2
3
4
5
6
7
8
9
//defs...
//includes...

//yadda yadda...

struct a_structure{
    string bla = blabla().info_getter();
    string whatever = "";
};


in another .cpp file I have this:
1
2
3
//......

vector<a_structure> list_of_this_structure = loads_from_file(); //lol?? 


and FINALLY, the root of all my problems here:

1
2
//in loads_from_file():
a_structure x = a_structure(); //declare a structure with a default being it's default constructor... with no args 


I've used this type of definition before to declare instances of data structures, but never has it spit out a segfault. i did some testing. I moved the vector declaration to the main function, and got the same results no matter what. Always a segfault when trying to set the a_structure data structure to it's default. I eliminated this assignment operation, and it worked!!

So, now I'm left with a question: why did it work, and what makes this scenario unique?

The latter can be answered: This is the first time I have declared a data structure with a variable set to default to the function member of a class.

The former is still unanswered, though.

Can someone help me to understand this dynamic? I've done this plenty of times, but this is the first time I have used a function member to declare a variable. I have used variable members plenty, and it worked fine.

My lack of an explanation may be because of ignorance to somthing, which is why I ask.
NEVERMIND... it is still spitting out the segfault...


Same location though... exactly.
The incomplete [pseudo]code snippets don't help, can you post a minimal, compilable example that demonstrates the problem?

And FYI a_structure x default constructs,
a_structure x = a_structure(); is at least two constructions and an assignment.
Thank you naraku.

Well, I can not seem to figure it out. I have pin pointed it to a location... not quite sure why in heck it's doing this though... I can really provide workable code, because I'm not even sure if I can, because I don't even know where the problem is...

I do know this: it's centered around a class. Specifically a class I wrote a while back to handle time.

I've done some testing, and it seems to be happening outside my code... but inside my code... somewhere...

Here is what I know:
1. It happens when the constructor of this class is called
2. Nothing in the constructor ever gets called...

Strangest thing I have ever had a program do... i re-wrote the constructors a while back, so that may be the problem, as this is the first time I'm actually using the class since it's modification.

Compile this little thing:

main.cpp: http://pastebin.com/cFEJRiNs
chrono_date.h: http://pastebin.com/YAqqvEck
chrono_date.cpp: http://pastebin.com/mWedL9Ej

for me, it immediately crashes...
NOTE: I modified the class again, eliminating the statics and using const correctness. I still get the segfault.

This was one of the first ever classes I wrote, so forgive the messiness... I may rewrite it sometime in the future so that it is more organized than it is.
wow... nvm... I found the problem while examinining the chaos I definitely plan on re-writing...

For anyone reading: The problem was that a variable was being set to a function inside the class as a default... in it's declaration... so, when constructing the class, it would initialize that variable, then it would have to call the function.. and after that I have no explanation...
this looks wrong to me but I don't know...

a_structure x = a_structure();

this looks like a function call but I did not see the function defined.

I thought to use default constructors you would go like this...

a_structure x();

am I wrong?
Try changing ...
in chrono_date.h line 46 to int year_v;// = chrono_date::year(); and initialize year_v in chrono_date ctor like year_v = chrono_date::year();

You may also wnat to take a look at the chrono header http://en.cppreference.com/w/cpp/header/chrono and/or Boost.Date_time http://www.boost.org/doc/libs/1_54_0/doc/html/date_time.html
Last edited on
Manga wrote:
this looks wrong to me but I don't know...

a_structure x = a_structure();

this looks like a function call but I did not see the function defined.
http://www.cplusplus.com/forum/beginner/107427/#msg582787

I thought to use default constructors you would go like this...

a_structure x();

am I wrong?
That is a function declaration, creating the object with a default ctor call would simply be a_structure x;
opps...

thanks naraku.
Topic archived. No new replies allowed.