| | |
problem with realloc
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 11
Reputation:
Solved Threads: 0
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);
}

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.
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.
>PLEASE help pps!!
Here's a hint: realloc only works on memory that was previously returned by malloc, calloc, or realloc.
I'm here to prove you wrong.
![]() |
Similar Threads
- How to use timer in C? (C)
- Just a quick question... (C++)
- pointer passing problem (C)
- realloc malloc and Heap problem. (C++)
- New/delete or malloc/realloc/free (C++)
- Help - Problem with array (C)
Other Threads in the C Forum
- Previous Thread: Some Huffman problems
- Next Thread: making and changing to a directory in C
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






