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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
10-01-2004, 01:36 PM
|
#1
|
Member
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407
Rep:
|
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.
|
|
|
10-01-2004, 01:51 PM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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;
}
|
|
|
10-01-2004, 02:47 PM
|
#3
|
Member
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407
Original Poster
Rep:
|
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.
|
|
|
10-01-2004, 03:06 PM
|
#4
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
10-01-2004, 03:14 PM
|
#5
|
Member
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407
Original Poster
Rep:
|
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.
|
|
|
10-01-2004, 03:35 PM
|
#6
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 10:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|