How to allocate 100 bytes of memory [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 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?

Related

how is char able to store an address? [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 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.

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

C++ conversion from char* to byte 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 8 years ago.
Improve this question
I would appreciate if someone could give me an example of how to convert char *variable to byte array and vice-versa in C++.
Thanks
If std::vector<char> is what you're after, then it's simply:
std::vector<char> byteArry(charPtr, charPtr + sizeOfCharPtr);
and the other way:
const char* charPtr = byteArry.data();
Have you tried anything you self, maybe google?
Anyways a char *variable is a char pointer, so you will need to know the size of the data and create a byte array in the same size.
After that you can do a memcpy

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)

uncommon use of malloc [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'm running into this (short) code and I'm not sure what it exactly does;
int amount = 5
int totalAllocatedMemory = 0;
totalAllocatedMemory += amount, malloc(amount);
It seems that 'malloc' has no effect here!
Thanks
It does have an effect, in that it allocates memory. However, the code does look bizarre and the memory does get leaked.
In case you're wondering about the syntax and exact semantics, see How does the Comma Operator work
It:
Allocates 5 byes
Adds 5 to totalAllocatedMemroy (typo?)
Leaks the memory
Well the code looks buggy but here's what it does:
Adds amount to totalAllocatedMemory
Allocates 5 bytes and discards the result (which would be the address of the allocated memory, therefore as others said results to a memory leak)
This is because comma has the lowest precedence of all operators in C.