strtok() gives segmentation fault!!


 
Thread Tools Search this Thread
Top Forums Programming strtok() gives segmentation fault!!
# 1  
Old 10-15-2011
strtok() gives segmentation fault!!

Code:
#include<iostream.h>
#include<string>
#include<stdio.h>

int main()
{
         char *cmd="delete backup backup-iso image a.iso b.iso c.iso d.iso";
         char *tokenized_cmd,*sub_cmd;

        sub_cmd=strstr(cmd,"image");
        tokenized_cmd=strtok(sub_cmd," ");

        while(tokenized_cmd != NULL)
        {
                tokenized_cmd=strtok(NULL," ");
                if(tokenized_cmd!=NULL)
                {
                        cout<<tokenized_cmd<<" ";
                }
        }

}

The output i expect here is :
a.iso b.iso c.iso d.iso
i.e.,whatever is there after the word "image".

but the command tokenized_cmd=strtok(cmd," ");
is throwing segmentation fault!!Smilie

Any mistakes you could find in the snippet??

Last edited by ashwini.engr07; 10-15-2011 at 02:14 PM..
# 2  
Old 10-15-2011
strtok() needs to be able to modify the buffer it's scanning. As declared, you cannot modify cmd (which sub_cmd references). Use an array instead of a pointer declaration.

For more info: http://c-faq.com/decl/strlitinit.html

Regards,
Alister
# 3  
Old 10-15-2011
Thanks Alister!
But i checked the strtok syntax shows: char * strtok ( char * str, const char * delimiters );

This was a sample program i was trying. In the original code, the cmd is actually passed to a function from elsewhere as const char* which i've done strdup to get char*.

So is there a way to do this by keeping cmd as char*?

any alternate way...

Last edited by ashwini.engr07; 10-15-2011 at 10:53 PM..
# 4  
Old 10-17-2011
Quote:
Originally Posted by ashwini.engr07
Thanks Alister!
But i checked the strtok syntax shows: char * strtok ( char * str, const char * delimiters );
It's the memory that's important, not the labels. Unwritable remains unwritable memory even after you force a 'const char *' pointer into 'char *'. Which is what you are doing by putting "strings" into 'char *' variables, incidentally.

A "string" like that is a const char *, even though you're shoving it into a "char *" pointer.
Quote:
This was a sample program i was trying. In the original code, the cmd is actually passed to a function from elsewhere as const char* which i've done strdup to get char*.

So is there a way to do this by keeping cmd as char*?
You've not done strdup() here, and are trying to modify the original "string" which resides in read-only memory.

Again, 'char *' is just a label. Putting a "string" inside a "char *" does not make it modifiable.

You'll just have to make a duplicate, either with strdup() or by saving into your own local buffer.

Code:
char buf[512];
strcpy(buf, "string");

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Programming

Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me. For example this code #include <stdio.h> main() { unsigned long a=0; unsigned long b=0; int z; { printf("Enter two... (2 Replies)
Discussion started by: sizzler786
2 Replies

3. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

4. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

5. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

6. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

7. Programming

segmentation fault

What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies

8. Programming

segmentation fault

Hi, I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Discussion started by: mind@work
3 Replies

9. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

10. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies
Login or Register to Ask a Question