Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
Got a couple questions. (this applies to C++)

I have a structure like this:

Code:
struct Word 
{
	int count;
    char* actualself;
};

I am dynamically creating a array of Word pointers. That's the easier part.

Now that I'm done,I'm trying to minimize my memory leaks. Like a good coder does. :D

But now I'm like *arg* Kidding aside, I would like your guys advice on how to delete structures and dynamically allocated memory that those structures hold.

Here's what I've tried thus far:

Code:
for (int i = 0; i < wordcount; i++) {
	cout << "I " << i << endl;
delete [] wordarray[i].actualself;
}
delete [] wordarray;

Assume that the loop does not go out of bounds. (it doesn't I can assure it. It craps out on the first time it tried to delete.)

When it reaches the first delete in the for loop, I get a HEAP CORRUPTION DETECTED error in VS.

If I comment out the for loop and just do the last delete statement, it runs fine, but I still have lots and lots of memory leaks. This is essentially the only allocated memory that I do runtime. I just tried taking out all deletes and leaving the last one in: it results into 10 memory leaks each time.

What's up?
 

mags631

Guest
Mar 6, 2007
622
0
Got a couple questions. (this applies to C++)

I have a structure like this:

Code:
struct Word 
{
	int count;
    char* actualself;
};

I am dynamically creating a array of Word pointers. That's the easier part.

Now that I'm done,I'm trying to minimize my memory leaks. Like a good coder does. :D

But now I'm like *arg* Kidding aside, I would like your guys advice on how to delete structures and dynamically allocated memory that those structures hold.

Here's what I've tried thus far:

Code:
for (int i = 0; i < wordcount; i++) {
	cout << "I " << i << endl;
delete [] wordarray[i].actualself;
}
delete [] wordarray;

Assume that the loop does not go out of bounds. (it doesn't I can assure it. It craps out on the first time it tried to delete.)

When it reaches the first delete in the for loop, I get a HEAP CORRUPTION DETECTED error in VS.

If I comment out the for loop and just do the last delete statement, it runs fine, but I still have lots and lots of memory leaks. This is essentially the only allocated memory that I do runtime. I just tried taking out all deletes and leaving the last one in: it results into 10 memory leaks each time.

What's up?

How are you allocating actualself? With new char[..]?
 

bbarnhart

macrumors 6502a
Jan 16, 2002
824
1
Code:
#include <iostream>

int main (int argc, char * const argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    struct my_struct
    {
      int my_int;
      char* my_string;
    };
    
    my_struct* my_struct_ptr = new my_struct[10];
   
    int i;
     
    for (i=0; i < 10; i++)
    {
      my_struct_ptr[i].my_string = new char[50];
    }
    
    for (i=0; i < 10; i++)
    {
      delete [] my_struct_ptr[i].my_string;
    }
    
    delete [] my_struct_ptr;
    
    return 0;
}

The above code does something similar. Look through it and see if you can find your mistake.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
Yes. I have valid pointers being returned. :D

I am able to use the same loop to print out my structures.
 

dcr

macrumors member
Jun 10, 2002
57
0
Do you have any code to ensure that new is returning a valid pointer? Its a bad idea to just blindly accept anything that new gives back. Always ensure that you have valid pointers.

contrary to popular belief, in standard C++ operator new does not return invalid pointers. if it's going to fail (say with out of memory) it will instead throw an exception like std::bad_alloc and bring your newbie program down in flames. :)

http://en.wikipedia.org/wiki/New_(c++)
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Got a couple questions. (this applies to C++)

I have a structure like this:

Code:
struct Word 
{
	int count;
    char* actualself;
};

Where's the constructor and destructor for struct Word? You know, the constructor that initializes "actualself" to NULL, and the destructor that deallocates it.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
When it reaches the first delete in the for loop, I get a HEAP CORRUPTION DETECTED error in VS.

I think this errror message is saying that delete[] has detected that you have overflowed the character array when you used it at some point. So, the bug lies elsewhere in your program… probably where gnasher729 was hinting, ie you've not allocated enough bytes to hold a string that you've written to actualself.

I would also agree with gnasher729 about using constructors and destructors, also I would add a method for setting the string, ie something like this:-

Code:
struct Word 
{
      Word() : actualself( 0L ) {}
      ~Word() { delete[] actualself ; }
     
      void set_actualself( const char* str ) { actualself = new[ strlen( str ) + 1 ] ; }

	int count;
    char* actualself;

};

hope this helps

b e n
 

bbarnhart

macrumors 6502a
Jan 16, 2002
824
1
What does the 0L part of that code do?

Sets actualself to zero which is also NULL. The L in 0L means to tell the compiler that the zero is a long(?) and not some other type (int, short, byte, char...). This is to assure that zero is a full 32-bit zero.
 

mags631

Guest
Mar 6, 2007
622
0
So I assume deleting with delete a word structure would trigger the deconstructor?

delete pointer - calls the destructor and then deallocates the memory
delete [] pointer - calls the destructor for each member of the array and then deallocates the memory of the array.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Ooops, assuming set_actualself() can be called more than once it has a memory leak! It should be like this:-

Code:
void set_actualself( const char* str )
{
   delete[] actualself ;
   actualself = new[ strlen( str ) + 1 ] ;
}

By the way in C++ it's okay to call delete and delete[] on a null pointer. Since the constructor initialises actualself with null, calling delete[] actualself is fine before setting actualself.

b e n
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.