Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
83 user(s) are online (67 user(s) are browsing Forums)

Members: 0
Guests: 83

more...

Support us!

Headlines

 
  Register To Post  

gcc gnu C library Reference Manual
Just can't stay away
Just can't stay away


See User information
It took me some time to find out that the documentation of the newlib library is probably the document above.
I found it at
http://www.codesourcery.com/sgpp/lite/arm/portal/doc714/libc.pdf

I did not find however what the 'restrict' in the function below mean.

char strtok (char *restrict newstring, const char *restrict delimiters )

???

and in case this has no bearing on this try:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
char *ptr "THIS=that";
  
char ttname[100], ttvalue[100];
  
char *ttnm ttname;
  
char *ttval ttvalue;

  const 
char *delimiters "=";
  
  
ttnm strtok (ptrdelimiters);
  
printf("ToolType = %s\\n"ttnm);
  
ttval strtok (NULLdelimiters);    
  
printf("ToolType = %s\\n"ttval);


  return 
0;
}


Why does this crash? (no error or warning)




Edited by JosDuchIt on 2011/7/19 20:31:54
Go to top
Re: gcc gnu C library Reference Manual
Not too shy to talk
Not too shy to talk


See User information
You should check that you don't print a NULL value with %s.

Arrays ttname and ttvalue are useless here.
Pointers ttnm and ttval are initialized but are overwritten just after, receiving results of strtok.

Go to top
Re: gcc gnu C library Reference Manual
Just can't stay away
Just can't stay away


See User information
I added debug printing (neither the printf Printf or DebugPrintF appear in the shell from which the programwas launched. Using
DumpDebugBuffer allowed me still to check where the crash appeared:

it is the first use of strtok, the
DebugPrintF("strtok done\n"); following immediately after the call was not executed.


I could be that the second call returns a NULL pointer instead of "that" , since "that" is not followed by a delimiter, but i don't see why the first call would fail to recognise "THIS"

Except for the mysterious 'restrict' qualifiers in the strtok definition i don't see what is wrong.


Go to top
Re: gcc gnu C library Reference Manual
Just popping in
Just popping in


See User information
The restrict keyword is to do with the location that the pointer points at. Basically restrict tells the compiler that no other pointer is going to point at that memory block, so the complier can do more optimizations. See Wikipedia - restrict keyword. As for the problem you're having what happens if you do explicit checks for NULL returns from strtok?

Go to top
Re: gcc gnu C library Reference Manual
Supreme Council
Supreme Council


See User information
According to http://sourceware.org/newlib/libc.html#strtok
there is no restrict qualifier for this command.

Simon

Comments made in any post are personal opinion, and are in no-way representative of any commercial entity unless specifically stated as such.
----
http://codebench.co.uk
Go to top
Re: gcc gnu C library Reference Manual
Just can't stay away
Just can't stay away


See User information
@billyfish

my first call to strtok now looks like
char ttname[100], ttvalue[100];
char *ttnm ttname;
char *ttval ttvalue;
char *ptr "THIS=that@";
const 
char *delimiters "=@";
DebugPrintF("D_Bug: delimiters = %s\n"delimiters); 
if (
strtok (ptrdelimiters)) DebugPrintF("D_bug:strtok done\n");
else 
DebugPrintF("D_bug:strtok returned NULL\n");


delimiters are printed OK, neither of the 2 other DebugPrintF's appear.
The GrimReapers crashlog seems to be the same as before

Go to top
Re: gcc gnu C library Reference Manual
Just popping in
Just popping in


See User information
@JosDuchIt

You are trying to tokenize a constant string, which is impossible since strtok() will modify the string. The crash you are getting is because strtok() is trying to write to read-only memory. The crash log, which you never made public here, should have exposed this.

The man pages included in Ubuntu Linux contain these warnings for strtok():

Quote:
Be cautious when using these functions. If you do use them, 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.

Go to top
Re: gcc gnu C library Reference Manual
Just popping in
Just popping in


See User information
@tboeckel

Well spotted. Changing from a "char *" to a "char []" should fix it. Something like:


char str [] = "THIS=that@";
const char *delimiters = "=@";
char *ptr;
printf ("delimiters = %s\n", delimiters);

ptr = strtok (str, delimiters);
while (ptr)
{
printf ("strtok returned %s\n", ptr);

ptr = strtok (NULL, delimiters);
}

fflush (stdout);

Go to top
Re: gcc gnu C library Reference Manual
Just can't stay away
Just can't stay away


See User information
@tboeckel and billyfish

Thanks.

In the mean time i had used strcpy to copy ptr to copyptr and discovered the perror() function.

Alas: using perror("strtok") i am told "function not implemented."

My next question is then : what functions are implemented in the Amiga OS4 NewLib? Is there a method to generate this list ?

I am still going to test billyfish'es solution adding perror("strtok")

Tested and was OK, seems that strtok() exists after all.


My "next" question above remains however.





Edited by JosDuchIt on 2011/7/21 14:48:17
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project