-1

I have a strange problem by initiating a class object. The problem is as strange as well not easily reproducible. However I will try to give an indicating example. I have inheritance classes.

class BarClass {
public:
   BarClass() {
      ...
   }
   BarClass(int i, int j) {
      ...
   }
   void doSomething() { ... }
};
class FooClass : public BarClass {
public:
   FooClass() {
   }
   FooClass(int i, int j) : BarClass(i,j) {
      ...
   }
};

Sometime if I initiate objects with following manner, I will get segmentation fault error by initialization.

FooClass foo1;
foo1.doSomething();
FooClass foo2(10, 20);
foo2.doSomething();

If I use explicit pointer new, then it is OK..

FooClass *foo1= new FooClass();
foo1->doSomething();
FooClass foo2(10, 20);
foo2.doSomething();

The following code will give me a compiler error on line 2.

FooClass foo1();
foo1.doSomething();
FooClass foo2(10, 20);
foo2.doSomething();

how should I properly initiate a object, especially when it has default constructor and those with arguments.

rnd_nr_gen
  • 2,203
  • 3
  • 36
  • 55
  • 2
    You have a bug on the line 42, exactly among those dots. – BЈовић Nov 05 '10 at 13:07
  • 1
    It's all about what the constructors and `doSomething()` do - that's what user VJo is trying to tell. Please roll in the complete class definitions enough to reproduce the problem. – sharptooth Nov 05 '10 at 13:07
  • I suppose you mean `FooClass::FooClass(int i, int j) : BarClass(i, j) { }` ? – wilhelmtell Nov 05 '10 at 13:09
  • @bosmacs yes that looks like a typo though. – CashCow Nov 05 '10 at 13:12
  • 5
    -1 Essentially this question is "something's wrong with my secret code, what's wrong, and by the way, here's some other code that doesn't even compile". – Cheers and hth. - Alf Nov 05 '10 at 13:12
  • @sharptooth Doesn't have to be. In a case of stack over write, a bug can be anywhere, not even in the presented code (which isn't really showed in the post). – BЈовић Nov 05 '10 at 13:13
  • 2
    *Don't be vague, be an ace; write a proper test-case!* http://tinyurl.com/so-hints http://sscce.org/ http://www.xs4all.nl/~weegen/eelis/iso-c++/testcase.xhtml (Though I wouldn't downvote for it on a question so new, you should definitely fix this.) –  Nov 05 '10 at 13:15
  • 1
    Shouldn't FooClass() {} be: FooClass() : BarClass() {} Also you can declare one of the constructors in both base and derived classes to be 'explicit' to avoid confusions for the compiler. – yasouser Nov 05 '10 at 13:16
  • 1
    @anand: The base class's default ctor should be called automatically. I think you're misunderstanding 'explicit'. –  Nov 05 '10 at 13:18
  • @anund.arumug. There is no need to invoke the default constructor of the base which will happen automatically. explicit is used for constructors that take one parameter or one parameter plus some extra ones with default values. None of his constructors are that way (although the copy-constructor does take one parameter). – CashCow Nov 05 '10 at 13:51
  • 'In initialisation' - meaning 'in the allocation' (memory scribbler), 'in the initialiser function' (look at the line in question)? Even if it's 'on that line' you may learn something by looking at what it's doing in the assembly code for that line, rather than saying 'ah, on line 2'. – ijw Nov 05 '10 at 13:52
  • I would also add: 'sometime' meaning 'it happens reliably on specific lines but not for every line where I initialise a variable this way' or 'sometime' meaning 'for a given line, sometimes it works and sometimes it doesn't'? (I don't think anyone will solve your bug for you the way you've presently described it - none of the (compilable) code above is wrong - but you might find that either characterising it better will help them help you, or the answer will come to you if you pin down what you're seeing a bit better.) – ijw Nov 05 '10 at 13:55

4 Answers4

7

Your last issue first...

FooClass foo1();

does not create an object of type FooClass but declares a function called foo1() that takes no parameters and returns a FooClass. Remove the parentheses to create the instance as you did in the first code sample.

why you get a segmmentation fault may have something to do with your destructor which we can't see, and this doesn't get invoked in your second example which leaks.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • Thanks for all your suggestions and comments. I am sorry that I was not able to have a clear test case and gave somehow a misleading question. After taking the suggestions into consideration I tried to look into my constructor step by step and found that yes! the problem is large memory usage. And I put the problem in another question http://stackoverflow.com/questions/4106655/c-how-large-is-the-attributes-can-a-class-object-hold-how-to-determine-the-s – rnd_nr_gen Nov 05 '10 at 14:20
2

You probably have some bug in your constructor or in doSomething(). Without knowing what happens in these functions there is no way to say what exactly that bug is.

sth
  • 222,467
  • 53
  • 283
  • 367
2

Most likely sizeof(YourClass) is too large for the stack, which would explain why only heap allocation succeeds.

visitor
  • 8,564
  • 2
  • 26
  • 15
0

Only use -> with pointers.

FooClass foo1();
foo1->doSomething();

Needs to be

FooClass foo1;
foo1.doSomething();
Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55