how is char able to store an address? [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 11 months ago.
Improve this question
I'm a beginner and I absolutely can't figure out how some string returns a pointer to its location in memory?
char* str = "okay"
why is it a pointer? After all, we don't even use new

how is char able to store an address
It doesn't, it's a pointer to a char.
how some string returns a pointer to its location
String literals are (generally) constant character arrays stored in a read-only section in memory that's mapped to a section in your executable. More details are OS-specific, but what's important is that string literals decay into a pointer to their 1st character, just like any other array.
After all, we don't even use new
new and malloc() return pointers, sure, but they're not the only source of pointers. As you see in your code, string literals are a source of pointers as well, and nothing stops you from writing stuff like char *p = (char *)0x800; if you want, which is something you see often in kernel or real-mode programming.

Related

Is this the correct way to dynamically allocate a float variable [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 2 years ago.
Improve this question
In this code, is this the correct way?
float pf = (float) malloc (sizeof(float));
This is not the correct way. It is incorrect on several different levels of severity.
First, a dynamically allocation of an object of any type returns a pointer to an object, not the object itself.
A dynamically allocated float will return a pointer to a float, so the declaration would be float *pf = (float*) ...
Which means that the cast would be (float*) not (float).
Going deeper, since the language tag is C++ using malloc is incorrect, or at best outdated. Use new and delete, not malloc and free.
Instead of C-style casting (float*), static_cast would be the modern C++ choice.
The code could easily be fixed to be correct, in the sense of compiling and giving the correct result. Then there's another big step from "it works" to "best practices."

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.)

What does a * do in front of a variable name? [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
What is the effect of putting an asterisk (*) in front of a variable name such as in the following line of code:
char prompt = 'y' vs. char *prompt = 'y'?`
Edit: When I originally posted this question, I thought that I saw this asterisked variable in a Java sample program that came included in Netbeans. That was my mistake -- It was C++ code sample in Netbeans. I was searching for an answer in some Java documentation and could not find it which led me to ask the question here. In the end, it was a trick question that I didn't realize was tricky at the time.
The answer is that the asterisk has something to do with "pointers", which has to do with memory addressing better explained by anyone but me. And, they are not used in the Java language but most certainly are in C++.
char prompt="something" is wrong since char is only one byte long so you can only assign it something that is one byte long like
char prompt='y'
More elaborately "something" is represented in c as a array of character so compiler will also shout that you cannot assign const char[10](note length is 10 since a null character is appended at the end of the string by the compiler) to char.It is just like putting of buckets of apples to a single apple and result will be total car crash.
On the other hand char* prompt ="something" is pointer to a character.Here prompt is pointing to the string "something" you can also use indexing on it like prompt[0],prompt[1] which will result in s and o respectively.
I am also assuming that you mistakenly tagged your question to java.
That means it's a pointer. There is no pointer in Java.
So if you have char *str, in str you don't have something like "Hello World" but the memory address where the string "Hello World" is.

How to allocate 100 bytes of memory [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 9 years ago.
Improve this question
I come across this question in an interview. I answered
char *p = new char[100];
which is wrong according to interviewer.
Can anyone tell me what is the correct way, and what exactly interviewer was expecting?
Your answer is correct. However, it requires manual memory management and is thus error-prone. A more C++-y way would be
std::vector<char> buffer(100);
Or indeed, if the number 100 is a compile-time constant:
std::array<char, 100> buffer;
// or
char buffer[100];
Finally, if we are really interested in low-level memory management, here is another way:
std::allocator<char> alloc;
char* buffer = alloc.allocate(100);
Without knowing what sits in his head, he might not know that char is guaranteed to have size equal to 1 in C++ (ยง5.3.3/1), and was expecting something like:
void* mem = malloc(100);
Still, in C++ vector would probably be preferred.
Or maybe he didn't want you to use dynamic allocation at all?

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)