#How to read errors properly?

23 messages · Page 1 of 1 (latest)

storm cape
#

Hey everyone hope you're having a good day.
I'm using cpp to solve leetcode problems. I often times get these kinds of errors,I know something is going out of bound but the error doesn't specifies which line it could posibly be.

It would be really helpful if you could share your process of interpreting the error in order to find bugs in program.

/usr/include/c++/15.2.1/bits/stl_vector.h:1263: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = int; _Alloc = std::allocator<int>; reference = int&; size_type = lon
g unsigned int]: Assertion '__n < this->size()' failed.

[Process exited 134]

For ex this is my most recent cpp error. From what i can gather from this error is that error's happening somewhere i'm using vector and finding size of it fails however i'm not sure why calculating size would fail . i mean its an inbuilt function ...

night brambleBOT
#

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

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

storm cape
#

How to read errors properly?

pulsar bluff
#

Not sure if you have it in LC, but you may be able to look up the call stack at the time of the assertion

#

std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type)
This tells you what's being called. In this case, the index operator on a std::vector<int>

#

Assertion '__n < this->size()' failed.
This tells you what went wrong. You're 100% correct on it being out of bounds

#

You don't get the line causing it without a stack trace, which is an issue with LC and other online platforms

#

you could try using .at(i) instead of [i], which throws an exception instead of using an assertion, and that may give you more info

storm cape
#

I actually solve problems locally since i prefer local editor. so the above error output is happening locally. I never see any stack trace.

storm cape
#
g++ -g -std=c++17 -o target %

this is the command i use for compiling cpp code.

silk relic
#

operator[] = the index operator. The std::vector<...>:: before it means it belongs to vector

storm cape
#

oh

lavish jacinth
storm cape
#

codelldb . but is compilation dependent on debugger ?

lavish jacinth
#

no, but whether your debugger stops and lets you see the stacktrace before exiting could be

#

you should be able to do b abort in the lldb console to set a breakpoint at the start of the abort function which is called when asserts fail. from there you can check the backtrace

storm cape
#

i see.haven't used the lldb console so far.will try it.

silk relic
bitter totem
#

I feel like you just need to read the error slowly and carefully. Resist the urge to make it go away as fast as possible and try to figure out what it is saying.
In this specific example you think it's a problem with finding size, but it doesn't say "finding" anywhere.

#

This is much more difficult than it sounds, but it can't be helped. Careful reading of error messages and documentation is just something you have to train over time.

#

It also helps when you figured out an error message before and remember the solution. "Assertion '__n < this->size()' failed." isn't super clear, but in connection with std::vector it'll be clear next time that it's an out of bounds access. You'll see every error message eventually and then it gets easier.

fallow wadi
#

that just good thing to do while programming

I feel like you just need to <do it> slowly and carefully.