04-04-2008
C++ Segmentation Fault on exit of main
Hi,
I have 2 problems with a simple C++ app which i feel may be related.
1/ my app throws a Segmentaion Fault when my code exists from main(). I have stripped it to it's simplest form with no code in main and it still generates a segmentation fault.
I'm not sure what is causing this, possibly i'm missing a compiler switch in my build?
see code sample below.
2/ if i debug my test code in dbx i notice that argc is set to 0 and argv is nil. on further testing i find argc and argv are always 0 no matter what command line arguments i pass to the app (tested with a modifed veriosn that prints the output of argc and argv)
Has anyone got any ideas what could be causing these problems? I tried the same code on a solaris10 box and got the same result so i'm assuming it's something i'm doing wrong with my compile and link settings.
would appreciate any input ..
ta,
SOP
First off .. my enviornment.
OS: SunOS devsun02 5.8 Generic_108528-29 sun4u sparc SUNW,Ultra-4
64-bit sparcv9 kernel modules
I also verified i get the same reults when i try my code on a Solaris10 box
C++ compiler: CC: Sun WorkShop 6 update 1 C++ 5.2 2000/09/11
Problem: When my program exists main it throws a Segmentaion Fault.
my code is as follows
Testharness.cpp:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "argc="<<argc<<" argv="<<argv<<std::endl;
return 0;
}
my copile and link:
CC -xarch=v9 -c -o src/TestHarness.o src/TestHarness.cpp
OS version : 5.8
Library Path : -L/opt/SUNWspro/lib/v9 -L/usr/lib/sparcv9 -L/opt/sunstudio9/SUNWspro/lib/v9 -L/usr/ucblib/sparcv9/
ld -o Testharness src/TestHarness.o -64 -L/opt/SUNWspro/lib/v9 -L/usr/lib/sparcv9 -L/opt/sunstudio9/SUNWspro/lib/v9 -L/usr/ucblib/sparcv9/ -lCrun -lCstd -lc
when I run:
>./Testharness
argc=0 argv=0
Segmentation Fault
the libraries Testharness is linking to:
ldd Testharness
libCrun.so.1 => /usr/lib/64/libCrun.so.1
libCstd.so.1 => /usr/lib/64/libCstd.so.1
libc.so.1 => /usr/lib/64/libc.so.1
libucb.so.1 => /usr/ucblib/sparcv9//libucb.so.1
libresolv.so.2 => /usr/lib/64/libresolv.so.2
libsocket.so.1 => /usr/lib/64/libsocket.so.1
libnsl.so.1 => /usr/lib/64/libnsl.so.1
libelf.so.1 => /usr/lib/64/libelf.so.1
libdl.so.1 => /usr/lib/64/libdl.so.1
libmp.so.2 => /usr/lib/64/libmp.so.2
/usr/platform/SUNW,Ultra-4/lib/sparcv9/libc_psr.so.1
I set dbx to check access and memuse and i get no further information.
output from dbx
(process id 13308)
argc=0 argv=0
signal SEGV (no mapping at the fault address) in (unknown) at 0x8
0x0000000000000008: <bad address 0x8>
any help would be most appreciated. .
Last edited by SonOfPerdition; 04-04-2008 at 08:18 AM..
Reason: cleaned up code sample
8 More Discussions You Might Find Interesting
1. AIX
I am tring to install Lotus Domino/Notes 5.0.5 on a AIX 4.3.3 server.
I go to run the cdrom/ibmpow/install and I get the following error.
Lotus Notes for Unix Install Program
---------------------------------------------
./install: 10088 Segmentation fault
This had Lotus Notes installed... (1 Reply)
Discussion started by: jshaulis
1 Replies
2. Programming
Hi,
I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Discussion started by: mind@work
3 Replies
3. Programming
What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies
4. Programming
I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek:
#include </usr/include/mysql++/mysql++.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies
5. Programming
This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b:
int main()
{
mysqlpp::Connection conn(false);
if... (3 Replies)
Discussion started by: sepoto
3 Replies
6. Programming
i have this code
int already_there(char *client_names, char *username) {
int i;
for(i = 0; i<NUM; i++) {
printf("HERE\n");
if (strcmp(client_names, username)==0) return(1);
}
return(0);
}
and i get a segmentation fault, whats wrong here? (7 Replies)
Discussion started by: omega666
7 Replies
7. Programming
I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this :
i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies
8. Programming
Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1...
I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one.
Below are two very condensed snippets of which I have added the results inside the each code section.
IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies
LEARN ABOUT ULTRIX
getopt
getopt(3) Library Functions Manual getopt(3)
Name
getopt - get option letter from argument vector
Syntax
#include <stdio.h>
int getopt (argc, argv, optstring)
int argc;
char **argv;
char *optstring;
extern char *optarg;
extern int optind, opterr;
Description
The subroutine returns the next option letter in argv that matches a letter in optstring. The optstring is a string of recognized option
letters; if a letter is followed by a colon, the option is expected to have an argument that may or may not be separated from it by white
space. The optarg is set to point to the start of the option argument on return from
The function places in optind the argv index of the next argument to be processed. The external variable optind is automatically initial-
ized to 1 before the first call to
When all options have been processed (that is, up to the first non-option argument), returns EOF. The special option -- may be used to
delimit the end of the options; EOF will be returned, and -- will be skipped.
Diagnostics
The function prints an error message on stderr and returns a question mark (?) when it encounters an option letter that is not included in
optstring. Setting opterr to 0 disables this error message.
Examples
The following code fragment shows how one might process the arguments for a command that can take the mutually exclusive options a and b,
and the options f and o, both of which require arguments:
#include <stdio.h>
main (argc, argv)
int argc;
char **argv;
{
int c;
extern int optind, opterr;
extern char *optarg;
.
.
.
.
while ((c = getopt (argc, argv, "abf:o:")) != EOF)
switch (c) {
case 'a':
if (bflg)
errflg++;
else
aflg++;
break;
case 'b':
if (aflg)
errflg++;
else
bproc( );
break;
case 'f':
ifile = optarg;
break;
case 'o':
ofile = optarg;
bufsiza = 512;
break;
case '?':
errflg++;
}
if (errflg) {
fprintf (stderr, "usage: . . . ");
exit (2);
}
for ( ; optind < argc; optind++) {
if (access (argv[optind], 4)) {
.
.
.
}
See Also
getopt(1)
getopt(3)