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.
int main()
{
char *data_ptr ;
vector <string> expr ;
char buf [64] ;
//this causes a segmentation fault
data_ptr = strtok(tree, " ");
//this works fine
strcpy(buf, tree);
data_ptr = strtok(buf, " ");
return 0 ;
}
Its not a crtical requirement to solve this but its strange. I don't see why strtoking the static memory should be a problem. I'd appreciate it if someone could shed some light on the issue?
The problem is not static storage, but constant strings.
From the man page for strtok(3) and strtok_r(3):
Code:
Never use these functions. If you do, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
The identity of the delimiting character is lost.
The strtok() function uses a static buffer while
parsing, so it's not thread safe. Use strtok_r() if
this matters to you.
The reason these functions cannot be used on constant strings (the second item in the above list) is that they modify their first argument (the first item). In many implementations of C, constant strings are stored in a read-only memory segment.
ok, right, I had thought that constant strings were meant in the compiler context. I did not know that such memory was stored in a read only segment (or that it was even possible to force that distinction onto your own data, although I suppose there is mprotect).
I think that char * tree = "foo" is a pointer to read only memory, thus the contents can be read but not changed. Whereas char[] tree = "bar" is an address in read only memory, whose contents the element bar[0] bar[1] and bar[2] are not located in the read only region.
nope, i'd say that the whole segment is marked as read only, then the individual elements will also be read only. I'd suspect that the particular compiler implementation is responsible.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.