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 01-05-2008, 05:26 AM   #1
ruh31
Member
 
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:

Code:
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:
Code:
long SIZE;
long INCREASE;
I assign memory and increase memory with the following functions:
Code:
int NEW(MyStruct2 **STRUCT2) {
  *STRUCT2=(MyStruct2 *)malloc(sizeof(MyStruct2));
  (*STRUCT2)->Structs=(MyStruct *)malloc(SIZE*sizeof(MyStruct));
}

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

int WHATEVER(MyStruct2 **STRUCT2) {
  NEW(STRUCT2);
  DO(STRUCT2,(*STRUCT2)->Structs);
}
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, 10:02 AM   #2
ruh31
Member
 
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!
 
  


Reply

Tags
pointer, realloc, struct



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

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

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