Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Am I leaking with malloc?
Index -> Programming, C -> C Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dreadnought




PostPosted: Thu Nov 28, 2013 11:32 pm   Post subject: Re: Am I leaking with malloc?

copthesaint wrote:

@Dreadnought It's an array of pointers, so I dereference the first pointer in the array I can get every pointer from the simple pointer math. This has it's uses in polymorphism creating an array of pointers. all the memory is in the heap together so the next pointer will be mine ).

Can we please skip the necessity of telling me to use new and delete and focus on Malloc and free? I want to make sure I am using it correctly. I will not be using new / delete in this specific part of my project.

I understand what an array of pointers is and yes, you can get to all the pointers in your array by simple pointer math, but you aren't doing that. You dereference m_Var which gives you the first pointer in your array you store the address pointed to by this pointer in tempVar. Next you overwrite tempVar with the address of a newly malloc'ed object (tempVar no longer has ANY connection to your array), and then you increment the value of tempVar (which won't actually get used).

Here's an example to illustrate my point.
This is your code as you posted it with a few print statements (I used int as the type T and commented out the lines to call constructors and destructors for T)
c:
#include <stdlib.h>
#include <stdio.h>

int main () {
        int size = 5;
        int** m_Var = (int**) malloc(size * sizeof(int*));

        for (int i = 0; i < size; i++) {
                printf("mvar[%i] = %p\n", i, m_Var[i]);
        }

        int* tempVar = *m_Var;
        for (int i = 0; i < size; i++){
                tempVar = (int*) malloc(1 * sizeof (int));
                printf("tempVar = %p\n", tempVar);
//              new (tempVar) T();

                tempVar++;
        }

        for (int i = 0; i < size; i++) {
                printf("mvar[%i] = %p\n", i, m_Var[i]);
        }

        tempVar = *m_Var;
        for (int i = 0; i < size; i++){
//              tempVar->~T();

                printf("freeing %p\n", tempVar);

                free(tempVar); // I fixed the typo in this line

                tempVar++;
        }
        free(m_Var);

        return 0;
}


Now lets see what happens:
code:

$ g++ test.cc

$ ./a.exe
mvar[0] = 0x0
mvar[1] = 0x0
mvar[2] = 0x0
mvar[3] = 0x0
mvar[4] = 0x0
tempVar = 0x200482c8
tempVar = 0x200482d8
tempVar = 0x200482e8
tempVar = 0x200482f8
tempVar = 0x20048308
mvar[0] = 0x0
mvar[1] = 0x0
mvar[2] = 0x0
mvar[3] = 0x0
mvar[4] = 0x0
freeing 0x0
freeing 0x4
Segmentation fault (core dumped)

Notice that your array is unchanged and you have lost all pointers to the objects you allocated (and it crashed, though that wasn't my point).
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Nov 29, 2013 12:14 am   Post subject: RE:Am I leaking with malloc?

To elaborate: when allocating an array of (count) pointers to type T and those (count) T objects, you can allocate it as one array (pointers) plus (count) allocations of a single T object. Or, if you want to be crafty, you can allocate one array (pointers) and one array (objects, as per your 1D array). Then you just initialize the pointers to point at the 1D array. You can even move the pointers about, so long as you decide exactly how destruction will work (do you destroy the objects in the 1D array, or do you follow the pointers?)

There are some caveats. Depending on your design decisions, you may have to constrain yourself to not sharing objects between pools of the same type. Worse, it's pretty easy to generate memory leaks, double-free, or dangling pointers (depending on exactly how you define the behaviour on deletion and how you work with the "pool" object).

Also, the whole thing could be a complete disaster that modern containers like vector<T> (et al) obviate entirely.
copthesaint




PostPosted: Fri Nov 29, 2013 3:35 am   Post subject: RE:Am I leaking with malloc?

DemonWasp @ Fri Nov 29, 2013 wrote:
you can allocate one array (pointers) and one array (objects, as per your 1D array). Then you just initialize the pointers to point at the 1D array.


-_- Duh! Geez I'm slow. Can't believe I haven't been doing something that is so obvious.

Alright, I have everything I need, thanks guys for your help!
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 18 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: