sprintf 弄乱了我的指针和数据?
char purl[1024];
unsigned char * puMember;
unsigned char uMember;
puMember = &uMember;
unsigned char * pMember;
unsigned char Member;
pMember = &Member;
printf("member: %s\n",pMember);
printf("username :%s\n",puMember);
sprintf(purl, "Username: %s %s",puMember,pMember);
printf("URL:%s\n",purl);
printf("member: %s\n",puMember);
printf("username :%s\n",pMember);
输出
member: 12345678
username :User1
URL: username: cUser1 ser1
member: User1
username :ser1
我很困惑为什么 sprintf 在执行后会弄乱我的指针。
pMember 有 12345678,puMember 有 User1,我试图将它们格式化为字符串(purl),但似乎 sprintf 把它们弄乱了:/
我可以如何完成此任务有任何帮助吗?
char purl[1024];
unsigned char * puMember;
unsigned char uMember;
puMember = &uMember;
unsigned char * pMember;
unsigned char Member;
pMember = &Member;
printf("member: %s\n",pMember);
printf("username :%s\n",puMember);
sprintf(purl, "Username: %s %s",puMember,pMember);
printf("URL:%s\n",purl);
printf("member: %s\n",puMember);
printf("username :%s\n",pMember);
Output
member: 12345678
username :User1
URL: username: cUser1 ser1
member: User1
username :ser1
Im confused into why sprintf is messing up my pointers once its been executed.
pMember has 12345678 and puMember has User1, im trying to format the them into a string (purl) but it seems that sprintf is messing them up :/
Any help how i could accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码很奇怪 - 如果
pMember
和puMember
指向单个字符,为什么printf
它们作为一个多终止字符串?它会打印任何内容,直到在内存中找到空值为止。同样,
sprintf
将在Member
和uMember
字符之后继续读取内存并填充purl
- 取决于何时在内存中找到一个空字节。使用
snprintf
而不是printf
将避免缓冲区溢出,但是您到底想完成什么?Your code is very strange - if
pMember
andpuMember
point to single chars, whyprintf
them as a mull-terminated string? It will print anything until it finds a null in memory.Likewise,
sprintf
will keep reading memory after theMember
anduMember
chars and fill uppurl
- depending on when it finds a null byte in memory.Using
snprintf
instead ofprintf
will avoid the buffer overrun, but what are you trying to accomplish exactly?uMember
有 1 个字符的空间;'F'
或'4'
或'\0'
puMember
指向该字符。仅该字符:该对象的大小为 1 个字节。Member
有 1 个字符的空间pMember
指向 1 个字符。不,不,不。 pMember 不指向“字符串”。您已经调用了未定义行为:任何事情都可能发生
不,不,不。 puMember 不指向“字符串”。您已经调用了未定义行为:任何事情都可能发生
不,不,不。 puMember 和 pMember 不指向“字符串”。你已经调用了未定义的行为:任何事情都可能发生
不,不,不......
uMember
has space for 1 character;'F'
or'4'
or'\0'
puMember
points to that character. And that character alone: the object is 1 byte in size.Member
has space for 1 characterpMember
points to 1 character.No, no, no. pMember does not point to a "string". You've invoked Undefined Behaviour: anything can happen
No, no, no. puMember does not point to a "string". You've invoked Undefined Behaviour: anything can happen
No, no, no. puMember and pMember do not point to "string"s. You've invoked Undefined Behaviour: anything can happen
No, no, no ...
您正在某处覆盖缓冲区。将
sprintf
替换为snprintf
让 C 库准确地知道目标缓冲区中有多少空间,例如:编辑 0:
C 中的字符串以零结尾,这意味着末尾有一个额外的字节具有值
'\0'
的内存块,并且表示字符串结束。这就是 C 库对strlen
和strcpy
等字符串函数以及所有printf
的%s
格式说明符的假设s。否则,这些库函数将在内存中运行,直到找到零值字节或操作系统杀死您的程序以访问未映射的内存。You are overwriting a buffer somewhere. Replace the
sprintf
withsnprintf
to let the C library know exactly how much space you have in the target buffer, e.g:Edit 0:
Strings in C are zero-terminated, meaning there's one extra byte at the end of the memory chunk that has value
'\0'
, and that signals end of the string. That what C library assumes for string functions likestrlen
andstrcpy
, and for%s
format specifier for allprintf
s. Otherwise these library functions would run through the memory till either zero-value byte is found or OS kills your program for access to unmapped memory.