uncommon use of malloc [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 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.

Related

Free up memory immediately by swap trick (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 3 years ago.
Improve this question
I have a very large vector as follows.
vector<vector<int>> a(100000, vector<int>(100000);
// do some work using a here
......
After the work is done, I use
vector<vector<int>>().swap(a);
in order to free the memory used by "a" immediately. I add
sleep(100)
after the swap and use "top" command to see whether memory usage is reduced immediately. But I observe that nothing has changed. What happens here?
To immediately clear vector out from the memory you should do :
v.clear();
v.shrink_to_fit();
It should work you might have error elsewhere in the code or you don't judge correctly whether the memory is freed.
You could make the deallocation more general - so it'll work on anything without performing any unnecessary operations in the swap. Swap consists of three moves. You can achieve the same result with a move and default reinitialization.
{auto x=std::move(a);a={};}
x steals a's memory and gets destroyed after leaving the scope. The a={}; isn't be necessary in most cases but according to standart after moving from object is allowed to be in a rather weird state.

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

C++ : deleting different static and dynamic pointer 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 6 years ago.
Improve this question
I have a piece of dumb code for which i need some explanations.
int main() {
int *ptr_i = new int[100];
char *ptr_c = (char *)ptr_i;
delete [] ptr_c;
return 0;
}
First of all I was expecting this code to crash but it didn't which i believe is because in this case the allocater will keep a track of how many bytes to de-allocate. I ran valgrind also on this code and it shows no memory leak.
I need clarification regarding following :
In case when dealing with POD data type, how will a c'tor of char differ from that of int ?
Apart from coding convention, what other problems can this code lead to ?
There is no constructor for an int, nor for a char. However, since the usage of operator delete (more accurately delete []) does not match the usage of operator new (new []) the behaviour is undefined.
Undefined behaviour does not mean a crash will occur. It does not mean that a memory leak will occur.
It simply means that the C++ standard places no restrictions on what happens.
A crash might or might not occur. A memory leak might or might not occur. The compiler might or might not reformat your hard drive. Your program might or might not print the value 42 a total of 27 times. Any other set of occurrences you can imagine might or might not occur.
So the problems such code may cause could be .... anything ... or even nothing. The biggest problem is that you cannot necessarily know.

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?

This works for any numbers smaller than 521153. This way it does not work and returns a very large negative number [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
#include <iostream>
using namespace std;
int main()
{int primes[521153];
return 0;
}
This works for any numbers smaller than 521153. This way it does not work and returns a very large negative number.
You have exceeded you stack size. The C languages work like "portable assembler", where all language operations map directly onto machine operations, even if those operations cause "undefined behavior". In this case your stack collides with the heap, and your program overwrites its own brains and dies.
Use int *primes = new int[BIG_NUMBER]; IIRC, that's well-defined to raise an exception if it cannot allocate that much memory. And your heap (where new gets its storage) can grow arbitrarily (on modern architectures), where your stack cannot. You C++ tutorial will define all these terms for you.