ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
While trying to understand some code for work I came across something I was initially puzzled over, but now have a slight understanding as to what the original author intended to do:
Now, what I think the intention is, is to artificially end the specified string at the last location of a period, if found.
However, what can possibly be going wrong with the '\000' literal? The code compiles without warnings (even with -Wall). When I run this it segfaults assigning that literal (run on a Solaris 10 box with gcc 3.4.3). Is this a NUL-byte (value 0) followed by two zero characters (value 30)? From what I can tell the code is pumping 3 bytes into a space allocated to hold only one byte. The literal seems to be okay with the compiler, but not when assigning it to the address pointed to by 'ptr'.
Ok, somebody check me on this, it's been a while since C...
Your assumption is good, the \000 is an octal literal for a one byte 0 which will end the string. I think the problem is the quote marks. By sitting in the quotes it translates to three character bytes instead of one. Try assigning the value as some sort of int. One of the following...
Oh that's interesting. I hadn't thought of octals for this... See I thought '\000' would translate to 0x003030 (NUL-byte, zero, zero). My task wasn't to clean up the code but to band-aid it.. And it is 8 years old with the original author gone, so I didn't want to touch the rest of it.
So,
Code:
*ptr = '\0'; // and
*ptr = 0;
are the same, correct? Since the decimal 0 is typecast into a single byte 0 before assigned to the location at ptr?
I can't really ask the person who wrote this what the heck they intended to do.. either use an octal value like you mentioned, or write two more zeros, or...
My confusion is only
Code:
'\000';
should not be valid syntax, disregarding semantics. Am I wrong?
Yes. Whenever you have single quote-marks, you will only get one character, period (with width of int in C and width of char in C++).
Quote:
Originally Posted by student04
When I run this it segfaults assigning that literal (run on a Solaris 10 box with gcc 3.4.3).
If the segfault does indeed occur on the assignment (*ptr = '\000';), it is most likely because the original string (some_string) was in read-only memory. This is where you should direct your debugging efforts. In particular, if some_string is a (pointer to a) string literal (and not an array initialized to a string literal), many older compilers (wrongly) allowed this to work, whereas most modern compilers put string literals in read-only memory. For example, the following program would have worked on an older compiler, but should segfault with a newer compiler:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.