How to assign a specific memory address to a pointer? [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 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.)

Related

How much Memory allocation there will be in the pointer array [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 years ago.
Improve this question
In the following statement:
char *myarray[] = {"Amir"};
For pointer myarray[], how many bytes of memory has been allocated?
It depends on the OS Architecture. Because it is an array of char *, It will take size equivalent to one pointer in this case.
For 32-bit addressing, it will take 4 bytes.
For 64-bit addressing, it will take 8 bytes.

Appending bits to increase the size of a char [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 years ago.
Improve this question
Is there any way I can append the size of a particular variable? For instance, if I have:
static_cast<char>(0x0147)
an 8 bit char and I want to increase it to say 16 bits without changing the data type, is that possible?
No.
The size of the object is not only related to its type; the size is defined by the type.
You cannot change one and not the other.
Just initialize a new int16_t from this char if that's what you want.
Or, you could have a vector<char> and add new elements to this collection as needed.
(Shifting has nothing to do with it; that's about transforming data.)

c++ binary value for indexed array [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 6 years ago.
Improve this question
I am extremely new to C++ so I'm probably asking a very trivial question, but if you could help that'd be great!
I have an array[n].
Indexed from 0 to some unknown value.
I need to access the index of the array, the n value but I need to do so in binary. I am intending to do a bit reversal on it.
So, if I have an array of 2048 points how do I represent the 1024 array in binary?
If you want to write a value in binary, you can do so in C++14 with
int my_binary_value = 0b01010101;
If you'd like to test a specific bit of an int, you can do that by masking it, i.e.
bool is_bit_4_set = my_binary_value & 0b00001000;

What is Predecessor of an element in an array? How do I find it? [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
I tried to Google but in vain.
In C++ arrays are stored in contiguous memory.
This means that if you have an element x and you know that it's inside an array and that it's not the first element of the array, then then previous one is
*(&x - 1)
i.e. the element pointed by the address of x after we subtract 1 (note that this works because pointer arithmetic in C++ considers element size, so &x - 1 is not point to the byte before, but to the element before).

How to know all memory an object occupy when the object have some dynamic allocated memory? [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 9 years ago.
Improve this question
For example:
How to know how many memory a std::string object occupy, including the memory allocated in the heap used by std::string?
According to the answers below:
It seems there is no genreral solution, I have to do it by the specific implementation.
The answer depends on the data structured used, and may depend on how it is implemented by your compiler. In the case of std::string, the capacity function returns the number of elements currently allocated in its internal buffer. In this case, as char has size 1, it is effectively the number of bytes dynamically allocated by the string object.
sizeof (std::string) returns the number of bytes contained in just the string class, which is constant for all string objects, but doesn't include the dynamic memory managed by the string object.
Maybe :sizeof(mystring)+mystring.size()*sizeof(char)