0

Is it nearly possible for one to assign a memory address to a char* and if so, then how? Assuming we have an existing address .. just like that:

char *string_received = (char*)0x30123f00; 

Resulting in an allocated string, obtained directly from memory.

9
  • 2
    Where do you get that memory address from? If you know that address represents a valid memory address then yes. Commented Nov 27, 2014 at 22:07
  • 1
    Your address should read 0x30123f00 btw ;) Commented Nov 27, 2014 at 22:08
  • Okay, it was figurative though. And yes, in real life, this is a real existing address. Commented Nov 27, 2014 at 22:09
  • have a chance to have a segfault if you just input some random address. are you running under real mode or what, so that you want to do that? Commented Nov 27, 2014 at 22:19
  • 1
    No you can't with two programs (in general)! Each running program/process has its own memory space and address A in process P1 has nothing to do with address A in process P2, even if P1 and P2 runs the same program. This is called virtual memory, it is intended for memory protection and security. In some very particular case you can, but this is very specific (memory mapped ports or things like that); I am pretty sure that this is not what you thought about. Commented Nov 28, 2014 at 8:51

1 Answer 1

1

Yes, it is possible. However, the string is not "allocated", it just exists. You have to be sure there is something valid at that memory location before using the pointer.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I am sure. Do you mind giving an example of working mechanism? I tried using memmove, but it doesn't print anything.
I'll also add that if you're using this to access memory mapped IO ports, you should add volatile to your pointer so they compiler doesn't re-order or optimise your memory accesses.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.