how to completely free 2d multidimensional pointer array in c++ [duplicate] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I created an object which has few integer variables, and one char memory block say which is allocated with memory of 300-500 bytes as its members . After that this object was pushed in to vector by one thread, and then another thread which is running parallel will swap with one empty vector and start processing the vector which contains the object, after processing I used to delete the char block used in object and also finalized the object and deletd the object also. But it seems the memory was not freeing up. I ran this block of code with valgrind tool it doesn't show any leak. Please help me with the solution

But it seems the memory was not freeing up.
How are you determining that exactly? I assume incorrectly. Calling delete whatever marks that memory as deallocated and free for future use. The language doesn't specify exactly what that means. Your OS is better at managing memory than you are. It isn't forced to, for example, ensure that whatever tool (task manager?) you are using to measure memory usage now sees X more free bytes.
The memory you were using is now free to be allocated again. That's what you need to be concerned with, let the OS's memory manager worry about the details.

Related

c++ cleaning the heap [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am having serious issues in the heap because in the previous programs, I did not delete the pointers variables. How do I clean the memory space/heap now to run the current programs?
Kill the offending processes, then the OS will clean up their memory for you.
For every allocation you make on the heap with new/malloc or other such functions you must delete/free the resource that you have used when you are finished with it.
C++ makes available some easy ways to do this with constructors allocating resources and destructors free'ing the resource.
Then there are lots of clever ways with std::auto_ptr or even other smart pointers that can help you manage this.
Basically you allocated it - so it is your job to free it.

Dynamic allocation as a parameter, so to speak? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Assume we have a vector of Cursor object pointers. A cursor object is constructed with a single int parameter. So will this syntax work?
vector<cursor*> cursors;
cursors.push_back(new cursor(4));
Or do I have to do it as:
cursor* tempCursor = new cursor(4);
cursors.push_back(tempCursor);
Despite the fact that you may not have tried this here's an explanation of what's going on:
when you create a new cursor object it returns a new cursor object. When you use a push_back function it pushes an object back on a vector. So, when you create a new object inside the push_back it evaluates that function which returns a new cursor which then gets pushed back.
Basically its all about return values and evaluations.
You will leak memory if you do this as you are stating and don't clean it up afterwards.
A better way would be to use a shared_ptr.
The code would look like this
cursors.push_back(std::make_shared<cursor>(4));
It's a little hard to understand exactly what you're trying to accomplish though.
As mentioned below in the comments by #cat-plus-plus, unique_ptr should be used unless you explicitly want the object shared elsewhere, the code then would look like the following:
cursors.push_back(std::unique_ptr<cursor>(new cursor(4)));
It will work, but chances are pretty good that you're better off doing something else. Absent a really good reason to do otherwise, I'd consider making it a vector of cursors (instead of pointers to cursors). In this case, you can just do something like:
std::vector<cursor> cursors;
cursors.push_back(4); // or cursors.emplace_back(4);
This generally improves programmer efficiency by automating memory management, and improves code efficiency by eliminating unnecessary levels of indirection.

How to fix stack overflow error in visual C/C++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am writing a file in C/C++ which generates 100000 lines of records (name, int[5] grade, double[5].value). The code should generate 100000 random characters for name and integer for value. I am getting stackoverflow error. Can anyone pls help?
Do not use a recursive function to generate or process the records and do not allocate the records on the stack. Recursion can be replaced by iteration, and a stack array can be replaced by std::vector.
To avoid a stack overflow error, don't put so much data on the stack. Essentially: don't use local variables that are large arrays. Instead, create locals which are pointers, and use malloc() or new to allocate space for the actual data... this puts the pointer on the stack but the data elsewhere.
Finally, don't forget that you must free() anything you malloc(), and you must delete anything you new, once you're done with it (and while you still have the pointers!).

FFmpeg potential mem leak like behavior points. Are there such? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
So I mean like it tries to educate it self and collects some data... Are there any such points/feilds in encoding part of ffmpeg (that I hope can be disabeld)?
BTW: My problem is simple: I looked thrue out all my code. It seriosly looks like it is some part of my ffmpeg windows build leaks memory a littel... all the time while I am encoding... So I hope ffmpeg is just triing to learn so that I would be able to tell ti not to learn!)
FFmpeg libraries use a very object-oriented design. All memory allocated should be kept track of in the context structures and freed when the relevant context is destroyed. There may be some one-time allocation and initialization of constant global data which one could call a "leak", but I believe that was all replaced with static const tables to facilitate better use of shared memory and eliminate memory leaks associated with dynamic loading. If you really think it's leaking (and if you care), you need to use some memory debugging tools to identify where the leaks might be and coordinate finding/fixing them with the developers.
If what you mean is that during a single encode, memory usage grows slightly, this is probably normal and to be expected. It shouldn't be much, and the memory should all be released when the encoding context is freed.

How to fix heap corruption in c/c++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
This is a further question for my previous one here.
Is there a general solution for this?
Fix all dangling pointers
Fix all buffer overflows
Use pointers only where they are really needed
Reading your original post I'm not 100% you are facing a heap-corruption and you really hope you don't because if you would this is one of the most tricky errors to track down and AFAIK there are no solutions that always work.
However, if you are positive its a heap-corruption and you are on the windows platform you could try out the tool gflags. This advanced debugging tools allow you to install a debug heap manager in order to make it possible to find certain kinds of heap corruptions. One of the neat options it has is to make each allocation in it's own page or to write protect the heap datastructures. This makes it easier to pinpoint the exact location of the heap-corruption. It sucks lots of memory and CPU though.
Another tip if you are using Visual Studio is that if you manage to find that something is corrupting the data after a certain point of time and you wish to find the code that corrupts the data you can use Data Breakpoint to break whenever someone changes the data in question.
I wish you good luck.
Adding to swegi's answer I would check all your C++ constructors and all C initializations. Transform all dynamic C++ constructors (those where you put the init code in the function body) into static ones (where you initialize with the special constructor syntax). And be sure that you init all pointers and pointer members to 0. For C, I would initialize all variables.
Then, on unix I would use valgrind, usually this is quite good in finding all access violations and if you compile with all debugging options on it is able to trace it to the source line. There should be something similar on windows, too.