Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

[CPP] Dynamic memory allocation - destructor & delete

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, as part of HW assignment, I wanted to make sure I implemented the destructor (Classroom, line 8) and the delete[] (classroom, line 39) the right way.


Student


Classroom
 
Rancher
Posts: 321
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alex - can't you use std::vector? A vector will just grow in size automatically as you add items to it, simplifying your code a lot.
Lots of online documentation eg. https://www.geeksforgeeks.org/vector-in-cpp-stl/
Cheers
John
 
Alex Keezner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey. We can use whatever methods we want with HW, but during the exam only what was taught in class and vectors aren't part of the curriculum. So I'm trying to avoid them.
 
Marshal
Posts: 73748
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know any C++, but what does delete mean when followed by array brackets? Does it remove a space from an array or does it delete the whole array?
Please avoid abbreviations like HW, which I haven't seen since 1966, because they will be unintelligible to some of our readers who don't have English as a first language.
 
John Matthews
Rancher
Posts: 321
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not a C++ expert either, but I don't think the ClassRoom destructor is correct. Inside the loop you should be deleting a single Student, delete studentList[i], and only after the loop can you delete[] stdudentList.

And I think the upper limit of the loop should be numberOfStudents (without the - 1), so that i goes from 0 to (numberOfStudents - 1).
 
Campbell Ritchie
Marshal
Posts: 73748
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:. . . deleting a single Student, delete studentList[i] . . .

That is different from what line 10 says, which makes me suspect line 10 is wrong.

. . . numberOfStudents (without the - 1), so that i goes from 0 to (numberOfStudents - 1).

Good catch, which I didn't notice; that loop was definitely written incorrectly.
 
John Matthews
Rancher
Posts: 321
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

John Matthews wrote:. . . deleting a single Student, delete studentList[i] . . .

That is different from what line 10 says, which makes me suspect line 10 is wrong.

Yes, although I think it's correct but in the wrong place.
 
Campbell Ritchie
Marshal
Posts: 73748
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree: you have explaineed that delete[] seeems to delete the whole array. Don't know whether it is necessary also to call delete[i] on each array element; I suspect it is.
[addition]...or whether you should call delete my_array[i] instead.
 
John Matthews
Rancher
Posts: 321
11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Agree: you have explaineed that delete[] seeems to delete the whole array. Don't know whether it is necessary also to call delete[i] on each array element; I suspect it is.
[addition]...or whether you should call delete my_array[i] instead.

Right - yes, you need both. Deleting an array of pointers doesn't delete what the pointers are pointing at, so the loop is necessary first to delete the pointed-to objects (Students).
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic