Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C C# Visual-Studio
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 18-Sep-12 6:27am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 18-Sep-12 17:24pm
   
Right. A 5.
--SA
steven8Gerrard at 19-Sep-12 3:36am
   
@Cpallini
Yes . Its used in developing STack.
CPallini at 19-Sep-12 3:52am
   
Strange: heap for implementing stack. :-)
steven8Gerrard at 26-Sep-12 14:05pm
   
I'm not talking about stack segment . I was talking about ZIGBEE stack :)
CPallini at 26-Sep-12 17:11pm
   
You should have mentioned that, then. :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 

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.
  Permalink  
v2
Comments
pasztorpisti at 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 at 18-Sep-12 17:23pm
   
I would up-vote it if it was posted as a solution.
--SA
pasztorpisti at 18-Sep-12 18:05pm
   
Thank you! :-)
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 528
1 OriginalGriff 459
2 ChintanShukla 305
3 Richard Deeming 275
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,858
1 OriginalGriff 7,451
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100