The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Segmentation fault big123456 Linux 0 07-20-2007 02:01 AM
Hi! segmentation fault vijlak High Level Programming 4 11-13-2006 06:48 AM
Segmentation Fault compbug UNIX for Dummies Questions & Answers 3 04-21-2006 07:43 AM
Segmentation fault jshaulis AIX 1 06-01-2004 01:16 PM
segmentation fault omran High Level Programming 2 08-01-2003 05:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-04-2008
Registered User
 

Join Date: Apr 2008
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 04:18 AM. Reason: cleaned up code sample
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-10-2008
Registered User
 

Join Date: Apr 2008
Posts: 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
solved it?

hello,
i wonder if you solved the problem or not?and if solved how?
thanks
Reply With Quote
  #3 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: Apr 2008
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
hi xyzt,

no .. I never did solve the problem .. pressure of work schedule forced me to work around it by using exit(0) instead of return 0.

I'm still stumped why the arguments are nil and why the exception is thrown if i use return.

my code has a nasty fix:

#ifdef WIN32
return 0;
#else
exit(0);
#endif




I'm not happy with it .. but the code runs with no segentation fault. Have you seen something similar?
Reply With Quote
  #4 (permalink)  
Old 05-06-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,289
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
The stack in main() has been corrupted with an overrun of one or more the variable(s) on it. Probably in one of the functions. It has overwritten the stack pointer that return uses to go back to the calling routines (_start is the usual name).

When main() attempts a return the SP is not "aimed" where it should be. And your code tries to read program text outside of process memory. Address: 0x08

exit() does not return from main or wherever - it starts image rundown directly. It does not use the SP.
You need to fix the problem before you push your code to production. Try running electric fence or whatever other memory checkers you have.

Last edited by jim mcnamara; 05-06-2008 at 09:50 AM.
Reply With Quote
  #5 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: Jun 2004
Posts: 15
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
try this
New code is argv[0]

std::cout << "argc="<<argc<<" argv="<<argv[0]<<std::endl;
It will work. argv is an array of pointer.
Reply With Quote
  #6 (permalink)  
Old 05-06-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,289
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
The problem is not limited just to the arguments to main().

The OP could dummy up a stack canary to see when and in what function the variables get overwritten.
Reply With Quote
  #7 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: Dec 2007
Location: Simi Valley, CA
Posts: 28
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
args

argv is a double pointer to strings. Just dumping the pointer to stdout won't work out very well. Consider traversing the array, using argc to resolve the end of the list of pointers.

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

int main(int argc,char** argv)
{
vector<string> args;
for (int i=1;i<argc;i++)
{
args.push_back(argv[i]);
}

for (vector<string>::iterator it=args.begin();it!=args.end();it++)
{
string& s=*it;
cout<<s<<endl;
}
return 0;
}
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix curses.h cut command in unix daemon process export command in unix find grep find mtime find null character in a unix file glance unix grep multiple lines grep or grep recursive inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 12:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101