memory fault/leak

This is a discussion on memory fault/leak within the C++ Programming forums, part of the General Programming Boards category; Can this code have a memory fault/leak? Code: class A { public: ~A() {} vector<B> b; }; class B { ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Question memory fault/leak

    Can this code have a memory fault/leak?

    Code:
    class A {
    public:
    ~A() {}
    
    vector<B> b;
    };
     
    
    class B {
    public:
    ~B() {}
    
    map<C, string> c_map;
    };
    
    
    Class C {
    }
    
    
    main() {
    
    C c1,c2,c3,c4;
    
    B b1,b2;
    
    b1.c_map(c1) = "c1";
    b1.c_map(c2) = "c2";
    
    b2.c_map(c3) = "c3";
    b2.c_map(c4) = "c4";
    
    A *a = new A();
    a.b.push_back(b1);
    a.b.push_back(b2); 
    
    delete a;
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It doesn't have a memory leak.* Are you asking because of the empty destructor? Destructors of members are still called (automatically after user-defined destructor completes) - you can't change this feature of destructors.

    *Because of the use of a dumb pointer and not catching exceptions, this code can (theoretically) leak, should either of the push_back's trigger an exception to be thrown.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    However, it does have at least two syntax errors. Also, your map is either not a standard map, which means there could be memory leaks in there, or you have four more syntax errors.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    Thank You, anon & Corned Bee, thanks for the reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. available memory from task manager
    By George2 in forum Tech Board
    Replies: 10
    Last Post: 01-18-2008, 01:32 AM
  2. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21