LinuxQuestions.org
Visit Jeremy's Blog.
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-30-2005, 04:42 AM   #1
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
realloc and segfaulting problem


ok,
Code:
void tree_add_child (parent, child)
	tree *parent;
	tree *child;
{

		int i;

		parent->number_of_children++;

		// segfault in the line below
		parent->children = (tree **) realloc (parent->children, sizeof (tree * [parent->number_of_children]);
		if (parent->children == NULL) exit (-1);

		parent->children[parent->number_of_children] = child;

}
ok, the function is simple, each struct "tree" has a list of children, its a pointer to a array of pointers to other struct "tree"s, this should increase the number of children, then use realloc to make a extra space, then insert the pointer at the end.

Code:
 typedef struct _tree {
   int number_of_children;
   _tree **children;
} tree;
the main function inits multiple tree struct, with the children set to NULL, and number to 0. Then this function is called to add children to 1 tree struct, it works fine for the first 3, but on the forth it segfaults at the realloc line, i been trying for days but cant seem to find out why, or make it go away. any ideas on why its doing this?, thanks.
 
Old 05-30-2005, 06:38 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're incrementing parent->number_of_children and then (skip a few lines) you're using parent->number_of_children as the index to set the child. So in essence you're completely skipping parent->children[0]. The first child is set to parent->children[1]. The problem here is that you've only reserved room for 1 child in your realloc() line so parent->children[1] on the first added child is out of bounds.

To fix this, don't increment parent->number_of_children until after using the original value as the index for added child. And in the realloc() line, allocate sizeof(tree *) * (parent->number_of_children+1) bytes.
 
Old 05-30-2005, 08:46 AM   #3
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Original Poster
Rep: Reputation: 51
i see,
thanks itsme86, i changed the needed parts and all is well.
 
  


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
realloc, does it copy? f0rmula Programming 4 03-22-2005 02:55 PM
Realloc trouble in C KneeLess Programming 5 11-01-2004 04:16 PM
slackware 9.0 everything segfaulting ranger12002 Slackware 6 10-31-2003 05:47 PM
RPM segfaulting wapcaplet Linux - Software 10 10-22-2003 03:45 PM
q about realloc() devoyage Programming 3 09-01-2003 12:50 AM

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

All times are GMT -5. The time now is 03:33 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