LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-26-2003, 06:26 PM   #1
fatman
Member
 
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158

Rep: Reputation: 30
C Segmentation Fault


I have a program I wrote that runs to completion and then segmentation faults.
I have even put a printf at the end of the code, and the output appears, and then the segmentation fault occurs.
I am thinking memory leak, but I am not sure. There are many complex structs,and pointers to those structs being passed around a buncha functions.


Anybody have any suggestions?
 
Old 03-26-2003, 07:16 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Looks like the error is on line 27.

Seriously, though, it may help if you post some of the source code
 
Old 03-26-2003, 09:08 PM   #3
fatman
Member
 
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158

Original Poster
Rep: Reputation: 30
There's way too much source to post it all,
I was just hoping for any general ideas that would cause such a behavoir

here is the function that causes the trouble and some relevant lines:


typedef struct {
int numCells;
cell activeCells[];
} MRG;

MRG testGame;

deleteCell(&testGame, 17, 999);


Any thoughts?
 
Old 03-26-2003, 09:23 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Tried stepping it with a debugger?
At least that would lead you closer to where
it's happening :)

Cheers,
Tink
 
Old 03-26-2003, 10:07 PM   #5
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,578

Rep: Reputation: 186Reputation: 186
electric fence

If you are trying to chase down memory problems you might try "electric fence".
 
Old 03-26-2003, 10:12 PM   #6
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
In general, segfaults occur when you try to mess with memory that doesn't belong to you. (or in the context of software, when a process tries using memory that doesn't belong to it).

