MemSet & MemCopy

Posted by pws5068 on Stack Overflow See other posts from Stack Overflow or by pws5068
Published on 2010-04-01T02:37:45Z Indexed on 2010/04/01 2:43 UTC
Read the original article Hit count: 411

Filed under:
|
|

I'm writing a memory allocator, and I need a way to store an integer inside of a chunk of memory. This integer will represent the size of the block so I can navigate to the end given the pointer to the beginning.

Here's my test example:

head_ptr = (char*) malloc(4*1024*1024); // Allocate 4MB

memset(head_ptr,12345,sizeof(int)); // Set Address head_ptr = 12345

memcpy(testInt,head_ptr,sizeof(int)); // Set testInt = head_ptr

printf("testInt = %i",testInt);

This throws a segmentation fault on the second to last line.

Does what I'm trying to do make sense?

If so, what is the correct approach?

© Stack Overflow or respective owner

Related posts about memset

Related posts about c++