dynamic memory causing crashes

Hi, I'm currently a university student and I'm rather new to C / C++. The scope of my course which I have completed is on matlab, and C, otherwise I'd ask my lecturer. I am doing an exercise, making a small database program (purely for practice- I have no intention of actually using it), and I've been getting random errors which I'm unsure of how to fix.

http://pastebin.com/x6UcrQEK

The two errors I'm getting:
1) makeStudent() fails somewhere between 5-10% of the time, crashing the application. The last line of output is "Please enter student name: ". I think the cin >> name is causing the crash, but I literally have no idea why.
2) After making 3 objects of the class student, if I quit, the application crashes while executing the clean function. A screen shot below shows this.

http://www.freeimagehosting.net/newuploads/ea5ao.png


My apologies if the syntax is difficult to read. I prefix input arguments with i and output with o- probably not the best way, but I'm focusing on learning the content before I worry about syntax and the like.

Any help at all would be much appreciated. Also, on a side note, if theres a place where I can learn tidy syntax, that would also be nice.
newStudent = (student *) malloc(sizeof(student));
Malloc will not call constructors so the student object will not be initialized properly. This will cause problems when you later try to use the object.

In C++ we often use new instead of malloc and delete instead of free so that constructors and destructors are called correctly.
Shouldn't this call the constructor properly?

*newStudent = student(name, age);
student(name, age)
This calls the constructor and creates a temporary student object.

Then operator= (the copy assignment operator) is called on the uninitialized student object that newStudent points to. You have not defined operator= so an implicit defined operator= will be used. It will call operator= on all of the student's members. One of the members is a string. The string's operator= is likely to fail because the string was never initialized.
I'd like to double down on what Pete said here:
In C++ we often use new instead of malloc and delete instead of free


Pete's very polite. What I would have said was
In C++ we often use new instead of malloc and delete instead of free unless we have a really, really, really good reason not to.
Alright, thanks for your help.
closed account (zb0S216C)
The C++ equivalent to malloc() is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
template <typename Type>
Type *Construct(Type *Memory_, Type const &InitData_ = Type())
{
    if(Memory_)
    {
        try
        {
            new(Memory_) Type(InitData_);
        }

        catch(...)
        {
            return(NULL);
        }

        return(Memory_);
    }
  
    return(NULL);
}

int main()
{
    // Accumulation:
    int *SomeMemory_(static_cast<int *>(operator new(sizeof(int))));

    Construct(SomeMemory_, 0);

    // Deletion:
    operator delete (SomeMemory_);
}

ne555 doesn't seem to realise that he's using C++.

Wazzak
Last edited on
^ Nope, the c++ equivalent of malloc() is malloc()

Also, LISP.
Topic archived. No new replies allowed.