Memory moving with 2 pointers

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 2
Reputation: Qisrem is an unknown quantity at this point 
Solved Threads: 0
Qisrem Qisrem is offline Offline
Newbie Poster

Memory moving with 2 pointers

 
0
  #1
May 27th, 2009
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
  1. 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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: zacs7 is an unknown quantity at this point 
Solved Threads: 2
zacs7's Avatar
zacs7 zacs7 is offline Offline
Newbie Poster

Re: Memory moving with 2 pointers

 
1
  #2
May 27th, 2009
Do you have memory allocated to you at "dst"? That is, at least size of "size"?

Given,
  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6. char src[32] = "Foobar",
  7. dst[32];
  8.  
  9. printf("%s\n", src);
  10.  
  11. memmove(dst, src, sizeof dst);
  12.  
  13. printf("%s\n", dst);
  14.  
  15. return 0;
  16. }
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?

Originally Posted by Qisrem
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...?
Last edited by zacs7; May 27th, 2009 at 10:49 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: Qisrem is an unknown quantity at this point 
Solved Threads: 0
Qisrem Qisrem is offline Offline
Newbie Poster

Re: Memory moving with 2 pointers

 
1
  #3
May 28th, 2009
Originally Posted by zacs7 View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 516 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC