LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-01-2004, 01:36 PM   #1
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
C seg fault


This code segfaults on the first strtok:
Code:
int mkdirp(char* file)
{
	printf("%s\n", file);
	char* pch;
	pch = strtok(file,"/");
	while (pch != NULL)
	{
		printf("%s\n", pch);
		mkdir(pch, 0777);
		pch = strtok(NULL, "/");
	}
	return 0;
}
I have tried calling it with file = "newdir" and file = "mydir/subdir" - neither work.
 
Old 10-01-2004, 01:51 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
You can't use strtok() on string constants according to the man page:
Code:
BUGS
       Never use these functions. If you do, note that:

              These functions modify their first argument.

              These functions cannot be used on constant strings.
So if main() looks like:
Code:
int main(void)
{
  mkdirp("newdir");
  return 0;
}
...it will crash. But this should work:
Code:
int main(void)
{
  char dirname[] = "newdir";

  mkdirp(dirname);
  return 0;
}
 
Old 10-01-2004, 02:47 PM   #3
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
still fails - this is now the relevant part of my main:
Code:
	char* newdir="newdir";
	mkdirp(newdir);
	printf("made newdir\n");
	char* mydir="mydir/subdir";
	mkdirp(mydir);
	printf("made mydir/subdir\n");
	char* newdir2="newdir2/subdir";
	mkdirp(newdir2);
	printf("made newdir2/subdir\n");
since it said i shouldnt use it, is there an alternative to using this (other than making the behaviour myself)?

Last edited by drigz; 10-01-2004 at 02:50 PM.
 
Old 10-01-2004, 03:06 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
This:
Code:
char* newdir="newdir";
...is not the same as...
Code:
char dirname[] = "newdir";
...like I suggested.

It's still segfaulting for you because you're still using a string constant. Use my suggestion and it will work fine.
 
Old 10-01-2004, 03:14 PM   #5
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
really? oh well - i just gave up and wrote it myself (turns out i forgot something and it was easier to write it myself)

thanks.
 
Old 10-01-2004, 03:35 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Yeah, string constants such as "newdir" go into read-only memory. So when you do char *name = "newdir"; you're just creating a pointer that points to read-only memory, but when you do char name[] = "newdir"; you're creating an array and copying the contents of "newdir" into the array. The memory for the array is not in read-only memory.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 on fedora only dh0104 Programming 3 03-26-2005 12:57 PM
RH 7.2 crash...getting seg fault when using ls sakima Red Hat 1 10-11-2004 09:14 AM
seg fault on fclose (); lackluster Programming 4 06-28-2003 07:30 PM
gnome_error_dialog seg fault?? Castro Programming 3 06-11-2003 01:28 PM
gnome_app_create_menus seg fault in RH 7.3 Castro Programming 0 05-30-2003 08:46 PM


All times are GMT -5. The time now is 01:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration