Re: strtok causes Segmentation fault



bofh1234@xxxxxxxxxxx wrote:
I understand strtok seg faulting if the FIRST call is with NULL, but
in my case the first call is not NULL. It is
tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?
list is a varible passed to the function.
You are passing a string literal as the list parameter, which you then
send to strtok(). This causes undefined behavior.

So?? I tried strtok(*list, ";"); and it still seg faults. According
to my old C book strtok is supposed to take a string for the first
argument. I even tried changing createvarible to:
int createvarible(const int sock, char *varname, char *vartype, char
*initialvalue, char accesslist[64])
and I still get a seg fault. How can I get this thing to work.

Think:

char *strtok(char *s1, const char *s2);

The string [s1] passed to strtok() must be mutable, because it is a destructive function. Your appear to be passing the function a static buffer or literal string.

Slow down a bit and re-read all the comments posted so far.

Consider:

[...]
char *fnord = "Fnord Motor Company";
fnord[0] = 'f'; /* Undefined behaviour */
[...]

Can you legally modify fnord, even with a direct assignment? If not, what about a library function like strtok()?
.



Relevant Pages

  • [PATCH 09/21] perf: rewire generic library stuff, p5
    ... +int eprintf(int level, const char *fmt, ...) ... * Helper function for splitting a string into an argv-like array. ... +static int count_argc(const char *str) ...
    (Linux-Kernel)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: Returning a character buffer from a DLL
    ... I need to return a string buffer from the DLL in a RunQuery function. ... I find it odd that you are using the obsolete 'char *' data type here. ... want to use a string pointer of any type here! ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: Whats going on with strtok
    ... that the bug isn't in strtok() ... ... In the code, I was using a local, automatic, char array to read in the ... memory to a pointer in the structure for the string and copy it in. ... char *buff = NULL; ...
    (comp.unix.programmer)