How can I tell if a string is null terminated in Eclipse and gdb? Under Debug for “Default”, if I just see the text I want to store in quotes I can assume it is properly null terminated?
Also, what’s the best way to copy a string to make sure that it is properly terminated?
How can I tell if a string is null terminated in Eclipse and gdb?
Share
Use strncpy to be safe, this allows you to specify a max length just in case its not terminated. It’s kind of tricky to check but yes if in the debugger it shows correctly then it’s properly terminated.
Edit: Should have made it clear that it only makes it safe in the sense that the function call won’t produce weird results if you specify a length, you must still manually terminate the string.
In general the only time it won’t be null terminated is due to programmer error as I believe all functions in the C library return properly terminated strings, possible errors can be forgetting the extra byte for the terminator. I’m hard pressed to think of times when an issue with strings that I’ve encountered has been because of a failed null terminator except maybe custom manipulation functions.