-1

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. Can someone tell me why is this happening and how to fix it? Thanks in advance and have a nice day!

  • 3
    Your `inv_string` has sizeof 1. Make it a bit bigger... – tkausl Jun 06 '21 at 20:03
  • I wonder how many of those `if`s you're going to write as you add new items. – Blindy Jun 06 '21 at 20:14
  • alright, made that 100. crashes again now at TTF_RenderText – CraigsCraig Jun 06 '21 at 20:25
  • I recently did an answer that does TTF rendering. It may help a bit: https://stackoverflow.com/questions/67708206/how-can-i-display-a-sudoku-solution-as-an-image-using-c/67726303#67726303 [P.S. I like your name :-)] – Craig Estey Jun 06 '21 at 21:00

2 Answers2

1

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. However, a size of 25 bytes is required to store the entire string, including the terminating null character. 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.

The most obvious solution would be to increase the size of inv_string, for example like this:

char inv_string[200] = "";

An alternative would be to use dynamic memory allocation instead. 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. If you later determine that you need the memory block to be larger, you can use realloc to request more 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. The std::string C++ class handles the memory management of the string for you.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

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.