Thread: heap memory management

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    heap memory management

    I've got a program with 1 struct and 3 functions. Function 1
    allocates memory to the structs. Function 2 deallocates the
    memory. And function 3 tells you if the memory is allocated or not.

    Code:
    struct sTest
    {
       int i1;
       int i2;
    };
    
    sTest *test1, *test2;
    
    void AllocTest(sTest* TestToAlloc);
    void DeallocTest(sTest* TestToDealloc);
    void TestTest(sTest* TestToTest);
    
    void AllocTest(sTest* TestToAlloc)
    {
        if(!TestToAlloc)
         {
            TestToAlloc=new sTest;     
            TestToAlloc->i1=10;
            TestToAlloc->i2=11;
          }
          printf("%d allocated.",TestToAlloc);
    }
    
    void DeallocTest(sTest* TestToDealloc)
    {
        printf("%d allocated.",TestToAlloc); 
        if(TestToDealloc)
         {
            delete TestToDealloc;
            TestToDealloc=NULL;
         }
    }
    
    void TestTest(sTest* TestToTest)
    {
        if(TestToTest)
        {
           printf("%d allocated!",TestToTest);
        }
        else
        {
           printf("%d deallocated!",TestToTest);
        }
    }
    In the app the you input a number 1-7 that is used in a switch to
    perform one of the 3 functions on one of the 2 struct pointers.
    Code:
    bool bExit=false;
    int choice;
    for(;;)
    {
       printf("Enter your choice: " );
       scanf("%d",&choice);
       switch(choice)
       {
        case 1:
         {
            AllocTest(test1);
            system("cls");
            getch();
          }break;
        case 2:
          {
            AllocTest(test2);
            //same system("cls") and getch() lines for 1-6, left out for
           //brevity
           }break;
         case 3:
          {
            DeallocTest(test1);
          }break;
          case 4:
           {
             DeallocTest(test2);
           }break;
          case 5:
           {
             TestTest(test1);
           }break;
          case 6:
          {
             TestTest(test2);
          }break;
          case 7:
          {
             bExit=true;
             DeallocTest(test1);
             DeallocTest(test2);
          }break;
       }
       if(bExit) break;   
    }
    return(0);
    Problems:
    First off, I really need a good tutorial on heap management

    When you choose either 1 or 2 it returns different #s every time.
    As I understand what I'm returning here is a long that represents
    a pointer to that structure in the heap memory. So if I choose 1
    I get (for instance) 7867904. Then if I choose 1 again (without
    choosing to deallocate it first) I get 7867840.

    Also if I run AllocTest either test# and then test it with TestTest I
    always get a return that it's deallocated (when I never chose to
    deallocate it).

    I'm pretty sure that it all boils down to the fact that I am really
    lost when it comes to heap memory management. Does anyone
    know of any good articles/tutorials on the subject? As I mentioned in an earlier post all that I could find in MSDN were
    functions/structures that said they were unsupported in Win98.

    Thanks in advance for any help.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    I don't think your problem is with heap management.
    Rather pointer management seems to be the problem.

    You declare these global pointers without initializing them to null:

    >>sTest *test1, *test2;

    You can think of them as wild pointers, no one knows where they point too.

    Next, you pass a wild pointer to a function and test it:

    >>if(!TestToAlloc)

    Since we don't know what the pointer is pointing at how can we trust the logic of the above statement?

    I think you will benefit by reading up on pointers and scope rules.

    Let me know what you think

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Ted,
    Definitely. When I first started learning C++ I skimmed the basics
    on pointers, but never went deep enough into them (ie: dynamic
    allocation, deallocating them, etc.). I'm kind of playing catch-up
    now.

    I found the tutorial listed in the FAQ (the tripod URL one) and I'm
    in the middle of it's 20 or so pages on pointers right now.
    Definitely helping to clear a lot of stuff up.

    Thanks.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You can't change where a passed pointer points to in a function. You can only change the data it points to. To change a pointer within a function (for example allocate memory for it), use a double pointer.
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    struct sTest
    {
       int i1;
       int i2;
    };
    
    sTest *test1 = NULL;
    sTest *test2 = NULL;
    
    void AllocTest(sTest** TestToAlloc);
    void DeallocTest(sTest** TestToDealloc);
    void TestTest(sTest** TestToTest);
    
    void AllocTest(sTest** TestToAlloc)
    {
        if(!*TestToAlloc)
         {
            *TestToAlloc=new sTest;     
            (*TestToAlloc)->i1=10;
            (*TestToAlloc)->i2=11;
          }
          printf("%d allocated.\n",*TestToAlloc);
    }
    
    void DeallocTest(sTest** TestToDealloc)
    {
        printf("%d allocated.\n",*TestToDealloc); 
        if(*TestToDealloc)
         {
            delete *TestToDealloc;
            *TestToDealloc=NULL;
         }
    }
    
    void TestTest(sTest* TestToTest)
    {
        if(TestToTest)
        {
           printf("%d allocated!\n",TestToTest);
        }
        else
        {
           printf("%d deallocated!\n",TestToTest);
        }
    }
    
    int main(void)
    {
    bool bExit=false;
    int choice;
    for(;;)
    {
       printf("Enter your choice: " );
       scanf("%d",&choice);
       switch(choice)
       {
        case 1:
         {
            AllocTest(&test1);
            system("cls");
            getch();
          }break;
        case 2:
          {
            AllocTest(&test2);
            //same system("cls") and getch() lines for 1-6, left out for
           //brevity
           }break;
         case 3:
          {
            DeallocTest(&test1);
          }break;
          case 4:
           {
             DeallocTest(&test2);
           }break;
          case 5:
           {
             TestTest(test1);
           }break;
          case 6:
          {
             TestTest(test2);
          }break;
          case 7:
          {
             bExit=true;
             DeallocTest(&test1);
             DeallocTest(&test2);
          }break;
       }
       if(bExit) break;   
    }
    return(0);
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's a clearer way to do it.
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    struct sTest
    {
       int i1;
       int i2;
    };
    
    sTest *test1 = NULL;
    sTest *test2 = NULL;
    
    void AllocTest(sTest* &TestToAlloc);
    void DeallocTest(sTest* &TestToDealloc);
    void TestTest(sTest* TestToTest);
    
    void AllocTest(sTest* &TestToAlloc)
    {
        if(!TestToAlloc)
         {
            TestToAlloc=new sTest;     
            TestToAlloc->i1=10;
            TestToAlloc->i2=11;
          }
          printf("%d allocated.\n",TestToAlloc);
    }
    
    void DeallocTest(sTest* &TestToDealloc)
    {
        printf("%d allocated.\n",TestToDealloc); 
        if(TestToDealloc)
         {
            delete TestToDealloc;
            TestToDealloc=NULL;
         }
    }
    
    void TestTest(sTest* TestToTest)
    {
        if(TestToTest)
        {
           printf("%d allocated!\n",TestToTest);
        }
        else
        {
           printf("%d deallocated!\n",TestToTest);
        }
    }
    
    int main(void)
    {
    bool bExit=false;
    int choice;
    for(;;)
    {
       printf("Enter your choice: " );
       scanf("%d",&choice);
       switch(choice)
       {
        case 1:
         {
            AllocTest(test1);
            system("cls");
            getch();
          }break;
        case 2:
          {
            AllocTest(test2);
            //same system("cls") and getch() lines for 1-6, left out for
           //brevity
           }break;
         case 3:
          {
            DeallocTest(test1);
          }break;
          case 4:
           {
             DeallocTest(test2);
           }break;
          case 5:
           {
             TestTest(test1);
           }break;
          case 6:
          {
             TestTest(test2);
          }break;
          case 7:
          {
             bExit=true;
             DeallocTest(test1);
             DeallocTest(test2);
          }break;
       }
       if(bExit) break;   
    }
    return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  3. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  4. Heap corruption with dynamic memory
    By Head In Jar Man in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2009, 02:58 AM
  5. memory allocation from stack and heap ?
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 12:37 AM