Right now I feel fairly unlearned with dynamically allocating memory... even after doing so several times and doing projects that require one to dynamically allocate memory for classes and primitive types.

Basically, how does one research allocation for different scenarios such as...

#include <cstdlib>
#include <iostream>

using namespace std;

class Other
{
};

class MyClass
{
     public:
           int amount;
           Other **other1, *other2;           

           MyClass(Other *ot, int size)
           {
                   amount = size;
                   other2 = new Other[size];
                   other1 = new Other*[size];
                   
                   for(int i = 0; i < size; i++)
                   {
                             other1[i] = new Other[1];
                             other1[i] = &ot[i];
                             other2[i] = ot[i];
                   }
           }
           ~MyClass()
            {
                    for(int i = 0; i < amount; i++)
                    {
                             delete other1[i];
                    }

                    delete other1;
                    delete other2;
            }
};

int main(int argc, char *argv[])
{
    
    Other o[5];
    
    MyClass mc (o, 5);
    
    mc.~MyClass();
    
    cin.get();
    return 0;
}

Is the above correct?

I've been looking through various sites for examples of how to properly deallocate memory or assign objects to pointers with allocated memory and the usual examples only include 1 pointer.

Edit: Also, what should I do when I don't have a primary void constructor when allocating memory for that object for a double pointer? What then?

Recommended Answers

All 4 Replies

The code you oposted has a huge memory leak. Line 24 allocates some memory then line 25 tosses the pointer away and uses it for something else. One of those two lines needs to be deleted. My preference is to keep line 24 and delete line 25 so that the class has complete control over all its data.

Since MyClass does not own the memory for Other1 and Other2 (see lines 25 and 26) the MyClass destructor will most probably cause a core dump or some other nasy abnormal segmentation fault. You can not delete[] something you did not allocate with the new operator.

line 48: you compiler will give you an error on that line because you do not call destructors explicitly.

Emphasizing line #48, the mc object is on stack (i.e. not allocated by new), so when it goes out of scope, its destructor gets called automatically. Generally you don't call destructors, those get called via delete or automatically in case of stack objects.

> Basically, how does one research allocation for different scenarios
One learns the basics of dynamic memory and applies it in an ad hoc manner with every new scenario. Eventually you'll have seen scenarios before and the effort you put into doing it the first time will make subsequent times easier. Edward hasn't found a better way. :(

> the usual examples only include 1 pointer
Pointers are very uniform. Adding another pointer or another level of indirection doesn't change the principles, even if it does make the code more complex really really fast. ;)

The code you oposted has a huge memory leak. Line 24 allocates some memory then line 25 tosses the pointer away and uses it for something else. One of those two lines needs to be deleted. My preference is to keep line 24 and delete line 25 so that the class has complete control over all its data.

Since MyClass does not own the memory for Other1 and Other2 (see lines 25 and 26) the MyClass destructor will most probably cause a core dump or some other nasy abnormal segmentation fault. You can not delete[] something you did not allocate with the new operator.

line 48: you compiler will give you an error on that line because you do not call destructors explicitly.

Thanks everyone for your responses. I didn't realize my projects were causing such incredibly memory leaks. I'll look out for them.

And AD, my experience with allocating memory for double pointer has been the following--

--Whenever I declare a double pointer (for a class for example) and don't allocate memory for the pointers, my program shuts down on me because I'm supposedly trying to assign data to memory that I shouldn't have access to...

Which makes no sense because I thought that you could assign the initial value of the pointer to a valid reference regardless of whether you've allocated memory for it or not... This is the most confusing thing for me from switching from java to C++ because it feels like there are some inconsistencies with memory allocation.

For example, if I want a pointer to pointers in the following fashion...

int **intDPtr, int arr[5] = {1, 2, 3, 4, 5}, int arr2[5] = {6, 7, 8, 9, 10}, int *ptr1 = arr, int* ptr2 = arr2;

I supposedly cannot assign intDptr[0] with arr then intDptr[1] with arr2 without initially allocating memory for these "pointer objects" in the double pointer.

What I have concluded (which I know I may be wrong) is that I must be assigning a copy of the address to the other pointers in the double pointer which is why the compiler has an issue with pointers being allocated to the double pointer. I suppose I have to do something like--

int Dptr = new int*[pointerAmount];

but afterwards do I really need to allocate blocks of memory for the amount of pointers the double pointer is pointing to, if the pointer already handles memory initialization on its own or do I need to assign a certain amount of space for each pointer since ill be copying the address again?

I'm confused honestly...

I have my project attached, which I'm sure has a ton of memory leaks. The reason I'm asking these questions is because I've encountered these problems numerous times and I know I'm only learning bad programming by trying to code around the problem without actually solving it.

Radical Edwards and AD I will definitely look into better solutions for maintaining memory management. And RE it's really a damn shame that it takes hardcore trial and error to get this down but I definitely won't give up on it.

If any of you have the time please tell me what I did wrong in my project (in terms of memory management - I'm aware of some of the access issues that I'm trying to tweak but I have some sketches drawn out for what I want to do... it's the memory management that is the biggest issue).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.