I am a hobbyist programmer. I used to be half decent in C, BASIC and some assembler (mostly PIC). After years without touching an IDE, I have recently brushed up on my C skills (some online courses), and I've started learning C++. Classes are my new challenge :-)
I am trying to fool create a linked list, and all goes well until the destructor is called, and then the program enters an endless loop as soon as I try to reference any element of the class. Here is the relevant code:
----------------------------------------------------------
class Person
{
private:
char Name[255];
char Address[255];
char *Notes;
int NoteSize;
public:
Person(); // Constructor
~Person(); //Destructor
void SetName (char*);
void SetAddress(char*);
int SetNotes(char*,int);
void GetName (char*);
void GetAddress(char*);
void GetNotes(char*);
int GetNotesSize();
Person *Successor;
};
I can create nodes, manipulate their data, create "Notes" on the nodes, etc, no issues. However, when "delete [] Currentnode" is executed, things go south. The "Destructor" line is output, but the moment we get to the "if" or I try to print the node name or anything, it dies.
If you can't do anything with the exception just don't bother with a try-catch and let someone else handle it. It rarely makes sense to try to recover from an std::bad_alloc anyway.
Hint: You can hit "edit post", highlight your code and then press the <> formatting button. Note: This will not automatically indent your code! That part is up to you!
I've found it's easiest to copy and paste pre-indented code directly from the IDE, that way it is already properly formatted.
You can use the "preview" button at the bottom to see how it looks.
Using Code Tags, @Handy Andy, from cplusplus.com
First of all, have you run it through a debugger? That might help.
It rarely makes sense to try to recover from an std::bad_alloc anyway.
Why, I have seen a few times a dialog box with "There is not enough memory for this operation."
Closing Firefox gave me enough free memory to proceed with the other program.
@OP,
C++ has 2 linked lists already - std::list and std::forward_list.
No need to create another one.
I would suggest to learn about std::string and other basics first.
> /* Other stuff ommitted for brevity */
Don't do that, post enough code to reproduce your issue.
¿how are you building your list?
> Classes are my new challenge :-)
> I am trying to fool create a linked list
you've created a class Person with useless getters/setters that are badly implemented and that also represents a node.
¿where's your list class?
¿what do you expend so much time on the `data' class? there is no big difference between a list of Person, a list of strings or a list of integers
focus on the list operations: clear, traverse, add/remove element in front, back or middle, copy, etc.
This is program is an exercise, part of an online course I am following (on the LinkedIn learning platform). It's not the most useful piece of software (for example, exception catching was a must for the exercise, even if I am not doing anything particularly useful with it). std::list and so on is part of the next chapter in the course.
Sorry for the formatting, I copied the code directly from the IDE and assumed it would keep the indenting, which it didn't. I'll be more vigilant next time.
I am taking notes of all your comments, and will implement them (either on the code or in my next posts).
As for the debugger, I tried using the debugging capabilities of the IDE (Code::Blocks in my case, that is what the course called for), and I couldn't spot the issue since things went wrong over a single instruction, the pointers seemed to be correct, etc. (I didn't try to go and see into the assembler code to figure out what's wrong - my level of x86 assembly is somewhere between bad and absolutely useless).
For now, the solution from @helios fixed it. The brackets in the delete [] Currentnode; were the culprit (my guess, from what I understood now, is that code was trying to delete a vector of nodes when there is only one that is referenced, and thus entered an infinite loop).
Again, thanks a lot everyone, I've learned some useful stuff!
I've made some changes for consideration. However without all the code etc not much testing can be done. You should use std::string rather than c-style char arrays.