CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

malloc() and free()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pm
Guest







malloc() and free()
PostPosted: Mon Dec 29, 2003 12:04 pm     Reply with quote

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?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 29, 2003 1:42 pm     Reply with quote

Can you give the version of compiler that you're using,
and also the PIC ?
pm
Guest







response to free() and malloc()
PostPosted: Mon Dec 29, 2003 3:44 pm     Reply with quote

im using a pic16f877a and the compiler is pcm version 3.169

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 29, 2003 4:11 pm     Reply with quote

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.
pm
Guest







PostPosted: Wed Dec 31, 2003 1:02 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 31, 2003 1:32 pm     Reply with quote

I can't work on the problem accurately without a demo that
shows the problem. Someone else will have to help you.
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

Malloc()
PostPosted: Wed Dec 31, 2003 2:09 pm     Reply with quote

Besides, if it is that secret I would be afraid that Tom Ridge would show up at my door after I read it... Laughing
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group