Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-05-2008, 06:26 AM   #1
Registered: Oct 2004
Location: Germany
Distribution: Ubuntu
Posts: 51

Rep: Reputation: 15
in external function: realloc of array within a struct

Hi everybody!

I am once again having some trouble with re-allocating memory. The basis of the following is this struct:

typedef struct _MyStruct {
  struct _MyStruct *Father;
  struct _MyStruct *Sons[2];
} MyStruct;

typedef struct _MyStruct2 {
  MyStruct *Structs;
} MyStruct2;
So I have one MyStruct2 in which there is an array of MyStructs in which there are pointers to other MyStructs (a hierarchy).

These variables are important:
long SIZE;
I assign memory and increase memory with the following functions:
int NEW(MyStruct2 **STRUCT2) {
  *STRUCT2=(MyStruct2 *)malloc(sizeof(MyStruct2));
  (*STRUCT2)->Structs=(MyStruct *)malloc(SIZE*sizeof(MyStruct));

int ENLARGE(MyStruct2 **STRUCT2) {
  (*STRUCT2)->Structs=(MyStruct *)realloc((*STRUCT2)->Structs,SIZE*sizeof(MyStruct));
My main procedure looks like this:
int DO(MyStruct2 **STRUCT2, MyStruct *STRUCT) {
  // Fill in father and sons
  // increase counter for used STRUCTS
  // if counter large enough:
  // and then recursively:
  for (a=0; a<2; a++) DO(STRUCT2,STRUCT->Sons[d];

int WHATEVER(MyStruct2 **STRUCT2) {
The first function that is called (out of some other code) is WHATEVER.

I know that it may look weird and maybe confusing, but this is the simplest I could do out of my code. I only included the (as I see it) relevant parts for my problem.

As it seems, the program works fine until the second call of ENLARGE. After the second call of ENLARGE, there is a segmentation fault (the pointers do not point to the right structures any more???).

It would be so great if anybody could help me with this problem!

Thanks a lot!
Old 01-05-2008, 11:02 AM   #2
Registered: Oct 2004
Location: Germany
Distribution: Ubuntu
Posts: 51

Original Poster
Rep: Reputation: 15
Anybody has any ideas??? I still have not found out why this is happening and it would be perfect if I finally got it running!

Thanks again!


pointer, realloc, struct

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
array of pointers to struct in c : how? moob8 Programming 6 11-21-2006 12:13 PM
I just want to make sure this is correct, struct pointer array paramter in function. RHLinuxGUY Programming 6 08-20-2006 10:30 PM
C question;how do I put struct in function? ngan_yine Programming 10 02-09-2005 03:54 PM
C: struct as a function parameter zokik Programming 3 12-06-2003 03:52 PM
array of struct ChimpFace9000 Programming 1 06-12-2002 09:14 PM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration