sprintf 会自发失败,具体取决于 printf 和 NSLog 调用的内容

发布于 2024-09-16 20:24:04 字数 1145 浏览 4 评论 0原文

您好,我在 sprintf 方面遇到了一个奇怪的问题。这是我的代码:

void draw_number(int number,int height,int xpos,int ypos){
    char string_buffer[5]; //5000 is the maximum score, hence 4 characters plus null character equals 5
    printf("Number - %i\n",number);
    sprintf(string_buffer,"%i",number); //Get string
    printf("String - %s\n",string_buffer);
    int y_down = ypos + height;
    for (int x = 0; x < 5; x++) {
        char character = string_buffer[x];
        if(character == NULL){ //Blank characters occur at the end of the number from spintf. Testing with NULL works
            break;
        }
        int x_left = xpos+height*x;
        int x_right = x_left+height;
        GLfloat vertices[] = {x_left,ypos,x_right,ypos,x_left,y_down,x_right,y_down};
        rectangle2d(vertices, number_textures[atoi(strcat(&character,"\0"))], full_texture_texcoords);
    }
}

通过 printf 调用,数字已成功打印,并且数字按预期绘制。当我把它们拿走时,我当然无法查看输出并进行比较,但数字无法正确呈现。我认为 sprintf 会以某种方式中断。

NSLog 也会发生这种情况。在程序中的任何位置添加 NSLog 可能会破坏或修复该功能。

到底发生了什么事?

这是使用 Objective-C 和 iOS 4 SDK。

感谢您的任何答复。

Hello I have a bizarre problem with sprintf. Here's my code:

void draw_number(int number,int height,int xpos,int ypos){
    char string_buffer[5]; //5000 is the maximum score, hence 4 characters plus null character equals 5
    printf("Number - %i\n",number);
    sprintf(string_buffer,"%i",number); //Get string
    printf("String - %s\n",string_buffer);
    int y_down = ypos + height;
    for (int x = 0; x < 5; x++) {
        char character = string_buffer[x];
        if(character == NULL){ //Blank characters occur at the end of the number from spintf. Testing with NULL works
            break;
        }
        int x_left = xpos+height*x;
        int x_right = x_left+height;
        GLfloat vertices[] = {x_left,ypos,x_right,ypos,x_left,y_down,x_right,y_down};
        rectangle2d(vertices, number_textures[atoi(strcat(&character,"\0"))], full_texture_texcoords);
    }
}

With the printf calls there, the numbers are printed successfully and the numbers are drawn as expected. When I take them away, I can't view the output and compare it, of-course, but the numbers aren't rendering correctly. I assume sprintf breaks somehow.

This also happens with NSLog. Adding NSLog's anywhere in the program can either break or fix the function.

What on earth is going on?

This is using Objective-C with the iOS 4 SDK.

Thank you for any answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

故事和酒 2024-09-23 20:24:04

好吧,这段代码肯定很奇怪,

char character = string_buffer[x]; 
...
... strcat(&character,"\0") ...

最初我在想,根据堆栈上何时出现 NUL 终止符,这会破坏一些内存,并可能导致您的问题。但是,由于您要附加空字符串,我认为它不会产生任何效果。

也许堆栈的内容实际上包含 atoi 正在解释的数字?无论哪种方式,我建议您修复该问题并看看它是否可以解决您的问题。

至于如何解决这个问题,乔治·弗里茨切(Georg Fritzsche)先于我。

Well this bit of code is definately odd

char character = string_buffer[x]; 
...
... strcat(&character,"\0") ...

Originally I was thinking that depending on when there happens to be a NUL terminator on the stack this will clober some peice of memory, and could be causing your problems. However, since you're appending the empty string I don't think it will have any effect.

Perhaps the contents of the stack actually contain numbers that atoi is interpretting?Either way I suggest you fix that and see if it solves your issue.

As to how to fix it Georg Fritzsche beat me to it.

铁轨上的流浪者 2024-09-23 20:24:04

使用 strcat(&character,"\0") 您尝试使用单个字符作为字符数组。这可能会导致 atoi() 返回与您期望的值完全不同的值(因为您没有空终止)或者只是崩溃。

要修复原始方法,您可以使用适当的以零结尾的字符串:

char number[] = { string_buffer[x], '\0' };
// ...
... number_textures[atoi(number)] ...

但更简单的是简单地使用以下内容:

... number_textures[character - '0'] ...

With strcat(&character,"\0") you are trying to use a single character as a character array. This will probably result in atoi() returning completely different values from what you're expecting (as you have no null-termination) or simply crash.

To fix the original approach, you could use proper a zero-terminated string:

char number[] = { string_buffer[x], '\0' };
// ...
... number_textures[atoi(number)] ...

But even easier would be to simply use the following:

... number_textures[character - '0'] ...
仄言 2024-09-23 20:24:04

不要使用 NULL 与字符进行比较,请使用 '\0',因为它是您要查找的字符。另外,你的代码注释听起来很惊讶,当然字符串末尾会出现“\0”,这就是 C 终止字符串的方式。

如果您的数字大于 9999,则会出现缓冲区溢出,这可能会导致不可预测的影响。

Don't use NULL to compare against a character, use '\0' since it's a character you're looking for. Also, your code comment sounds surprised, of course a '\0' will occur at the end of the string, that is how C terminates strings.

If your number is ever larger than 9999, you will have a buffer overflow which can cause unpredicable effects.

叫思念不要吵 2024-09-23 20:24:04

当您遇到此类问题时,请立即想到堆栈或堆损坏。您应该动态分配足够大小的缓冲区 - 将其设置为固定大小会带来这种麻烦。因为您没有检查该数字是否在最大值内,如果您遇到另一个错误导致该数字高于最大值,您就会在这里遇到此问题。

When you have that kind of problem, instantly think stack or heap corruption. You should dynamically allocate your buffer with enough size- having it as a fixed size is BEGGING for this kind of trouble. Because you don't check that the number is within the max- if you ever had another bug that caused it to be above the max, you'd get this problem here.

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