sprintf 弄乱了我的指针和数据?

发布于 2024-11-06 06:41:55 字数 672 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹 2024-11-13 06:41:55

你的代码很奇怪 - 如果 pMemberpuMember 指向单个字符,为什么 printf 它们作为一个多终止字符串?它会打印任何内容,直到在内存中找到空值为止。

同样,sprintf 将在 MemberuMember 字符之后继续读取内存并填充 purl - 取决于何时在内存中找到一个空字节。

使用 snprintf 而不是 printf 将避免缓冲区溢出,但是您到底想完成什么?

Your code is very strange - if pMember and puMember point to single chars, why printf them as a mull-terminated string? It will print anything until it finds a null in memory.

Likewise, sprintf will keep reading memory after the Member and uMember chars and fill up purl - depending on when it finds a null byte in memory.

Using snprintf instead of printf will avoid the buffer overrun, but what are you trying to accomplish exactly?

梦里寻她 2024-11-13 06:41:55
char purl[1024];
unsigned char * puMember;
unsigned char uMember;

uMember 有 1 个字符的空间; 'F''4''\0'

puMember = &uMember;

puMember 指向该字符。仅该字符:该对象的大小为 1 个字节。

unsigned char * pMember;
unsigned char Member;

Member 有 1 个字符的空间

pMember = &Member;

pMember 指向 1 个字符。

printf("member: %s\n",pMember);

不,不,不。 pMember 不指向“字符串”。您已经调用了未定义行为:任何事情都可能发生

printf("username :%s\n",puMember);

不,不,不。 puMember 不指向“字符串”。您已经调用了未定义行为:任何事情都可能发生

sprintf(purl, "Username: %s %s",puMember,pMember);

不,不,不。 puMember 和 pMember 不指向“字符串”。你已经调用了未定义的行为:任何事情都可能发生

printf("URL:%s\n",purl);
printf("member: %s\n",puMember);
printf("username :%s\n",pMember);

不,不,不......

char purl[1024];
unsigned char * puMember;
unsigned char uMember;

uMember has space for 1 character; 'F' or '4' or '\0'

puMember = &uMember;

puMember points to that character. And that character alone: the object is 1 byte in size.

unsigned char * pMember;
unsigned char Member;

Member has space for 1 character

pMember = &Member;

pMember points to 1 character.

printf("member: %s\n",pMember);

No, no, no. pMember does not point to a "string". You've invoked Undefined Behaviour: anything can happen

printf("username :%s\n",puMember);

No, no, no. puMember does not point to a "string". You've invoked Undefined Behaviour: anything can happen

sprintf(purl, "Username: %s %s",puMember,pMember);

No, no, no. puMember and pMember do not point to "string"s. You've invoked Undefined Behaviour: anything can happen

printf("URL:%s\n",purl);
printf("member: %s\n",puMember);
printf("username :%s\n",pMember);

No, no, no ...

指尖凝香 2024-11-13 06:41:55

您正在某处覆盖缓冲区。将 sprintf 替换为 snprintf 让 C 库准确地知道目标缓冲区中有多少空间,例如:

char buffer[32];
snprintf( buffer, 32, "my text: %s\n", some_char_ptr );

编辑 0:

C 中的字符串以零结尾,这意味着末尾有一个额外的字节具有值 '\0' 的内存块,并且表示字符串结束。这就是 C 库对 strlenstrcpy 等字符串函数以及所有 printf%s 格式说明符的假设s。否则,这些库函数将在内存中运行,直到找到零值字节或操作系统杀死您的程序以访问未映射的内存。

You are overwriting a buffer somewhere. Replace the sprintf with snprintf to let the C library know exactly how much space you have in the target buffer, e.g:

char buffer[32];
snprintf( buffer, 32, "my text: %s\n", some_char_ptr );

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 like strlen and strcpy, and for %s format specifier for all printfs. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文