Hello, I need some help if anyone has an idea. I'm working a memory management simulator trying to implement a mark & compact algorithm. To do that I need a function that can move/copy a block of memory from address A to address B. I tried using

memmove (dst, src, size)

but what ever I try I keep getting a Segmentation fault.

The simulator(which acts somewhat like a interpreter) asks the OS(Linux) for a block of memory to work with, and within that memory it allocates chunks of memory which can be worked with, as well as giving me a pointer to the usable space within the chunk.
-I know the *dst pointer points to a safe address
-the only way to access the memory location where I need to write is by the *dst pointer
-basicly all I have to work with is the src and dst address, that is the pointers A and B

In short, does anyone have an idea how to move a block of memory from pointer A to pointer B without running into a Segmentation fault?

-or is there maybe some way to tell my OS I want to write whatever I want, where ever I want... any input is welcome, maybe I'm just doing something wrong, thx in advance

Do you have memory allocated to you at "dst"? That is, at least size of "size"?

Given,

#include <string.h>
#include <stdio.h>

int main(void)
{
   char src[32] = "Foobar",
        dst[32];

   printf("%s\n", src);

   memmove(dst, src, sizeof dst);

   printf("%s\n", dst);

   return 0;
}

Sure they're not "pointers", but the idea is there. The name is a bit... off-putting, as memory isn't actually "moved" but rather copied. So if you wanted to move memory, you'd allocate memory of size "src" for "dst" move the memory with memmove(), and free the source memory with free() -- providing it's dynamic memory.

Perhaps "dst" points to read-only memory?

The simulator(which acts somewhat like a interpreter) asks the OS(Linux) for a block of memory to work with, and within that memory it allocates chunks of memory which can be worked with, as well as giving me a pointer to the usable space within the chunk.

And how exactly are you doing that...?

commented: good post. +10

And how exactly are you doing that...?

I found the problem... The simulator implements a hash table which more or less messes up the address of the pointer I get...

Nevertheless, thx a lot. The comments that the dst memory must be allocated lead me to start looking at what were the values of the memory addresses I was getting back and to compare that to the memory map.

I think I just need to reverse hash the addresses and everything "should" work. Thx again for the idea.

commented: Great followup. +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.