views:

197

answers:

5
+1  Q: 

MemSet & MemCopy

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:

// EDIT: Declared space for testInt int* testInt = new int;

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

// EDIT: Should have used std::fill and std::copy
memset(head_ptr,23,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?

Thank you so much everyone for your help!! Problem solved :-)

+5  A: 

Answer to original question

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

No it doesn't. This sets the first sizeof(int) bytes of head_ptr to 12345, which will overflow (unless you are using an architecture where a byte is more than 8 bits).

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

What is testInt? An int*? An int? In the latter case use &testInt.

Also it appears from your tags that you are using C++ rather than C. But your code is really just C, you should really use the safer C++ functions and features:

  • memset -> std::fill
  • memcpy -> std::copy
  • malloc -> new
  • printf -> cout or (better) Boost::Format

Answer to your edit

int* testInt; is a pointer to an integer variable but it's not initialized: it will point to a random memory area (we can consider it "random" for all intent and purposes even if it isn't).

memcpy will then try to write to this random memory area to which most likely you don't have access to, and therefore this results in a segmentation fault (that means "you can't access this memory area").

Andreas Bonini
+1 for catching memset issue I missed.
Billy ONeal
Thank you Andreas, I am much obliged.
pws5068
+1  A: 

Without being able to see what testInt is I can't be sure, but my psychic debugging powers are indicating that you need to take the address of testInt rather than testInt itself as the argument to memcpy.

EDIT: Upon seeing what you've posted now, you need to allocate memory for testInt before using it.

Billy ONeal
Your psychic debugging powers were correct, +1, Thanks!
pws5068
+1  A: 

If testInt is just an "int", could it be since you're passing it by value, and it's not getting changed?

Chris Cooper
You were correct, +1, thank you!
pws5068
+2  A: 

You've never initialized testInt, so your memcpy call is writing to who knows where.

Try this:

int *testInt = malloc(sizeof(int));

Laurence Gonsalves
I edited my original post, test int is an int*
pws5068
+2  A: 

Others commented on improper usage of memset(3) and memcpy(3), so I'll reply to the allocator issue.

If you are really in the business of building custom memory allocator in C++ - take a look at Chapter 4 in Alexandrescu's Modern C++ Design. It walks you through implementing small object allocator in detail. The code is available as part of the Loki Library.

Man, somebody likes unicorns ... :)

Nikolai N Fetissov
Who doesn't love unicorns??? +1
pws5068
They are so tasty ...
Nikolai N Fetissov