View previous topic :: View next topic |
Author |
Message |
pm Guest
|
malloc() and free() |
Posted: Mon Dec 29, 2003 12:04 pm |
|
|
WHen using repeated calls to malloc() and free() the code will hang up.
example:
int *ptr;
main()
{
ptr=malloc(10);
}
void subroutine()
{
if(ptr) free(ptr);
ptr=malloc(10);
}
after calling subroutine once or twice it hangs. Any ideas why? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 29, 2003 1:42 pm |
|
|
Can you give the version of compiler that you're using,
and also the PIC ? |
|
![](templates/subSilver/images/spacer.gif) |
pm Guest
|
response to free() and malloc() |
Posted: Mon Dec 29, 2003 3:44 pm |
|
|
im using a pic16f877a and the compiler is pcm version 3.169
thanks |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 29, 2003 4:11 pm |
|
|
The program that you posted doesn't do what you
explain in your text. ie., it doesn't call subroutine().
Can you copy and paste a demo program from your
MPLAB project into a post here, so I can see the exact
program that fails ? (ie., a short demo program that
shows the problem). Then I can test it with PCM
vs. 3.169 as well as the latest version. |
|
![](templates/subSilver/images/spacer.gif) |
pm Guest
|
|
Posted: Wed Dec 31, 2003 1:02 pm |
|
|
I re-worked the code to overcome this problem. I only allocate the space once and this seems to work. I was trying to use the same pointer to point to different strings based on certain conditions. I still do the same thing but include the length of the strings in the parameter list passed to the subsequent function. But now my curiosity is piqued. Why would it fail? The code sure looks valid. I'd rather not post any code fragments at this time. What is your opinion of doing repeated calls to malloc() and free() i.e. using the same pointer over and over. I have done this before in C, but never on a microcontroller. Never had a problem in the past.
Thanks for your help
pat |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 31, 2003 1:32 pm |
|
|
I can't work on the problem accurately without a demo that
shows the problem. Someone else will have to help you. |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
Malloc() |
Posted: Wed Dec 31, 2003 2:09 pm |
|
|
Besides, if it is that secret I would be afraid that Tom Ridge would show up at my door after I read it... ![Laughing](images/smiles/icon_lol.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|