#What is the best Approach to deleting data here?

128 messages · Page 1 of 1 (latest)

weak wrenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

snow ermine
#

I'm not entirely sure, however...

#

generally with something like this the approach would probably be to return an iterator

#

then you can check if iter == q.end(); to see if it's valid

#

or, alternatively, maybe throw an exception if you try to pop something from an empty queue, because that would be not dissimilar from accessing an out of bounds array index

weak ravine
#

well, what do you need to delete why? what is the function going to return if the thing was deleted?

thorny grail
#

I’m just trying to understand memory management and leaks. I believe if I don’t delete it my program will have a leak?

weak ravine
#

well, if the thing was created via new, you generally will need to delete it yes

thorny grail
#

Oh, but in this case it’s both

#

Not*

#

So I won’t have any leakage

#

Leaks*

#

Is that correct?

weak ravine
#

it's pretty simple: do you delete everything that you new?

thorny grail
#

Well I just started cpp, so naturally I want to say yes.

#

In Java garbage collector does it automatically

weak ravine
#

well, C++ is based on the idea of simply not producing any garbage in the first place

#

then there's also no need to have someone come pick it up 😉

weak ravine
thorny grail
#

Like in general? i would new when i want to create a new instance of an object? such as Node* temp = new Node(params); then delete after it has been used/outputed to watever needs it?

weak ravine
#

i mean in your code

#

if you use new, you need to delete

#

simple as that

thorny grail
#

thats the only case? for example in my Node* temp = qFront; i dont need to delete the temp?

weak ravine
#

temp is not created via new

#

so no

thorny grail
#

man, this sounds dumb but this is what my professor said.

weak ravine
#

yeah that makes sense

#

qFront is what?

thorny grail
#

a node?

weak ravine
#

a pointer to a node it would seem

#

i'm assuming that node it points to was created via new

#

so what's temp then?

thorny grail
#

temp is just a copy of qFront that stores the data to be deleted so that i can re arrange the pointers

weak ravine
#

right

#

so temp also points to the node that was created via new

thorny grail
#

yes

weak ravine
#

and then you delete temp

thorny grail
#

yes

weak ravine
#

so you delete the node that you created via new

thorny grail
#

Correct.

weak ravine
#

well

#

then if we assume you always take everything out of the queue, there should be nothing left that wasn't deleted

#

ofc you still have the problem that your dequeue function doesn't return anything

thorny grail
#

right.

weak ravine
#

well, what should it return?

thorny grail
#

Well, if im following you correctly. since there wont be anything in the queue it should return null since there wont be anything there?

weak ravine
#

i'm not following i'm afraid

#

based on the screenshot, i would have assumed that it should return a CustomerData

thorny grail
#

yes! it returns an object of that type

#

correcto

weak ravine
#

what's that type?

thorny grail
#

CustomerData?

weak ravine
#

yes

thorny grail
#

so. maybe i need to extract that data from the node instead?

#

the node holds CustomerData data

weak ravine
#

yeah that sounds like a good idea

#

random note: one really shouldn't use NULL in C++

#

use nullptr instead

thorny grail
#

what would you have me use then? just dont assign?

#

oh

weak ravine
#

NULL is a C thing. it exists in C++ solely for the purpose of backwards compatibility with C.

#

there are various issues with NULL in C++

#

it's bad practise to use NULL in C++

thorny grail
#

good to know. i can fix that no prob

snow ermine
snow ermine
thorny grail
#

what do these symbols mean next to the variables?

#

just random side question

weak ravine
#

they indicate what kind of thing it is

#

variable, class, etc.

thorny grail
#

i see so the fact that nullptr and Null have different symbols doesnt mean one is c and the other is c++? lol as like a shortcut to know which is right

weak ravine
#

no

#

it means one is a kind of value and the other one is a type

#

or smth like that

#

tbh, not sure what the symbols there stand for exactly. would have to consult the documentation of your ide for that

snow ermine
#

nullptr isn't a thing in C

#

so that gives you part of your answer

weak ravine
#

(yet)

snow ermine
#

indeed, I wouldn't doubt it may be forthcoming in newer C language standards, but I don't pay any real attention to C, nor C language standards, either historical or forthcoming

weak ravine
#

iirc, C23 will introduce nullptr

snow ermine
#

TIL

weak ravine
#

not that it matters because who writes C anyways amirite 😛

snow ermine
#

insert <CHEEKY_QUIP_ABOUT_C_BEING_ABSOLUTE_GARBAGE>

#

I mean, it arguably gave us C++, so it's not 100% bad, only like 99.9999% bad

thorny grail
#

do you think im getting this now for not using New or is this a issue with something else

weak ravine
#

no, you're getting this because you don't have a main function in your program

thorny grail
#

crap sorry that was the wrong ss

weak ravine
#

well, you're getting those because those functions haven't been defined

thorny grail
#

omg i just realized its a different function as well from what i was working on originally.

weak ravine
#

no worries

#

linker errors specifically are something most people struggle with at the beginning

thorny grail
#

dawg i dont understand its properly defined at the top. how could something be undefined if i can see it in the file properly written out and declared.

snow ermine
#

short answer: lots of ways

thorny grail
#

i didnt even write the implementation though. i was given the files to edit LOL

snow ermine
#

🤷

#

Show Code ™️

thorny grail
#

what part of the code?

#

the tester has over 600 lines

snow ermine
#

in other words: the relevant parts, but all the relevant parts, but only the relevant parts

#

indeed most of us probably don't want to sit and read 600 lines of test harness if that doesn't relate to the actual core functionality

weak ravine
thorny grail
#

i mean if im getting a undefined reference im assuming its just my headers?

weak ravine
#

no

#

undefined reference means the function is used in your program but not defined anywhere

thorny grail
#

Yes, i am compiling my file

snow ermine
#

time to learn what a TU is

#

and probably, related, what it means to create and use your own libraries, both shared and static

weak ravine
thorny grail
#

No, so i have a Queue.h and a Queue.cpp with the implementations additionally im using Queue.h in a tester just to test the functions.

weak ravine
#

and how are you compiling your program?

snow ermine
#

if you have, for example, main.cpp, foo.cpp, and bar.cpp then you need to compile all of them

weak ravine
#

^

snow ermine
#

in your context, presumably you want to compile all of them into a single result

thorny grail
#

yea i know. one second im trying to re do it

#

dont know maybe the VM was running really slow or something but its working now...

#

this is how i was compiling i just reset VSC and it ran lol

#

Thanks for ur help dot 🙂 im sure ill have more questions in a bit but thanks for now feelsgoodman

#

!solved

weak wrenBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] What is the best Approach to deleting data here?

weak wrenBOT
#

@thorny grail

This question thread is being automatically marked as solved.

thorny grail
#

BRUH OKAY! I already closed it

#

!solved