-4

when I run this code I get a segmentation fault and I understand why I get it it is because I did not use the new initializer to allocate space for it. How do I do what I am trying to do? you should be able to see what I am doing by looking at my code, but in case you can't tell. I am trying to declare a pointer to a specific address and then pass a value to that address. here is my code:

#include <iostream>
using namespace std;

int main(){


    double *ptr;
    ptr= (double*) 0x7fff07814c28;
    *ptr= 77.9;
    cout<< "pointer value \n";
    cout<< ptr;
    cout<< "\n";
    cout<< "value pointed to by pointer \n";
    cout<< *ptr;
    cout<< "\n";

    }
Brian Kreidberg
  • 179
  • 1
  • 13
  • 1
    Pointers are orthogonal to dynamic memory. – Quimby Jun 11 '19 at 18:42
  • Where did you `0x7fff07814c28;` get from actually? – πάντα ῥεῖ Jun 11 '19 at 18:43
  • 1
    Brian, do not read any of those **tutorials**. Read a good C++ book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – SergeyA Jun 11 '19 at 18:44
  • Instead of "How do I do what I am trying to do?", you should ask "Why do I want to that?" – Tony J Jun 11 '19 at 18:47
  • 1
    Addresses in any virtual-address OS are process-specific. An address in one process (or the kernel) is unrelated to the same address in a different process. – stark Jun 11 '19 at 18:59
  • This one is hot [Tutorial](http://www.cplusplus.com/doc/tutorial/pointers/) – lsalamon Jun 11 '19 at 19:06
  • @AlexanderZhang: You can allocate memory at a specific address. In many embedded systems, the `operator new` is overloaded to allocate from a specific address. Also, pointers are assigned to the addresses of hardware devices in order to read and write from the hardware registers. – Thomas Matthews Jun 11 '19 at 22:40
  • @ThomasMatthews Well then, I guess my comment should have been "You can't, at least not in standard C++". Thanks for letting me know. – eesiraed Jun 12 '19 at 04:26
  • You can't allocate memory at a specific address in standard C++. We would need more information about what you're trying to do here in order to give you an answer. – eesiraed Jun 12 '19 at 04:31

3 Answers3

3

how to declair a pointer ....

You can declare a pointer (to double) like this:

double* ptr;

... at a memory location ...

You can initialise the pointer with a memory location:

double* ptr = some_address;

Or assign it after declaration:

ptr = some_another_address;

and a initilize it with a value

There are a few ways to acquire storage for objects:

  • You can acquire static, thread static and local storage by defining variables. As an example, the storage for the pointer variable ptr was allocated like this - not to be confused with the storage that the pointer points to.
  • You can acquire dynamic storage using a(n allocating) new expression (or the std::malloc family of functions from the C standard library).

Neither of these options let you specify the memory address where the storage should be placed. There is no way in standard C++ to request storage for a variable from an arbitrary address.


when I run this code I get a segmentation fault

The reason from perspective of C++: You indirect through a pointer that doesn't point to an object. The behaviour of your program is undefined.

The reason from perspective of your operating system: The process attempted to write into a virtual address that was not mapped, or was marked protected or read only, and so the operating system raised a signal and terminated the process.


Now, if and only if the C++ implementation that you use gives you a guarantee that some arbitrary memory location may be used for storage, then you can use placement-new expression to create an object in that memory location. An example of such situation is mmap call on a POSIX system. Here is an example how to create an object in such storage:

// let there be storage at some memory address
// let the amount of storage, and alignment of the address be sufficient for T
char* storage = some_special_address;

// create an object into the storage
T* tptr = new(storage) T;

// after you're done using the object, destroy it:
tptr->~T();
// after destruction, the storage can be released, if needed and if possible

How do I do what I am trying to do?

Run your program on a system that doesn't use virtual memory (i.e. on a system that does not have an operating system). Then consult the manual of that system for what memory addresses you can use. Then see the previous example about how to create objects in the storage that you control. Make sure that the address satisfies the alignment requirement of the object that you create.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Accessing specific address in the memory space depends on the platform and the operating system.

On many desktop systems (e.g. Windows & Linux), the OS grants your program permission to access a portion of memory. Any access beyond those limits will cause a segmentation fault or other error. You'll have to have kernel permission to access specific areas in memory provided that there is memory in that address range.

On embedded systems, pointers are assigned to specific addresses, to access memory and hardware devices. Also depends on if the OS (if there is one) grants you privilege to access those devices. Also, be prepared for undefined behavior when you access undefined addresses or address that have nothing associated with them. (On many embedded systems, the addressing space is decoded for various purposes, such as USB controller. If the address is not decode, undefined behavior will rear its ugly head.)

Before you access a random location, verify that there is something there and that you have permission to access it.

Search the internet for "Operating system virtual memory".

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0
 ptr= (double*) 0x7fff07814c28;

This has no meaning. In modern OSes, memory operation is not real mode so you cannot directly address it.

Memory is not a C array (e.g. if you have 4GB, you can't do unsigned long m[0] to address the first byte).

Memory is managed by the CPU in various modes (protected, long are the standard) and therefore, you cannot know the "exact" array location.

In short, use new[], or even better, STL.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • that line is actually working it's this line: *ptr= 77.9; that is not working – Brian Kreidberg Jun 11 '19 at 18:44
  • 1
    Your first sentence is not correct. It does have meaning, and although `ptr` is not safely derived pointer at this point, there are lot's of usage to those, especially embedded systems. Overall, I think this answer is oversimplified and is unlikely to be of help to the OP. The last advice to use `new[]` seems to be flat out wrong, and mentioning of STL is completely unwarranted. – SergeyA Jun 11 '19 at 18:45
  • I was referring to the nonsense code, not to the code that actually crashes :) – Michael Chourdakis Jun 11 '19 at 18:45
  • what I am saying is if I comment out the lines that have to do with the value pointed to by the pointer. I can initialize the pointer to that address I just can't change the value at it – Brian Kreidberg Jun 11 '19 at 18:48
  • Is there a way to do what I want to do @SergeyA – Brian Kreidberg Jun 11 '19 at 18:53
  • @MichaelChourdakis – Brian Kreidberg Jun 11 '19 at 18:53
  • 1
    @BrianKreidberg But **what** do you want to do? – SergeyA Jun 11 '19 at 18:56
  • @BrianKreidberg you can make a pointer point to an arbitrary address, but there is no value at that adress that you could change. What is the actual problem you want to solve? – 463035818_is_not_an_ai Jun 11 '19 at 19:12
  • the reason I want to do that is I want to communicate with an ui i built in qt for a robot and send it values for motors to displace and the values will just be updated in a specific address in memory – Brian Kreidberg Jun 11 '19 at 19:36
  • @formerlyknownas_463035818 – Brian Kreidberg Jun 11 '19 at 19:59
  • @BrianKreidberg afaik you need shared memory to do that. One process cannot simply write into memory that belongs to a different process. Suggested reading: [what is the xy-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). better ask for the actual problem instead of something that would be a solution if it was possible, but in fact is impossible ;) – 463035818_is_not_an_ai Jun 11 '19 at 20:02
  • @BrianKreidberg I would suggest to open a new question and ask for what you actually want to do. The quesiton here has little to do with sending values to a gui built with qt – 463035818_is_not_an_ai Jun 11 '19 at 20:03
  • @BrianKreidberg You are right that setting an arbitrary address to a pointer is not necessarily illegal. But then there isn't much you actually can do with that pointer. What is illegal is trying to use that pointer because it doesn't actually point to a `double` object. Even *if* you know for a fact that the address you specified is a hardware address (the OS has no memory virtualization) and you know for a fact that the address contains a `double` and you know for a fact that your process has access to it, the compiler doesn't know that. – François Andrieux Jun 11 '19 at 20:29
  • @BrianKreidberg ... So assuming the compiler translates your code naively to assembly and the CPU does exactly what you wrote step by step, it may seem like your code is fine. But that assumption is incorrect. Compilers can alter your code in any way it wants to improve it's performance *as long as it doesn't change the observable behavior*. But since (as far as the language is concerned) your pointer is garbage and dereferencing it is undefined behavior, it can do pretty much what it wants and the assumption made at the top of this comment doesn't hold. – François Andrieux Jun 11 '19 at 20:31