LinuxQuestions.org
Review your favorite Linux distribution.
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 08-04-2012, 10:01 AM   #1
#siya
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
segmentation fault while using malloc to populate a structure


struct cell_info
{
int row;
int col;
int adj_cellcount;
int clicks_to_exp_by_enemy;
int clicks_to_exp_by_me;
cell cell_to_move;
};


i am making a llist of these structure
a segmentation fault error is received on line
struct cell_info *newenemycell=(struct cell_info *)malloc(sizeof(structcell_info));


when i run this code in college no such error comes

pls suggest something
 
Old 08-04-2012, 10:05 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Reported to the moderators for moving to programming forums.
Also please enclose code segments in [ code ] tags if ease of reading.
 
Old 08-04-2012, 01:09 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,632
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
If you are obtaining malloc(sizeof(struct cellInfo)) then you should not be encountering a segfault <em>unless</em> you have previously munged the stack by such sins as a "double free." We need to see a larger excerpt of your code; and please, use a code-tag.
 
Old 08-04-2012, 01:31 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by #siya View Post
a segmentation fault error is received on line
struct cell_info *newenemycell=(struct cell_info *)malloc(sizeof(structcell_info));
Some error your program made earlier (such as writing past the end of a previous allocation) causes the failure there.

seg faults don't always occur at the point of the actual bug. Sometimes a bug has no immediate symptom and leads to a seg fault elsewhere.
 
Old 08-05-2012, 03:16 AM   #5
#siya
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
seg fault in this line does not occur when i run the code on college's pc..
when i comment this line then seg fault occurs in the line where malloc is used next..
 
Old 08-05-2012, 05:18 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
That it does not occur on somebody else's PC does not mean anything. That's a nasty part of segfaults; they do not necessarily show and also not where you think they happened.

Comment out the line that you posted, put some other statements there and it more than likely will crash again.

gdb is your friend and else use printf's before and after the malloc statement.

As others indicated, give us a larger part of your code.
 
Old 08-05-2012, 06:27 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Some earlier part of your program overwrote data malloc uses to keep track of which memory chunks are free.

Quote:
Originally Posted by #siya View Post
seg fault in this line does not occur when i run the code on college's pc..
Typically that means in the college's pc the same bug overwrote something that doesn't have an symptom within the execution of this simple program. The lack of a symptom doesn't imply lack of a bug.

A different version of malloc might reuse freed memory in a different pattern, so it never gets around to hitting the corrupted area within the rest of the execution of the program. Or it might store its control info a little differently relative to the allocated data in a way that delays or avoids the symptom of your bug.

But is your Linux 32 bit or 64 bit and same question for the college's pc?
If you recompiled the code from 32 bit to 64 bit, then the bug might be a portability bug that would never have any symptom in 32 bit. In 32 bit, pointers, longs, and size_t all have the same size as int. One of the most common portability bugs is code that assumes (intentionally or accidentally) that one of those other types is the same size as int.

You still need to find the section of code that overwrites memory it shouldn't. But having a guess at the nature of the problem can change the way you look for it.

An expert would first use gdb to examine the failure inside malloc to see exactly which location had been overwritten by the earlier bug, then restart with gdb and a data breakpoint to see exactly where that location is written. But that approach is too far beyond beginner level to suggest. Instead, I'll assume your whole program is short enough you could post it so someone would easily spot the bug, or you could examine it for the expected kind of bug.

Quote:
when i comment this line then seg fault occurs in the line where malloc is used next..
That tells us very little new information. It hints (but doesn't prove) that the original bug is stable (always clobbers the same thing). We already could assume some bug had clobbered data used by malloc, so a subsequent use of malloc would trigger the symptom.

Many memory clobber bugs are unstable: An unrelated change elsewhere in the program tends to change the use or location of the memory that the bug clobbers, leading to lots of false conclusions about what "fixes" or "causes" the bug, when those unrelated changes really just hide or reveal the symptom.

But the most common memory clobber bugs, which write past the end of an allocated chunk of memory, tend to be stable. They clobber memory used by malloc, not by random accident but systematically, because memory used by malloc to keep track of used and free chunks consists of little control blocks right before and after each used chunk.

Last edited by johnsfine; 08-05-2012 at 06:30 AM.
 
Old 08-05-2012, 07:21 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-06-2012, 01:06 AM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by Tinkster View Post
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
I think you forgot to actually move it

To the original poster: Most likely there was memory corruption somewhat earlier in the code.
I think we will need to see some more code before we can make a clear answer.
 
Old 08-06-2012, 03:45 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
right you were ... rectified.
 
Old 08-06-2012, 04:07 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by johnsfine View Post
Some error your program made earlier (such as writing past the end of a previous allocation) causes the failure there.

seg faults don't always occur at the point of the actual bug. Sometimes a bug has no immediate symptom and leads to a seg fault elsewhere.
This

In fact, in my experience, seg faults usually don't occur at the same place as the bug that caused them. Sometimes you can overrun an array without issue, then hundreds of lines later the code will seg fault when you go to print some random variable that has nothing to do with the bug.

-fbounds-check will usually help in catching these array overrun bugs.

Last edited by suicidaleggroll; 08-06-2012 at 04:09 PM.
 
  


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
Structure malloc problem instinct46 Programming 5 09-07-2011 01:40 PM
Segmentation fault when the recursing through a large file structure vbnandu86 Programming 1 07-30-2010 03:53 AM
[SOLVED] C - How to malloc for a structure golmschenk Programming 5 03-08-2010 12:16 PM
malloc segmentation fault C for simple program assyrian1 Programming 2 07-29-2009 08:48 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM

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

All times are GMT -5. The time now is 11:10 PM.

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
Open Source Consulting | Domain Registration