Thread: Pointer problem - Memory leak

  1. #1
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33

    Pointer problem - Memory leak

    Hi,

    I am not sure where the memory leak is in this program or why it is crashing. Could you please review the code for me and point out where I am going wrong?

    Thanks,

    QUESTION
    Put together a program that keeps track of monetary contributions to the Society
    for the Preservation of Rightful Influence. It should ask the user to enter the number
    of contributors and then solicit the user to enter the name and contribution of
    each contributor.The information should be stored in a dynamically allocated array
    of structures. Each structure should have two members: a character array (or else a
    string object) to store the name and a double member to hold the amount of the
    contribution.After reading all the data, the program should display the names and
    amounts donated for all donors who contributed $10,000 or more.This list should
    Programming Exercises 303
    be headed by the label Grand Patrons.After that, the program should list the
    remaining donors.That list should be headed Patrons. If there are no donors in one
    of the categories, the program should print the word “none.”Aside from displaying
    two categories, the program need do no sorting.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    struct donors
    {
        char names[80];
        double contribution;
    };
    
    int main()
    {
        int size = 0;
        donors *people = new donors[size];
        cout << "Enter the amount of contributers" << endl;
        cin >> size;
    
        for (int i = 0; i < size; i++)
        {
            cout << "\nContributer no. " << (i + 1) << endl;
            cout << "Enter name:\t";
            cin >> people[i].names;
            cout << "Enter the amount donated\t";
            cin >> people[i].contribution;     
        }
    
        cout << endl;
    
        for (int j = 0; j < 3; j++)
        {
            if (people[j].contribution >= 10000)
            {
                cout << "\nGrand Patrons: " << endl;
                cout << people[j].names << " donated "<<  people[j].contribution << endl;
            }
    
            else if (people[j].contribution < 10000)
            {
                cout << "\nPatrons: ";
                cout << people[j].names << " donated " << people[j].contribution << endl;
            }
            else if (size = 0)
            {
                cout << "\nNone" << endl;
            }
        }
         
        delete[] people;
    
        system("PAUSE");
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look at:
    Code:
        int size = 0;
        donors *people = new donors[size];
        cout << "Enter the amount of contributers" << endl;
        cin >> size;
    You should get the size and then create the dynamic array, not create a 0 length dynamic array and then get the size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33

    Cool

    Silly me, thank you for your response

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>donors *people = new donors[size];
    You should use a std::vector instead.

    >>char names[80];
    You should use std::string instead.

    >>for (int j = 0; j < 3; j++)
    What happens if size < 3? Always check your bounds!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion with pointer and memory leak
    By Dang Scholar in forum C++ Programming
    Replies: 9
    Last Post: 06-01-2012, 06:28 AM
  2. Any problem or memory leak here?
    By sehang in forum C Programming
    Replies: 4
    Last Post: 12-11-2010, 10:07 PM
  3. Memory leak With new
    By Shadowwoelf in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2010, 01:36 AM
  4. Possible Memory Leak?
    By g1i7ch in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 01:35 PM
  5. Replies: 2
    Last Post: 09-28-2006, 01:06 PM