473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what runs before main()? debug trouble

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
4 3829
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
"lilburne" <li******@godzi lla.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
"Jacek Dziedzic" <ja***********@ janowo.net> wrote in message
news:27******** ******@janowo.n et...
[...]


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
Jacek Dziedzic wrote:
"Jacek Dziedzic" <ja***********@ janowo.net> wrote in message
news:27******** ******@janowo.n et...
[...]

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_variabl e" 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.l earn.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
22750
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F: operator delete(void*) (vg_replace_malloc.c:156) ==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned)...
10
4961
by: linq936 | last post by:
Hi, I have many assert() call in my code, now I am considering to replace them with exception. The reason I want to do this change is that with the program going bigger and bigger, it is hard to test all the corner cases, and thus assert() does not work as good as before. The problem is if there are something really bad which was not captured...
6
2634
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we have something like stack size growing enormously and then saying you can't access ,so a segfault ? It would be helpful if someone can run the...
28
2695
by: Vishal Naidu | last post by:
i m new to the C world... i ve been told by my instructors not to use goto stmt.. but no one could give me a satisfactory answer as to why it is so.. plz help me out of this dilemma, coz i use a lot of goto in my codes....
10
4877
by: Scott | last post by:
I have a simple asp.net app which works fine in debug mode, but crashes on the following line when I run it on the production server: Dim dt As DataTable I have tried the following variations which produce the same result: Dim dt As System.Data.DataTable Dim dt As DataTable = New DataTable The error message reads: ...
3
5140
by: Kceiw | last post by:
Dear all, When I use #include "queue.h", I can't link it. The error message follows: Linking... G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In function `main': G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to `Queue<char>::Queue()' G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In function...
15
8995
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str, &endptr, 10); printf("errno=%d\n", errno);
8
5558
by: Jothishankar | last post by:
Hi, I am new to c#. I am trying to build an application that does backup of files to an external hard disk. My application behaves strangely. When i run the application under debug mode (F5), it works properly. But when i run it in normal mode (Ctrl+F5) the program stalls in the middle. I use a couple of worker threads which run...
2
1475
by: Matthew Wilson | last post by:
In this code, I tried to kill my thread object by setting a variable on it to False. Inside the run method of my thread object, it checks a different variable. I've already rewritten this code to use semaphores, but I'm just curious what is going on. Here's the code:
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.