c++Does this contain a memory leak

Does the code below contain a memory leak. I suspect it does but the tools I use to detect them(Visual Studio + Parasoft c++ test) aren't flagging up anything. If it is how would I fix it?

//A dynamically allocated array of char pointers
int numOfStrings = 10, numOfChars = 32;
char** data = new char*[numOfStrings];

//Generate each each individual string
for(int i = 0; i <numOfStrings; i++)
    data[i] = new char[numOfChars];

//moves the elements 1-5 in the array to the right by one
int index = 1, boundary = 5, sizeToMove = (boundary - index) * sizeof(numOfChars);
memmove(&data[index + 1],&data[index],sizeToMove);

delete[] data;

EDIT:

I should mention, I have tried iterating over each individual string as below but an exception occurs.

for(int i = 0; i< numOfStrings; i++)
    delete [] data [i];

Yes it does. When you are deleting

delete[] data;

You are releasing the memory allocated for data. However the memory allocated

data[i] = new char[numOfChars];

is still not freed.

You have to iterate over data and delete each data[i] before deleting data.

Generally you should make sure you have as many deletes as news.

Here you have numOfStrings + 1 news and only one delete.

One more leak

Since you are doing

int index = 1, boundary = 5, sizeToMove = (boundary - index) * sizeof(numOfChars);
memmove(&data[index + 1],&data[index],sizeToMove);

(You are not moving shifting five places as you thought but 4 places (5 - 1 = 4))

After this operation

data[2] will get the value of data[1]

data[2] <- data[1]
data[3] <- data[2]
data[4] <- data[3]
data[5] <- data[4]

And what was pointed to by data[5] will be lost.

data[2], data[1] will have the same value (point to the same place)

This can also explain why you are getting segfault when you want to delete by iterating over data

The rule is: for every 'new', there MUST be a corresponding 'delete' call. You don't have that, so you have a leak.

Yes you have a leak!

You have to delete each pointer in the array before deleting the pointer pointing to them.

//delete each array
for(int i = 0; i <numOfStrings; i++)
    delete[] data[i];

//this is a single pointer, not an array
delete[] data;

Now you won't have a leak

Related Articles
  • I'm following a book on C++ programming, and I'm following the exercises. One exercise asks me to create a program that produces a memory leak. Will this program produce such a leak? int main() { int * pInt = new int; *pInt = 20; pInt = new int; *pIn
  • Say I have an AbstractBaseClass and a ConcreteSubclass. The following code creates the ConcreteSubclass and then disposes of it perfectly fine and without memory leaks: ConcreteSubclass *test = new ConcreteSubclass(args...); delete test; However, as
  • The following code is taken from a DirectX tutorial and slightly modified by me: void Initialize() { // case 1 m_camera.SetLookAt( new D3DXVECTOR3( x_component, y_component, z_component ) ); // case 2 m_camera.SetLookAt( &D3DXVECTOR3( x_component, y_
  • I'm writing a string class that is similar to std::string for a homework assignment, but I cannot figure out how to return a c-string that does not cause a memory leak and is guaranteed to stay the same until it is no longer in use. I currently have:
  • I am trying to understand how events can cause a memory leak. I found a good explaination at this stackoverflow question but when looking at objects in Windg, I am getting confused with the result. To start with, I have a simple class as follows. cla
  • UPDATE I noticed that with a particular require_once('myobject.php') that the memory increase showed up. This require_once() was run conditionally. When I moved the require_once()statement out of the if statement so it loads all the time, the memory
  • function outer(){ var a, b, c; function inner1(){ ... } function inner2(){ ... } inner1(); inner2(); ... } I want to keep the global namespace clean so I wrote above code. Inner functions are only used by code inside Outer. But after that I begin to
  • Below is the extract of Code, in which i am getting some Possible memory loss in Valgrind Report. 681 int pbsc::PBSCAppMain( int argc, char **argv ) 682 { 683 char pbscPath[255]; 684 std::string configDir = "/../config/"; 685 std::string pbscBin
  • I'm trying to implement my own version of NSViewController (for backwards compatibility), and I've hit a problem with bindings: Since bindings retain their target, I have a retain circle whenever I bind through File's owner. So I thought I'd just exp
You Might Also Like