LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2007, 07:58 AM   #1
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
seg fault when strtok on static memory c++


hello,
I am observing seg faults when I try to do a strtok on static memory. The code is as follows :

#include <iostream>
#include <string>
#include <string.h>
#include <vector>
using namespace std ;

char *tree = "B C H I I I";

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?

Thanks in advance.
 
Old 03-14-2007, 12:06 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.

Hope this helps.
 
Old 03-15-2007, 07:43 AM   #3
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
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).

Thanks for your insight.
 
Old 04-29-2007, 06:36 PM   #4
NetCrawler
LQ Newbie
 
Registered: Feb 2007
Posts: 12

Rep: Reputation: 0
For some reason unknown to me, if you declare your static string like this:

char tree[] = "B C H I I I";

It runs through strtok() fine with no segmentation fault. I guess this definition causes the string to be placed in read/write memory.

I would have thought that both definitions were the same thing, I guess I'd be wrong...
 
Old 04-29-2007, 09:19 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-30-2007, 11:04 AM   #6
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
seg fault vm_devadas Linux - Enterprise 1 12-05-2006 02:28 PM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
free memory from strtok? swinchen Programming 1 09-08-2004 11:01 PM
ut2004 seg fault dopefish Linux - Games 3 04-11-2004 02:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration