22-11-2004, 09:21 AM
Hi,
I have a few queries with respect to \"dynamic memory allocation\". As per the rule we are not supposed to use functions like \"malloc()\", \"free()\", \"calloc()\" etc. But malloc() is a very common requirement. Most of the embedded system applications use their own application level memory managers so as to make the allocation and de-allocation fast. Do you have any suggestions to get around this problem ( if we can't use malloc, any other way )?
One possible solution I can think of right now it to do a static allocation of a big chunk of memory and redirecting all the dynamic memory requirements to that chunk ( basically a kind of memory manager ). But here also we are stuck with one problem. If we write an application level memory manager, it has to cater to all data types. This will require conversion from any data type to \"void*\" and from \"void*\" to any data type ( say \"char*\" ). But Rule No: 45 of MISRA C ( Typecasting from any type to or from pointers shall not be used ) prohibits me doing that also.
The third problem is with writing portable code. Its quite common to use codes like (X*)0->data1 to get the offset to data1 , where X is a structure ( struct X { }; ) and data1 is a member within that structure. Are we not supposed to use these types of casts too [ (X*)0 ] ???
It would be great if someone could give some possible solutions to these kind of issues.
- Thanks
- Raveendran V.
I have a few queries with respect to \"dynamic memory allocation\". As per the rule we are not supposed to use functions like \"malloc()\", \"free()\", \"calloc()\" etc. But malloc() is a very common requirement. Most of the embedded system applications use their own application level memory managers so as to make the allocation and de-allocation fast. Do you have any suggestions to get around this problem ( if we can't use malloc, any other way )?
One possible solution I can think of right now it to do a static allocation of a big chunk of memory and redirecting all the dynamic memory requirements to that chunk ( basically a kind of memory manager ). But here also we are stuck with one problem. If we write an application level memory manager, it has to cater to all data types. This will require conversion from any data type to \"void*\" and from \"void*\" to any data type ( say \"char*\" ). But Rule No: 45 of MISRA C ( Typecasting from any type to or from pointers shall not be used ) prohibits me doing that also.
The third problem is with writing portable code. Its quite common to use codes like (X*)0->data1 to get the offset to data1 , where X is a structure ( struct X { }; ) and data1 is a member within that structure. Are we not supposed to use these types of casts too [ (X*)0 ] ???
It would be great if someone could give some possible solutions to these kind of issues.
- Thanks
- Raveendran V.
<t>Raveendran Vadakkoot</t>