By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

what runs before main()? debug trouble

P: n/a
Hi!

First of all, I hope my problem is not too loosely tied to the
"standard C++" that is the topic of this group.

I have some code that exhibits a strange behaviour: on
one computer, where I compile with g++-3.2 it compiles and
works fine, on another computer, whether I compile with g++-3.0
or g++-2.95 (unfortunately 3.2 is not available) it crashes
with a segmentation violation on runtime.

I tried to find the offending code by commenting out most
of my program, and found out that the program crashes upon
entering an init() method of one of the objects. The bulk of
the function is never executed, since if I change its first line to
a std::cerr << "I'm here" << std::endl, it never appears on my
console.

Even if I try to execute this init() function as the first line of
main(), my program still crashes. As I can't use a debugger
on the system where it crashes, and it works fine on the
system where I have the debugger, I'm running out of options.

I've now come to the point, where if I start my main() with
a debug dump to cerr and then the offending function, I get
the debug dump at runtime, but not the debug dump of the
offending function. That leads me to the conclusion, that
something that runs before main is causing the trouble
(like overwriting memory, etc.). My question is...

/*** here's my C++ question ***/:

what is "the stuff" that runs before main()? I could recall
that top-level objects' constructors could be called
before main() and I double-checked these.
I also checked my statics and found nothing suspicious.
Is there anything else that runs any code before main
(apart from platform-specific stuff that goes before any
actual C++ code)?

Perhaps some of you can tell, by experience, what should
I look for? I've spent six hours now looking for the exact cause
and I'm getting frustrated. In plain-old-DOS I'd use a debugger
to single-step the code, unfortunately I can't do that now.
The assembler output of the compiler looks too complicated too.

TIA,
- J.

PS. The object whose method crashes my program is a child
object of a global object, like in

class B;

class A {
public:
B foo;
};

class B {
public:
init();
};

A global;

int main() {
global.foo.init(); // crashes
};

Of course this simple program works. That's why I'm lead to
believe there's some init stuff (not presented here, I can't post
the actual code because it's too long) that causes some hidden
trouble.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jacek Dziedzic wrote:

Of course this simple program works. That's why I'm lead to
believe there's some init stuff (not presented here, I can't post
the actual code because it's too long) that causes some hidden
trouble.


#include <cassert>

const int VALID = 98765;
const int DEAD = 0xdeaddead;

class validity {
int alive;
public:
validity() { alive = VALID; }
~validity() { alive = DEAD;}
bool valid(void * p);
};

bool validity::valid(void* p)
{
assert(p != 0);
return alive == VALID;
}

Stick something like the above cheapNcheerful in your
classes and add:

assert(vc.valid(this));

as the first statement of all methods. It will check that
things have been created, not delete, and provide some check
on memory getting trampled.

Jul 19 '05 #2

P: n/a
"lilburne" <li******@godzilla.net> wrote in message
news:bn*************@ID-203936.news.uni-berlin.de...
Jacek Dziedzic wrote:

Of course this simple program works. That's why I'm lead to
believe there's some init stuff (not presented here, I can't post
the actual code because it's too long) that causes some hidden
trouble.


#include <cassert>

const int VALID = 98765;
const int DEAD = 0xdeaddead;

class validity {
int alive;
public:
validity() { alive = VALID; }
~validity() { alive = DEAD;}
bool valid(void * p);
};

bool validity::valid(void* p)
{
assert(p != 0);
return alive == VALID;
}

Stick something like the above cheapNcheerful in your
classes and add:

assert(vc.valid(this));

as the first statement of all methods. It will check that
things have been created, not delete, and provide some check
on memory getting trampled.


Thanks for the advice, but I've now found out that my
problem is probably not C++ related. It looks like a
third party library (SCALI's mpi) is messing up my program.
When I cease to link it in, my program works fine. When
I link it, it core dumps. Must be some objects in the library
that have some code run before my main() that cause trouble,
damn it.

thanks, anyway
- J.
Jul 19 '05 #3

P: n/a
"Jacek Dziedzic" <ja***********@janowo.net> wrote in message
news:27**************@janowo.net...
[...]


Never mind my question. I've somehow figured out that
it's a third party library that causes the trouble. By some
weird magic everything works fine when I link it statically
and does not work when I link it dynamically. It still
puzzles me, but it's now definitely out of scope of this ng,
sorry.

- J.
Jul 19 '05 #4

P: n/a
Jacek Dziedzic wrote:
"Jacek Dziedzic" <ja***********@janowo.net> wrote in message
news:27**************@janowo.net...
[...]

Never mind my question. I've somehow figured out that
it's a third party library that causes the trouble. By some
weird magic everything works fine when I link it statically
and does not work when I link it dynamically. It still
puzzles me, but it's now definitely out of scope of this ng,
sorry.

- J.


Actually, there is a part that is within the scope of this
newsgroup. Many global and static variables are initialized
before the main() function. The constructor code for these
objects will be executed before the main() function.

Try this:
#include <iostream>
#include <cstdlib>

using std::cout;
using std::endl;

class My_Class
{
public:
My_Class()
{ cout << "My_Class constructor." << endl; }
~My_Class()
{ cout << "My_Class destructor." << endl;}
};

My_Class global_variable;

int main(void)
{
cout << "main() has begun." << endl;
cout << "main() has ended." << endl;
return EXIT_SUCCESS;
}

Hopefully, your compiler won't optimize the
"global_variable" away. If it does, make it
volatile:
volatile My_Class global_variable;

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.