LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2006, 10:48 AM   #1
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Rep: Reputation: 15
SIGSEGV when running an executable in a loop


Hi friends,

I have a shell script that runs an executable in a 'while' loop. The programs are in C. The executable is run around 100 times in the loop and different parameters are passed to the program each time. I used to get segementation fault more often than not when running this script. Later found out that there is a 'char *' variable to which a string of more than 1 byte is copied. When i changed this to a char array of 10, this problem seem to have disappeared. anybody has any idea why this happened?

Thanks for any suggestions.
 
Old 03-23-2006, 11:37 AM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
Is the question Why you program seg faulted?

If that is the case the reason might be if you have a char* and you don't do a new there is no space to store the value you are coping to the char*. Since you changed it to a char[10] when the program starts that array is given enough memory space to store 10 characters. You could have done the same thing by doing this

char* myChar;
myChar = new char[10];

that will create a memory location to store 10 characters.

Hope that was the question.
 
Old 03-23-2006, 01:23 PM   #3
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Original Poster
Rep: Reputation: 15
Yes, that was my question. but i know why it seg faulted, because there was not enough space. but i want know how using a 'char *' is causing SIGSEGV in the loop, since all the invocations of the executable have its own memory space?
 
Old 03-23-2006, 04:01 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,667

Rep: Reputation: 217Reputation: 217Reputation: 217
When you have a table, you have memory to copy the string. If you just have char *, there's no space (what's more, you have it uninitialized, so it points to some random address). When you try to copy to space that doesn't exist...
 
Old 03-23-2006, 04:05 PM   #5
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
They have there own memory space but was your pointers pointing at the memory?

The char* points to a memory location, when first created it points at a random location, ( basically what the value was at its location when it was created ). It this value is outsidde your memory SIGSEGV.

When you set it as an array, 10 bytes where allocated and the char* was set to point to the first of them. The same as if you did new char[10]; So its now poiting at the programs allowed memory.

Its all to do with initializing your pointers, and knowing where their pointing.
 
Old 03-24-2006, 11:09 AM   #6
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Original Poster
Rep: Reputation: 15
i get it, thanks a lot for the replies guys.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
running executable in C++ code psIpher Programming 2 11-25-2005 01:44 PM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 04:57 AM
Running an executable. wesleyarchbell Linux - Software 4 05-03-2005 03:50 AM
running an executable wogga Linux - Software 8 06-12-2004 04:21 AM
running an executable aznamvet Linux - Software 5 06-10-2004 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration