I am taking a C++ class and have a assignment which requires me to dynamically allocate memory for a struct. I don\'t recall ever going over this in class and we only brief
new returns a pointer to an object... so you'd want
struct Student* student1 = new Student;
Why are you using new
? Just declare an instance of the variable:
Student student1;
Given the definition of Student
, it doesn't look like it has identity,
and it is certainly copyable, so you should probably never new
it.
(I'd also provide it with a constructor, so that it can be initialized correctly from the moment it is defined.)