Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have been working on microcontrollers . Recently working on a stack which requires indepth knowledge of malloc, calloc , etc and mainly memcopy , memmove .

Have you guys got any tutorial links? I have searched in google it came up with a few good links but those links give only a brief details about memcpy etc .
Posted

If you need to use such library functions then you just need a good C programming book and a (good) reference to the function themselves (for instance malloc at cplusplus.com[^]).
On the other hand if you want to know about implementation details (that is function internals) the you have to follow a different path. A starting point might be this page[^]).

BTW do you really need malloc on a microcontroller?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Sep-12 17:24pm    
Right. A 5.
--SA
steven8Gerrard 19-Sep-12 3:36am    
@Cpallini
Yes . Its used in developing STack.
CPallini 19-Sep-12 3:52am    
Strange: heap for implementing stack. :-)
steven8Gerrard 26-Sep-12 14:05pm    
I'm not talking about stack segment . I was talking about ZIGBEE stack :)
CPallini 26-Sep-12 17:11pm    
You should have mentioned that, then. :-)
I learned all that from books, many years ago...

malloc - allocates a block of memory of a requested size.
calloc - allocates multiple contiguous blocks of memory of a requested size.
alloc - alternate name for malloc
realloc - expands or contracts the size of a memory block. If the block cannot be expanded in place, it will copy your data to a new block and return a pointer to that block. If it returns a new location then you must not resuse the old pointer or free it. realloc manages that for you.
memcpy - copies a block of data from one location to another.
memmove - copies a block of data from one location to another. If the regions overlap this function insures that the copy order is correct to prevent data loss.
free - returns a block of memory allocated by malloc, calloc or alloc. This prevents memory leaks.


C++
void * pMemory = malloc(64); // Returns a block of memory of at least 64 bytes in size.
void * pMemory = calloc(4, sizeof(MyStruct)); // returns a block that is large enough to contain 4 instances of MyStruct. You can cast this to an array pointer.
void * pNewMemory = realloc(pOldPointer, 128); // Attempts to resize the memory block for pOldPointer in place. If it cannot do this, it will allocate a new block that is 128 bytes in size, and copy up to 128 bytes from pOldPointer to pNewPointer. It will then free pOldPointer.
memcpy(pNewLoc, pOldLoc, 64); // Copies 64 bytes from pOldloc to pNewLoc.
memmove(pNewLoc, pOldLoc, 64); // Just like memcpy, but insures that the copy direction is correct, if pNewLoc and pOldLoc are less than 64 bytes apart. Useful for shifting text around...


http://msdn.microsoft.com/en-us/library/634ca0c2%28v=vs.71%29.aspx[^]

Source code for these functions should be in your Visual Studio install folders.
 
Share this answer
 
v2
Comments
pasztorpisti 18-Sep-12 16:54pm    
+5, And would add some notes: realloc() is a malloc(), realloc() and free() all in one. If you pass in a NULL pointer then it behaves like malloc(). If you pass in a zero size then it behaves like free(). Its usually much easier to use just realloc() without the other functions and playing around with a pointer and a size parameter. Beware!!! If you use realloc() instead of 3 other functions then read the documentation carefully because the resize to zero might not work like free() on every plaftform. The only exception I found so far is ios/freebsd where a resize to zero doesn't free a block, it allocates a zero sized block instead: https://developer.apple.com/Library/ios/#documentation/System/Conceptual/ManPages_iPhoneOS/man3/realloc.3.html
http://www.unix.com/man-page/FreeBSD/3/realloc/
Even on freebsd the classic realloc behavior can be achieved by turning on the V flag.
Sergey Alexandrovich Kryukov 18-Sep-12 17:23pm    
I would up-vote it if it was posted as a solution.
--SA
pasztorpisti 18-Sep-12 18:05pm    
Thank you! :-)
Sergey Alexandrovich Kryukov 18-Sep-12 17:22pm    
5ed for putting it all together, and just one reasonable link to cover things.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900