943,948 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 513
  • C RSS
Sep 20th, 2009
0

problem with realloc

Expand Post »
realy desperate here..
my prog is getting data about problems (computer/electricity..ect (not real C problems)) from txt file..and every line in file has a different problem..so I made one PROBLEM problem; (object) and in my getProbDetails() function I'm getting the first line..reallocing to add one more object (creating array of problem objects) and then getting next line into the new object and so forth..(it's in a loop)
now I have a stupid problem in the line when I do "realloc" and I can't figure it out,the first loop runs but when it gets to realloc the program stops and I get a pop out window with error PLEASE help pps!!

typedef struct
{
.......
} PROBLEM;
typedef PROBLEM *ProbPtr;
void main()
{
__int probSize=1; //num of broblems
__int *probSizePointer; //pointer to num of broblems
__probSizePointer=&probSize;
__ProbPtr pProblem;
__PROBLEM arrProb; //the first problem object
__pProblem=&arrProb;
__getProbDetails(&pProblem,probSizePointer);
}
int getProbDetails(ProbPtr *pProblem, int *psize)
{
__STRING temp;
__ProbPtr ptr; //temp for new address after realloc
__........
__i=0;
__do
__{
_____fscanf(myfile,"%d",&(*pProblem+i)->probDate.day);
_____.....
/*MY PROG STOPS HERE SO I THINK THE PROBLEM IS IN THE REALLOC*/
_____if((ptr=(ProbPtr)realloc(*pProblem,(*psize+1)*sizeof(PROBLEM)))==NULL)
____{
________printf("Not enough memory ");
________return 0;
_____}
_____*pProblem=ptr;
_____(*psize)++;
_____i++;
__}while (!feof(myfile));
__flag=1; //file was read
__return (flag);
}
Last edited by arcticM; Sep 20th, 2009 at 2:05 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arcticM is offline Offline
19 posts
since Aug 2009
Sep 20th, 2009
2

Re: problem with realloc

Amazing. You took the time to add underscores so that your formatting wasn't lost and didn't bother to look for some kind of forum tag that would preserve it for you?

>PLEASE help pps!!
Here's a hint: realloc only works on memory that was previously returned by malloc, calloc, or realloc.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 20th, 2009
0

Re: problem with realloc

you should read the documents abt realloc before you do it right away
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Sep 20th, 2009
0

Re: problem with realloc

Click to Expand / Collapse  Quote originally posted by Narue ...
Here's a hint: realloc only works on memory that was previously returned by malloc, calloc, or realloc.
Thanks a lot!!! it worked!!! my stupid book said nothing about it I could have hit my head against the wall for another year!!!
THANK YOU!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arcticM is offline Offline
19 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Some Huffman problems
Next Thread in C Forum Timeline: making and changing to a directory in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC