| | |
Memory moving with 2 pointers
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
memmove (dst, src, size)
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,
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?
And how exactly are you doing that...?
Given,
C Syntax (Toggle Plain Text)
#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; }
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.
Last edited by zacs7; May 27th, 2009 at 10:49 pm.
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Question about pointers (C++)
- using pointers (C++)
- fstream Tutorial (C++)
- Pointers (archived tutorial) (C++)
- C Can't Handle Pointers (C)
- Pointers (C++)
- How to "delete" this memory??? (C++)
- Singly-Linked Lists: Ultimate (C++)
- Pointers (C++)
- Problem with pointers (C++)
Other Threads in the C Forum
- Previous Thread: Get the current time(according to your system).
- Next Thread: Binary addition in C
Views: 516 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi





