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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
03-26-2003, 05:26 PM
|
#1
|
|
Member
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158
Rep:
|
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?
|
|
|
|
03-26-2003, 06:16 PM
|
#2
|
|
Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018
Rep:
|
Looks like the error is on line 27.
Seriously, though, it may help if you post some of the source code 
|
|
|
|
03-26-2003, 08:08 PM
|
#3
|
|
Member
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158
Original Poster
Rep:
|
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?
|
|
|
|
03-26-2003, 08:23 PM
|
#4
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Tried stepping it with a debugger?
At least that would lead you closer to where
it's happening :)
Cheers,
Tink
|
|
|
|
03-26-2003, 09:07 PM
|
#5
|
|
Guru
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Squeeze, Fedora 14
Posts: 7,268
Rep:
|
electric fence
If you are trying to chase down memory problems you might try "electric fence".
|
|
|
|
03-26-2003, 09:12 PM
|
#6
|
|
Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018
Rep:
|
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.
|
|
|
|
03-26-2003, 10:16 PM
|
#7
|
|
Member
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158
Original Poster
Rep:
|
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--;
}
|
|
|
|
03-27-2003, 02:16 AM
|
#8
|
|
Member
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618
Rep:
|
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 02:18 AM.
|
|
|
|
03-27-2003, 10:19 AM
|
#9
|
|
Member
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158
Original Poster
Rep:
|
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?
|
|
|
|
03-27-2003, 10:49 AM
|
#10
|
|
Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018
Rep:
|
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.
|
|
|
|
03-27-2003, 11:32 AM
|
#11
|
|
Member
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158
Original Poster
Rep:
|
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 )
|
|
|
|
03-27-2003, 03:14 PM
|
#12
|
|
Member
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618
Rep:
|
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 
|
|
|
|
03-27-2003, 06:29 PM
|
#13
|
|
Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018
Rep:
|
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!
|
|
|
|
03-27-2003, 07:54 PM
|
#14
|
|
Member
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618
Rep:
|
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. 
|
|
|
|
04-01-2003, 08:52 PM
|
#15
|
|
LQ Newbie
Registered: Mar 2003
Location: Westminster, CO
Distribution: SuSE 8.1
Posts: 15
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:47 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|