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 CCS Technical Support

malloc not working

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







malloc not working
PostPosted: Thu Nov 25, 2004 7:48 pm     Reply with quote

Someone from CCS, I need to know how __DYNAMIC_HEAD works.
Somehow, when I call the malloc function, it keeps looping and never returns anything.

char *malloc(size_t size)
{
node_t *node,*new;
long nsize;
node=__DYNAMIC_HEAD;
while(node!=NULL) // chk until end of memlist
{
if(!bit_test(node->size,pos) && node->size >=size) // node is free and > = req size
{
nsize=node->size;
update_node(node,size+csize);
if(nsize>size) //node > req size, so split and add new node to memlist
{
new=create_node(nsize-size-sizeof(node_t),(long)node+sizeof(node_t)+size);
insert_node_after(node,new);
}//end if
break;
}//end if
node=node->next;
}//end while
if(node==NULL)// reached end without finding an appropriate node
{
//printf("\r\n Not enough memory for allocation");
return NULL;
}
else
return (char *)node+sizeof(node_t); // return pointer to allocated space
}


The condition is the if statement in bold is never true, so the malloc function just keep looping node after node.
What's going on?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Nov 26, 2004 3:58 am     Reply with quote

Quote:
Someone from CCS, I need to know...
This is a user's forum, so basically who you will find here are other users like you. Which doesn't mean that we won't try to help you. Smile

Quote:
The condition is the if statement in bold is never true, so the malloc function just keep looping node after node.
So this means all the nodes are in use, which is possible. Then at the end of the node list it exits the while loop and malloc() returns NULL. You say malloc never returns... That's strange and I can hardly believe it. Are you sure this is really what's happening?

Please post a small test program so we can verify your conclusions.

A few other remarks:
- When posting code, please use the 'Code' button, this makes sure the layout of your code is preserved.
- When reporting problems always report the compiler version and processor type you are using.
Guest








PostPosted: Fri Nov 26, 2004 8:48 am     Reply with quote

I am pretty sure that malloc never returns. I simulated my program using MPLAB, and the malloc function just keeps looping.
What really sucks is that when I write a small test small program, malloc works somehow. But Malloc is unable to allocate 41 bytes in my larger program.
That's why I wanted to know a little bit more about __DYNAMIC_HEAD.
Maybe my larger program has an influence on it.
Guest








PostPosted: Fri Nov 26, 2004 9:58 am     Reply with quote

Here is some code.

The code that calls malloc is:

Code:
void *pvReturn;
pvReturn = malloc(  usWantedSize );


At this point, usWantedSize contains the value 0x29, which is 41 in decimal.

Now, when the malloc function is called:

Code:
char *malloc([b]size_t size[/b])
{
   node_t *node,*new;
   long nsize;
   node=__DYNAMIC_HEAD;
   while(node!=NULL) // chk until end of memlist
   {
      if[u](!bit_test(node->size,pos)
      && node->size >=[b]size[/b]) [/u]// node is free and > = req size
      {
         nsize=node->size;
         update_node(node,size+csize);
         if(nsize>size) //node > req size, so split and add new node to memlist
         {
            new=create_node(nsize-size-sizeof(node_t),(long)node+sizeof(node_t)+size);
            insert_node_after(node,new);
         }//end if
         break;
      }//end if
      node=node->next;
   }//end while
   if(node==NULL)// reached end without finding an appropriate node
   {
      //printf("\r\n Not enough memory for allocation");
      return NULL;
   }
   else
   return (char *)node+sizeof(node_t); // return pointer to allocated space
}


The value of size is 0x0A29 instead of of 0x29. This is why the condition in 'if' statement node->size >=size is never true.
So the function keeps looping.

Can someone tell me why I pass 0x29 to the function, and the function gets the value 0x0A29 ????
Guest








PostPosted: Fri Nov 26, 2004 10:37 am     Reply with quote

I Found the solution to my problem.

I chaged the definition of size_t in <stddef.h>

from #define size_t int

to #define size_t long

my memory allocation is working fine now.
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