Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15360

Re: memory allocation/deaalocation in calss

From asit <lipun4u@gmail.com>
Newsgroups comp.lang.c++
Subject Re: memory allocation/deaalocation in calss
Date 2012-04-13 15:17 -0700
Organization http://groups.google.com
Message-ID <2177910.9.1334355438050.JavaMail.geo-discussion-forums@pbky1> (permalink)
References <28714271.3.1334350972034.JavaMail.geo-discussion-forums@pbcuv5>

Show all headers | View raw


Here is the code..

#include <iostream>

using namespace std;

class LinkedList
{
    struct node
    {
        int data;
        node *next;
    };

    node* header;

public:
    LinkedList():header(NULL) {}
    LinkedList(const LinkedList &cp)
    {
        node *t = cp.header, *t2;
        header = NULL;
        while(t != NULL)
        {
            t2 = new node;
            t2->data = t->data;
            t2->next = header;
            header = t2;
            t = t->next;
        }

    }
    LinkedList& operator=(const LinkedList &cp)
    {
        if(&cp == this)
            return *this;
        //LinkedList();
        node *t = cp.header, *t2;
        header = NULL;
        while(t != NULL)
        {
            t2 = new node;
            t2->data = t->data;
            t2->next = header;
            header = t2;
            t = t->next;
        }

        return *this;

    }
    ~LinkedList()
    {
        node * t;
        while(header != NULL)
        {
            t = header;
            header = header->next;
            delete t;
        }
        header = NULL;
    }
    void push(int data)
    {
        node *t = new node;
        t->next = header;
        t->data = data;
        header = t;
    }

    void printList()
    {
        node *t = header;
        while(t != NULL)
        {
            cout<<t->data<<"->";
            t = t->next;
        }
        cout<<endl;
    }
};

int main()
{
    LinkedList list1;
    list1.push(10);
    list1.push(30);
    list1.push(12);
    list1.push(19);
    list1.push(78);
    cout<<"List1 : ";
    list1.printList();

    LinkedList list2;
    list2 = list1;
    list2 = list1;
    cout<<"List2 : ";
    list2.printList();


    return 0;
}


Here is how DrMemory screams at me..

Dr. Memory version 1.4.6 build 2 built on Mar  7 2012 10:14:04
Application cmdline: ""C:\web\LoopLinkedList\bin\Debug\LoopLinkedList.exe""
Recorded 62 suppression(s) from default C:\Program Files\Dr. Memory/bin/suppress-default.txt

Error #1: UNINITIALIZED READ: reading 0x0022fd38-0x0022fd3c 4 byte(s)
# 0 ntdll.dll!TpDisassociateCallback         +0x382    (0x7730f641 <ntdll.dll+0x2f641>)
# 1 ntdll.dll!TpCheckTerminateWorker         +0x11     (0x7733b9ae <ntdll.dll+0x5b9ae>)
# 2 KERNELBASE.dll!TerminateThread           +0x50     (0x7561dc35 <KERNELBASE.dll+0xdc35>)
# 3 profile_ctl       
# 4 profil            
# 5 moncontrol        
# 6 _mcleanup         
# 7 msvcrt.dll!isspace   
# 8 msvcrt.dll!cexit     
# 9 mainCRTStartup    
#10 KERNEL32.dll!BaseThreadInitThunk
#11 ntdll.dll!RtlInitializeExceptionChain
Note: @0:00:01.789 in thread 4812
Note: instruction: cmp    0xfffffff8(%ebp) %ebx

Error #2: LEAK 8 direct bytes 0x003e14d8-0x003e14e0 + 32 indirect bytes
# 0 operator new()    
# 1 operator new()    
# 2 LinkedList::operator=()               [C:/web/LoopLinkedList/main.cpp:40]
# 3 main                                  [C:/web/LoopLinkedList/main.cpp:93]

DUPLICATE ERROR COUNTS:

SUPPRESSIONS USED:

ERRORS FOUND:
      0 unique,     0 total unaddressable access(es)
      1 unique,     1 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total warning(s)
      1 unique,     1 total,     40 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
     68 still-reachable allocation(s)
         (re-run with "-show_reachable" for details)
Details: C:\Users\asit\AppData\Roaming/Dr. Memory/DrMemory-LoopLinkedList.exe.1280.000/results.txt



Please help me to resolve this.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

memory allocation/deaalocation in calss asit <lipun4u@gmail.com> - 2012-04-13 14:02 -0700
  Re: memory allocation/deaalocation in calss "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-04-13 23:35 +0200
  Re: memory allocation/deaalocation in calss Ian Collins <ian-news@hotmail.com> - 2012-04-14 09:36 +1200
  Re: memory allocation/deaalocation in calss Kevin McCarty <kmccarty@gmail.com> - 2012-04-13 14:35 -0700
    Re: memory allocation/deaalocation in calss Juha Nieminen <nospam@thanks.invalid> - 2012-04-14 05:27 +0000
      Re: memory allocation/deaalocation in calss Christopher Creutzig <christopher@creutzig.de> - 2012-04-26 19:46 +0200
  Re: memory allocation/deaalocation in calss asit <lipun4u@gmail.com> - 2012-04-13 15:17 -0700
    Re: memory allocation/deaalocation in calss Ian Collins <ian-news@hotmail.com> - 2012-04-14 11:29 +1200
  Re: memory allocation/deaalocation in calss asit <lipun4u@gmail.com> - 2012-04-13 15:31 -0700

csiph-web