0

I'm working on a game project which should be similar to a street fighter game. The problem is that when I try to add new variables to the struct player which is inside a Abstract Data Type it crashes or the program doens't work propertly.

struct player {
//int neueVariable;
//int health;
float frameTime;
bool left;
float moveSpeed;
int curDoing;
int textureHeight;
int textureWidth;
SDL_Texture *texture;
SDL_Rect playerRect;
SDL_Rect playerPosition;

When I try to define the integer health it still works but some animations which are not connected to this abstract data type don't work anymore. So I guess it's some kind of a memory leak even though I have no Idea what it could be. But when I define the integer neueVariable and health (so both) it just crashes. Nothing works.

If you need some special code just tell me because I don't know what code you need because I have no clue what can cause it.

Im using the SDL library by the way.

#include "SDL.h" 
#include "SDL_Image.h"

PPlayer player_create() {
PPlayer ply = (PPlayer) malloc(sizeof(struct player));
//player_setHealth(ply,100);


 //player_setDead(ply,0);
    player_setMoveSpeed(ply,0);
    player_setCurDoing(ply,0);
    player_setTextureHeight(ply,0);
    player_setTextureWidth(ply,0);
    player_setPlayerPositionX(ply,0);
    player_setPlayerPositionY(ply,0);
    player_setPlayerPositionW(ply,0);
    player_setPlayerPositionH(ply,0);

    player_setPlayerRectX(ply,0);
    player_setPlayerRectY(ply,0);
    player_setPlayerRectW(ply,0);
    player_setPlayerRectH(ply,0);

    return ply;
}


void player_setTextureHeight(PPlayer player, int value){
    player->textureHeight = value;
}

in main for example

player_setTextureHeight(player1,rin_textureHeight);

rin_textureHeight is defined with int rin_textureHeight = 13600;

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Dagun
  • 1
  • 1
  • Hard to say without seeing the code allocating / copying / accessing objects of type `struct player`; do you use some hard-coded `malloc(40)`, some `memcpy`, some "hand-written" pointer arithmetics; do you miss compiling dependent libraries? ... Really hard to say... – Stephan Lechner Jan 06 '18 at 22:29
  • Having to guess, since you don't show any details of the failures, I would look at where you have pointers and check that they are valid, i.e. point to properly allocated and initialised data areas. If you are copying, as @StephanLechner suggests, note that copying pointers only copies the address, you can have two structs pointing at the same single object. – cdarke Jan 06 '18 at 22:34
  • I updated the code. Im allocating it via malloc(sizeof...)) – Dagun Jan 06 '18 at 22:34
  • 2
    look at the `texture` field. Since it's C, you couldn't have allocated or set `texture` anywhere. So it's just a dangling pointer in your structure. can you show us the code for `player_setTextureHeight` for instance? – Jean-François Fabre Jan 06 '18 at 22:35
  • I've sent the code – Dagun Jan 06 '18 at 22:40
  • The code has been updated. – Dagun Jan 06 '18 at 23:18
  • 1
    Try running under `valgrind`. What you describe sounds as if you are overwriting something somewhere. – Jongware Jan 07 '18 at 01:27
  • 1
    Assuming `PPlayer` is a `typedef`ed pointer, you may want to read [Is it a good idea to **typedef** pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). (which may very well be the source of (or masking) your problem) – David C. Rankin Jan 07 '18 at 02:12
  • It seems C++ syntax bool doesn't support in C – Suvam Roy Jan 07 '18 at 05:05
  • Learn [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Compile [with](https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html) `gcc -Wall -Wextra -g` (all warnings & debug info) and improve your code to get no warnings at all. Then [use the `gdb` debugger](https://sourceware.org/gdb/current/onlinedocs/gdb/) to understand the behavior of your program. Be [scared](https://stackoverflow.com/a/46526702/841108) of undefined behavior. – Basile Starynkevitch Aug 06 '18 at 05:42

0 Answers0