So for example, if you try to call the C++ delete statement on a pointer that is invalid (NULL, or doesn't point to a real object), you'll get a segfault.

In your code snippet, I would imagine that something inside the deleteCell function is trying to do this. Usually, when you allocate an object (or struct), using new or malloc(), it's a good idea to check whether the allocation was successful.

What does the deleteCell() function look like?

And definitely, as Tinkster says, try using a debugger to step through. If you use gdb, you can just run the program, wait for it to segfault, and then type "where" to get a good idea of where it went wrong. Another thing I like to do is put cout statements here and there, as progress reports. Like so:

cout << "Trying to make a new foo object..." << endl;
fooPtr = new foo();
if (fooPtr != NULL) cout << "allocation successful..." << endl;

etc.
 
Old 03-26-2003, 11:16 PM   #7
fatman
Member
 
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158

Original Poster
Rep: Reputation: 30
I am still at a loss.
Ok, here is the deleteCell function - be kind to my coding, its very rusty.
Basically this would be a member function for the MRG class if I was using C++.
This function does it job, it just causes the segmentation fault once the entire program is done.

void deleteCell(MRG * theGame, int locationX, int locationY){
// Function: deleteCell
// Pre: Cell at (locationX, locationY) exists
// Post: Cell has been deleted
// Purpose: to remove a cell from the game once all users have exited


int i;

// search active cell list for input location
for(i=0; i < (theGame->numCells - 1); i++){
// Once found, remove and adjust downward (should copy NULL up - not sure)
if(theGame->activeCells[i].cellLocation.locationX == locationX && theGame->activeCells[i].cellLocation.locationY == locationY){
theGame->activeCells[i] = theGame->activeCells[i+1]; // watchout here! - struct assignment?
locationX = theGame->activeCells[i+1].cellLocation.locationX;
locationY = theGame->activeCells[i+1].cellLocation.locationY;
}
}

theGame->numCells--;
}
 
Old 03-27-2003, 03:16 AM   #8
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
Here is how I used to chase down seg faults. Put a cout statemet before each line to see where in the loop the seg fault occurs.

theGame->activeCells[i] = theGame->activeCells[i+1]; // watchout here! - struct assignment?

Are you copying the entire struct?? I don't think you can do that unless you overloaded the assignment operator. I am a little rusty here though.

If that is a struct or class you're trying to copy, try copying each data member one at a time and see if that works.

Last edited by rmartine; 03-27-2003 at 03:18 AM.
 
Old 03-27-2003, 11:19 AM   #9
fatman
Member
 
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks for the replies, all.

I am using, C, so I can't use overloading or cout.

The function completes, I even have a printCell function later in the main program that executes fine with the desired contents. Just when the program completes, the last thing it does is seg fault.

I was a little concerned about assignmetnt of entire structs myself, but it appears to be working elsewhere in the program, and it does what its supossed to here. As a last resort I could break the assignmnet down - but its large and compound and would require several iterators.

I guess my latest hypothesis would be that when I delete the cell from the game structure, I decrement the numCells data member to tell all future functions that the exact number of cells in the array. I never access beyond numCells. However I never explictly free the memory that held the last cell in the array before the deletion. Is this suffcient memory leak to cause a seg fault?
 
Old 03-27-2003, 11:49 AM   #10
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Assigning structs directly to one another works in general in C, except when pointers are involved. For example:

Code:
struct {
  int x;
  int y;
} A, B;

A.x=5;
A.y=10;
B=A;
This will work fine, since C will just create a member-by-member copy of one struct to another. However, if you have:

Code:
struct {
  int x;
  int y;
  char name[20];
} A, B;
and try to do B=A, it will not work, because of the character array inside (which is actually a pointer to someplace in memory that has 20 bytes set aside for the 'name' variable.) In this case, you would need to write your own function to copy one struct to another.

An easy way to check is to ask yourself whether it would work to just use regular assignment for each member of the struct. Clearly, saying:

Code:
int x;
int y;
x = y;
is fine, but:

Code:
char a[20];
char b[20];
a = b;
is not. You would need to use strcpy or something like that.
 
Old 03-27-2003, 12:32 PM   #11
fatman
Member
 
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks wapcaplet

I went through and created an assignCell function, and my seg fault is gone.

There were quite a few pointers involved in my struct hiearchy.

You spend a few years in school writing C++, and you tell everybody you can write C as well. I'm learning it ain't quite that easy. (I miss the C++ string library and the '<<' operaror the most )
 
Old 03-27-2003, 04:14 PM   #12
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
Heh.. thanks a lot wapcaplet. I didn't know you could do that. I figured since in C++ you can't copy class to class (ie. A = B) you couldn't copy struct to strcut.

Hmmm.. maybe you can copy classes now... or always could... I need more practice
 
Old 03-27-2003, 07:29 PM   #13
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Quote:
Originally posted by fatman
You spend a few years in school writing C++, and you tell everybody you can write C as well. I'm learning it ain't quite that easy. (I miss the C++ string library and the '<<' operaror the most )
Whew, don't I know it! They've focused almost exclusively on C++ in my courses, and they kind of imply that "if you know C++, you know C" but it's just not true. I had to force myself to write a few of my assignments in C, just to get familiar with the differences in doing things. They are definitely not the same - it's a whole batch of different functions and concepts to get used to.

martine: as for copying classes, yes, you can do that too. A class is just a struct that can contain member functions along with member data. If you do class assignment, the compiler creates instructions for a member-by-member copy of each part of the class. But again, if you have complex data structures (like arrays) inside your class, you'll run into trouble. But that's where C++'s operator overloading becomes really useful - you can actually write your own "=" function. (you can do a lot of other operators too - look up operator overloading for more info). Op overloading is easily my favorite part of C++. If not for that, I would probably prefer strict C instead!
 
Old 03-27-2003, 08:54 PM   #14
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
AHH... My Brain Hurts!!!

Gosh Darn it.... I need more experience.. Believe it or not I'm supposed to graduate in June with a degree in Computer Engineering.

Although... most of my homework/projects involve assembly or VHDL so that'll be my excuse for my C++/C ignorance.

Thanks again wapcaplet. Back to the books.
 
Old 04-01-2003, 09:52 PM   #15
jw_griffith
LQ Newbie
 
Registered: Mar 2003
Location: Westminster, CO
Distribution: SuSE 8.1
Posts: 15

Rep: Reputation: 0
When you add (or subtract) code and your error goes away you did not fix the problem. You are just no longer referencing a something sensitive to the system.

Liberal printf statements are a good subsitute for cout in C. You are probably using a pointer that has not been initialized.
 
  


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
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 05:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 10:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 12:13 PM
Segmentation fault tejas15_10 Programming 9 06-20-2005 10:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 06:45 AM


All times are GMT -5. The time now is 12:55 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration