Help nullptr
im not sure why this is saying its a nullptr
52 Replies
I have no idea why this is the case
Could you check the stack trace when the error happens?
how do you do that
Give me a sec, I'm gonna show you real quick
I gotta take screenshots
okay thank you
okay
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
okay
This is how the Call trace might look like if the breakpoint has not hit. Focus on the bottom panel
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
Could you check the second function shown from the top? The
read(Postal* &pptr)
oneCould you show the
load(const char* filename)
?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.oh
how do i fix that
C++ can be confusing at times
im dying
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 functionoh
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 referenceso just Postal p;
Yeah
ok
TY for the call stack tip. I didn't know about that.
I might hate Visual Studio for being bloaty, but it does have a lot of convenient features that ease debugging.
its saying my read function in load is wrong
What do you mean?
its saying instance of this function
no*
You forgot to update the definition and prototype of the read() function
omgggg
how would i delete this
I think you can delete that line
okay
do you know what this assertion error is
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
i added the plus 1 on the first line in the if
That only counts when you are counting the length, but if you are going to allocate the array, you need 4
should i remove the plus 1?
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
okay
I apologise if I wasn't clear enough
no its okay
its still the same even when i changed it to 4
thank you for all your help btw!
I think the format string in the fscanf might be a little bit weird
I think it has to be
"%[^,]s"
, maybe?the format of the file is A0B,45769\n
Oh, I'm talking about of line
readPostalCode = (fscanf(fptr, "%[^,],", postalString) == 1)
I think the string "%[^,],"
should be "%[^,]s"
oh that worked thank you!
wouldn't those reads have to be inside the loop though
or you will only read the first one
i am using both read functions from part 1