LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-15-2006, 03:14 PM   #16
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941

Quote:
Originally Posted by George2
When executing the following statement,
--------------------
bool* a = new bool[8];
--------------------
There is a segmentation fault error
If you got this error, George, then I am sure that the root cause of the problem lies elsewhere.

This is what we call a "stack scribble," where the internal data structures used by malloc et al have been .. hosed. Unfortunately, the real source of the problem could have happened anywhere, quite some time ago.

The most common cause, however, is a double-free. You attempt to free() the same region of memory more than once. A "stale pointer." (Less often, an uninitialized pointer-variable leads you to free an area that you had never malloc()ed. "Stack scibbles" can also occur by overwriting the end of a block.)

{P.S. There are many different flavors of allocating and freeing memory... they're all the same for this discussion.}

Here's the best thing to do: after every single free() (or its equivalent), insert a statement to expressly set the affected pointer-variable to NULL:
Code:
 free(fooptr); 
fooptr = NULL;  // <- add this
Furthermore, anytime you create a pointer-variable, explicitly set it to NULL on creation unless you know that it will always get a known value.
Code:
 struct foobar *fooptr = NULL; // add "= NULL"
What this little bit of self-discipline does for you is to enforce the following rule: "At all points throughout this program, any pointer-variable either points to what is known to be a valid structure in memory, or it is known to contain NULL."

free() checks for NULL and does nothing if you attempt to free NULL, but it does not otherwise check that the pointer value makes sense, nor that it points to an area of memory that has not already been freed.

Incidentally... there are implementations and variants of these routines which add "boundary tags" on either side of the allocated memory-blocks. They're a painless and efficient way to detect this nasty problem. They work by slightly increasing the size of each allocation-request to make room for a short "tag" at the beginning and at the end of each block. They hand you a pointer to the space in the middle... which they initialize to known-zeroes. When freeing or resizing the block, they first make sure that the tags still exist (they weren't overwritten), and that they don't indicate an already-freed block. Very powerful, and something that you can actually leave in production-release code without consequence.
 
  


Reply



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
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault alnreddy Linux - Software 1 11-05-2005 08:54 PM
why segmentation fault??? andrewnow Programming 8 03-26-2005 06:50 PM
segmentation fault... perdesiz Linux - Software 1 08-18-2003 01:55 AM
Segmentation Fault live2 Linux - General 6 03-02-2003 08:36 PM

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

All times are GMT -5. The time now is 12:52 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
Open Source Consulting | Domain Registration