How to name a memory location (C++)? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is a question regarding pointers in C++.
The diagrams that follow use the following representation:
Let's say we have int* k = 1778. This is represented in the left diagram below. How do I name the memory location in k? This is marked by the question mark in the diagram on the right.
I understand that if I write int x = *k, a new memory location will get assigned as shown on the right below, so this does not accomplish the task.

Variable names are not a property of the memory location. Those names are used by the program for accessing relevant memory locations. So that you cannot name a memory location in the physical memory (RAM).
If you need to access the dereference(*) of the pointer k through a new name, please create a reference variable and assign the dereference of pointer k to created reference variable.
Example:
int& ref_k = *k;

Related

How to implement an array without initialization value in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
The uninitialized means that the array should firstly be created, but without any value initialized. It will gots values if and only if we do operations on the elements.
For example, given an array A[100]. I want to create another array B[100] with each element in it larger than A by 1, i.e. B[i] = A[i] + 1. But I don't want to initialize all the elements at the beginning. How can I implement such a structure/object?
To be more specific, the operation I want to avoid is the initialization of the elements in B because I need to change the values later. Instead, I want to assign the values directly by some functions/operations.
I think a struct is needed here but I am not familiar with the concepts of the details about how to implement the value assignment once each element got some operations on it.

Is there ever a need to allocate a single int dynamically? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
One example of dynamic allocation on cppreference.com is:
int* p1 = new int;
Is there ever a need to allocate a single int dynamically?
When all integral values are valid in your application's logic, using an int* adds the additional NULL value. This is useful when, for example, you are dealing with a NULLABLE database column of type int.
Additionally, on 16-bit systems, int operations weren't atomic, so updating an int value while reading it from another thread isn't a threadsafe operation, but sharing an int* is.
(Yeah, I'm stretching here, but I have used both in the past)

How to assign a specific memory address to a pointer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose I have a memory address '0x8f820dae' on my computer
I want to store an integer value '2' in this specific memory location, how can I do that?
Assuming this is a valid writable on your process memory address and by "an integer" you meant an int:
*reinterpret_cast<int*>(0x8f820dae) = 2;
Note that this will write the value 2 (0x00000002) to the address 0x8f820dae (considering x86). Change the <int> type-parameter if you want to write a different numbers of bytes (i.e. sizeof(int) bytes will be written at the memory address).
Typically like this:
*(int *)0x8f820dae = 2;
(Or use a C++-style cast if you prefer.)

Reference pointer in class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Well, I need an attribute in my class is a pointer reference & and start to null, because it pointed to the address of an image that has not yet been charged. It is possible to do this?
If not possible, is as follows:
I'm creating a game with Allegro 5 and C ++, and in part to carry the images of tiles (tileset) I carry every memory and for each object of the game I pass him the reference pointer (memory address) of your respective image.
I'm doing the right thing? Got any better way?
Don't use a reference, use a pointer.
Image* image = nullptr;
image = loadImage(...);
image->getWidth();
A reference must reference an existing object, if the variable should have a "non loaded state" then you are out of luck. You must use a pointer or let it reference a non valid object created just as a placeholder.

Reference to a variable of different type [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to create a reference of an another data-type referring to a variable of a different datatype?
LPWSTR Buffer = new WCHAR[BUFFER_LEN];
LPBYTE& rfBuffer = (LPBYTE&) Buffer;
//Compiles but rfBuffer is BAD
Yes. The syntax T x = (T&)y tells the compiler to treat the memory taken by the variable y as if a T was located there. But it's just a gross abuse of the language.
Refrences are implicit pointors. They hold the address of the original object but behave syntactically as the object itself. According to this MSDN article: Any object whose address can be converted to a given pointer type can also be converted to the analogous reference type