Help nullptr

im not sure why this is saying its a nullptr
52 Replies
sigma
sigma3y ago
I have no idea why this is the case Could you check the stack trace when the error happens?
sesame.seed2
sesame.seed23y ago
how do you do that
sigma
sigma3y ago
Give me a sec, I'm gonna show you real quick I gotta take screenshots
sesame.seed2
sesame.seed23y ago
okay thank you
sigma
sigma3y ago
sesame.seed2
sesame.seed23y ago
okay
sigma
sigma3y ago
sigma
sigma3y ago
During debugging, ensure that you have the call stack window. Click on Debug, then expand Windows, and the finally click on Call Stack When you open it the first time, it might the empty or not. It would depend on whether your program has hit the breakpoint you placed or not
sesame.seed2
sesame.seed23y ago
okay
sigma
sigma3y ago
This is how the Call trace might look like if the breakpoint has not hit. Focus on the bottom panel
sigma
sigma3y ago
This is how it looks like after execution hit the breakpoint I placed. Notice that the breakpoint red circle has an arrow inside, and the Call trace has some stuff inside
sesame.seed2
sesame.seed23y ago
sigma
sigma3y ago
Could you check the second function shown from the top? The read(Postal* &pptr) one
sesame.seed2
sesame.seed23y ago
sigma
sigma3y ago
Could you show the load(const char* filename)?
sesame.seed2
sesame.seed23y ago
sigma
sigma3y ago
So, the reason you are having a nullptr error is because in the load function, you passed p by reference. This means that pptr in read(Postal*& pptr) is a reference to a pointer to Postal. So, when you call read(*pptr, fptr) in line 75, you are derefencing the nullptr. However, the error doesn't happen until later, because you passed the nullptr value by reference.
sesame.seed2
sesame.seed23y ago
oh how do i fix that
sigma
sigma3y ago
C++ can be confusing at times
sesame.seed2
sesame.seed23y ago
im dying
sigma
sigma3y ago
Well, I don't know your intentions, but the idea is that p shouldn't be Postal*, if you are intending that it should be filled by the read function
sesame.seed2
sesame.seed23y ago
oh
sigma
sigma3y ago
Instead, it should be Postal, and create a Postal object in p. Then change read(Postal*& pptr) to read(Postal& pptr), and update line 75 to reflect that you are not dereferencing, but instead passing by reference
sesame.seed2
sesame.seed23y ago
so just Postal p;
sigma
sigma3y ago
Yeah
sesame.seed2
sesame.seed23y ago
ok
The Many
The Many3y ago
TY for the call stack tip. I didn't know about that.
sigma
sigma3y ago
I might hate Visual Studio for being bloaty, but it does have a lot of convenient features that ease debugging.
sesame.seed2
sesame.seed23y ago
its saying my read function in load is wrong
sigma
sigma3y ago
What do you mean?
sesame.seed2
sesame.seed23y ago
sesame.seed2
sesame.seed23y ago
its saying instance of this function no*
sigma
sigma3y ago
You forgot to update the definition and prototype of the read() function
sesame.seed2
sesame.seed23y ago
omgggg how would i delete this
sigma
sigma3y ago
I think you can delete that line
sesame.seed2
sesame.seed23y ago
okay do you know what this assertion error is
sesame.seed2
sesame.seed23y ago
sigma
sigma3y ago
The postalString array is too small It shouldn't be 3 characters It should be 4: 3 for the postal code plus one more for the null byte
sesame.seed2
sesame.seed23y ago
i added the plus 1 on the first line in the if
sigma
sigma3y ago
That only counts when you are counting the length, but if you are going to allocate the array, you need 4
sesame.seed2
sesame.seed23y ago
should i remove the plus 1?
sigma
sigma3y ago
No, you should keep it What I mean is when you are declaring the postalString array. You allocated only three elements. It should be 4
sesame.seed2
sesame.seed23y ago
okay
sigma
sigma3y ago
I apologise if I wasn't clear enough
sesame.seed2
sesame.seed23y ago
no its okay its still the same even when i changed it to 4 thank you for all your help btw!
sigma
sigma3y ago
I think the format string in the fscanf might be a little bit weird I think it has to be "%[^,]s", maybe?
sesame.seed2
sesame.seed23y ago
the format of the file is A0B,45769\n
sigma
sigma3y ago
Oh, I'm talking about of line readPostalCode = (fscanf(fptr, "%[^,],", postalString) == 1) I think the string "%[^,]," should be "%[^,]s"
sesame.seed2
sesame.seed23y ago
oh that worked thank you!
The Many
The Many3y ago
wouldn't those reads have to be inside the loop though or you will only read the first one
sesame.seed2
sesame.seed23y ago
i am using both read functions from part 1
Want results from more Discord servers?
Add your server