64 位机器上的 strtok
以下代码在 64 位和 32 位上的工作方式不同,这给我移植代码带来了麻烦。
char * tmp = "How are you?";
printf("size of char * = %ld and size of strtok return val = %ld \n",sizeof(char *),sizeof(strtok(tmp," ")));
以下是输出:
32 bit:
size of char * = 4 and size of strtok return val = 4
64 bit:
size of char * = 8 and size of strtok return val = 4
strtok 的手册页说:
#include <string.h>
char *strtok(char *str, const char *delim);
RETURN VALUE
The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens.
64 位机器上的 char* 应该是打印时的 8 个字节。那么为什么 strtok 在 64 位机器上返回 4 字节 char 指针呢?
谢谢
The following code works differently on 64 bit and on 32 bit which is causing me trouble to port my code.
char * tmp = "How are you?";
printf("size of char * = %ld and size of strtok return val = %ld \n",sizeof(char *),sizeof(strtok(tmp," ")));
Following is the output:
32 bit:
size of char * = 4 and size of strtok return val = 4
64 bit:
size of char * = 8 and size of strtok return val = 4
The man page of strtok says:
#include <string.h>
char *strtok(char *str, const char *delim);
RETURN VALUE
The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens.
The char* on a 64 bit machine is supposed to be 8 bytes as printed. So why is strtok returning a 4 bytes char pointer on a 64 bit machine??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您忘记了
#include
。这导致编译器推断出默认返回类型
int
。通过 #include 正确的头文件,正确的原型被拉入范围。这解决了我在 gcc 上的问题。如果不适合您,您使用什么编译器?
You forgot to
#include <string.h>
.This is causing the default return type of
int
to be inferred by the compiler. By #including the right header file, the correct prototype is pulled into scope.This solves the problem for me on gcc. If it doesn't for you, what compiler are you using?
调用
strtok(tmp, " ")
会导致未定义的行为,因为它会尝试修改tmp
指向的字符串文字 - 但由于的操作数sizeof 不会被评估(有一个例外,此处不适用),这不是问题。
真正的问题是您尝试使用
"%ld"
格式打印size_t
值,这需要unsigned long
参数。如果您的实现支持它,则
size_t
参数的正确格式为"%zu"
(在 C99 中添加):否则,将参数显式转换为适当的大小。我会使用
"%lu"
,因为size_t
是无符号类型。这是一个完整的独立程序,应该在任何 C89 或更高版本的实现上产生预期结果:
编辑:
OP 对另一个答案的评论表明
string.h
标头是问题所在;显然是他而不是
我将这个答案留在这里,因为它描述了需要在OP代码中修复的另一个问题,尽管不是导致观察到的症状的问题。
并且编译器选择了错误的
string.h
头文件。Calling
strtok(tmp, " ")
would cause undefined behavior, since it would attempt to modify the string literal thattmp
points to -- but since the operand ofsizeof
is not evaluated (with one exception that doesn't apply here), that's not an issue.The real problem is that you're trying to print
size_t
values with a"%ld"
format, which requires anunsigned long
argument.If your implementation supports it, the correct format for a
size_t
argument is"%zu"
(added in C99):Otherwise, explicitly convert the arguments to the appropriate size. I'd use
"%lu"
, sincesize_t
is an unsigned type.Here's a complete self-contained program that should produce the expected results on any C89 or later implementation:
EDIT :
The OP's comment on the other answer indicates that the
string.h
header was the problem; apparently he hadrather than
I'm going to leave this answer here because it describes another problem that needs to be fixed in the OP's code, though not the one that caused the observed symptom.
and the compiler picked up the wrong
string.h
header file.