简体   繁体   English

在 C 中使用 strcpy、strcat、sprintf 等后 SDL_CreateTextureFromSurface 崩溃

[英]SDL_CreateTextureFromSurface crashes after using strcpy, strcat, sprintf, etc. in C

I want to write some text on the screen.我想在屏幕上写一些文字。 For this purpose I'm using this code:为此,我正在使用以下代码:

TTF_Font* font = TTF_OpenFont("arial.ttf", 15);
SDL_Color text_color = { 255, 255, 255 };
SDL_Surface* text_surface = TTF_RenderText_Blended_Wrapped(font, "inventory", text_color, 100);
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(rend, text_surface);

I have some other stuff below this but that doesn't matter.我在这下面还有一些其他的东西,但这没关系。 Point is this works.重点是这行得通。 Now instead of it saying "inventory" all the time I want to have a string to control that.现在我不想一直说“库存”,而是想要一个字符串来控制它。 This is what I put before the above code:这是我在上面的代码之前放的:

char inv_string[] = "";
char items[15][10] = { "air", "cloud", "water", "wood   ", "leaves ", "in_cave", "grass  ", "dirt   ", "stone  ", "coal   ", "iron   ", "gold   ", "diamond", "player" };
if (item_count <= 0) strcpy(inv_string, "your inventory is empty\n");
if (item_count > 0) sprintf(inv_string, "> %s %d <", items[inventory->item], inventory->amount);
if (item_count > 1) sprintf(inv_string + strlen(inv_string), "  %s %d\n", items[inventory->next->item], inventory->next->amount);
if (item_count > 2) sprintf(inv_string + strlen(inv_string), "  %s %d\n", items[inventory->next->next->item], inventory->next->next->amount);
if (item_count > 3) sprintf(inv_string + strlen(inv_string), "  %s %d\n", items[inventory->next->next->next->item], inventory->next->next->next->amount);
if (item_count > 4) sprintf(inv_string + strlen(inv_string), "  %s %d\n", items[inventory->next->next->next->next->item], inventory->next->next->next->next->amount);
for (unsigned int i = 5 - item_count; i > 0; i--) sprintf(inv_string + strlen(inv_string), "  empty\n");

And the string contains exactly what I need.该字符串包含我需要的内容。 But now even without replacing "inventory" with inv_string , I get an access violation error at SDL_CreateTextureFromSurface.但是现在即使没有用inv_string替换"inventory" ,我在 SDL_CreateTextureFromSurface 也会遇到访问冲突错误。 Can someone tell me why is this happening and how to fix it?有人能告诉我为什么会发生这种情况以及如何解决吗? Thanks in advance and have a nice day!提前致谢,祝您有美好的一天!

The statement该声明

strcpy(inv_string, "your inventory is empty\n");

will cause undefined behavior, because the memory buffer inv_string only has room for a single byte.将导致未定义的行为,因为 memory 缓冲区inv_string只有一个字节的空间。 However, a size of 25 bytes is required to store the entire string, including the terminating null character.但是,需要 25 个字节的大小来存储整个字符串,包括终止 null 字符。 This causes a buffer overflow .这会导致缓冲区溢出

The function calls to sprintf have the same problem, as they also require the buffer size to be larger than a single byte.sprintf的 function 调用有同样的问题,因为它们还要求缓冲区大小大于单个字节。

The most obvious solution would be to increase the size of inv_string , for example like this:最明显的解决方案是增加inv_string的大小,例如:

char inv_string[200] = "";

An alternative would be to use dynamic memory allocation instead.另一种方法是改用动态 memory 分配。 You could change the line你可以换行

char inv_string[] = "";

to

char *inv_string;

and then make that pointer point to allocated memory of a certain size using malloc .然后使用 malloc 使该指针指向一定大小的已分配malloc If you later determine that you need the memory block to be larger, you can use realloc to request more memory.如果您稍后确定需要更大的 memory 块,您可以使用realloc请求更多的 memory。

If you find the memory management of strings too tedious and complicated in C, then you might want to consider writing your program in C++ instead of C. If you find the memory management of strings too tedious and complicated in C, then you might want to consider writing your program in C++ instead of C. The std::string C++ class handles the memory management of the string for you. std::string C++ class 为您处理字符串的 memory 管理。

I managed to fix it, part of it was the thing you guys point out so thanks for that, the other thing was SDL not being able to find the font file.我设法修复了它,其中一部分是你们指出的,所以谢谢,另一件事是 SDL 无法找到字体文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM