Better malloc practice, plus several missing warnings

This commit is contained in:
Julie Marchant 2019-05-23 11:25:54 -04:00
parent b251acb4fe
commit d17a8f36c7
9 changed files with 36 additions and 13 deletions

View File

@ -1187,10 +1187,10 @@ int alien_add()
int *alienArray;
int numberOfAliens = 1;
alienArray = malloc(8 * sizeof(int));
alienArray = malloc(8 * sizeof(*alienArray));
if (alienArray == NULL)
{
engine_warn("WARNING: Failed to allocate memory for aliens");
engine_warn("Failed to allocate memory for aliens");
return 0;
}

View File

@ -34,9 +34,12 @@ void bullet_add(Object *theWeapon, Object *attacker, int y, int dy)
int imageIndex;
int tempX, tempY, steps;
bullet = malloc(sizeof(Object));
bullet = malloc(sizeof(*bullet));
if (bullet == NULL)
{
engine_warn("Failed to allocate memor for bullet");
return;
}
if (attacker == &player)
game.shots++;

View File

@ -37,6 +37,7 @@ Create a new collectable item based on supplied arguments.
void collectable_add(float x, float y, int type, int value, int life)
{
int r;
Collectable *collectable;
if (type == P_ANYTHING)
{
@ -143,7 +144,7 @@ void collectable_add(float x, float y, int type, int value, int life)
}
}
Collectable *collectable = malloc(sizeof(Collectable));
collectable = malloc(sizeof(*collectable));
if (collectable == NULL)
{
engine_warn("Failed to allocate memory for collectable");

View File

@ -60,7 +60,7 @@ void engine_init()
engine.smx = 0;
engine.smy = 0;
engine.bulletHead = malloc(sizeof(Object));
engine.bulletHead = malloc(sizeof(*engine.bulletHead));
if (engine.bulletHead == NULL)
{
engine_error("Failed to allocate memory for bullet head.");
@ -68,7 +68,7 @@ void engine_init()
engine.bulletHead->next = NULL;
engine.bulletTail = engine.bulletHead;
engine.explosionHead = malloc(sizeof(Object));
engine.explosionHead = malloc(sizeof(*engine.explosionHead));
if (engine.explosionHead == NULL)
{
engine_error("Failed to allocate memory for explosion head.");
@ -76,7 +76,7 @@ void engine_init()
engine.explosionHead->next = NULL;
engine.explosionTail = engine.explosionHead;
engine.collectableHead = malloc(sizeof(Collectable));
engine.collectableHead = malloc(sizeof(*engine.collectableHead));
if (engine.collectableHead == NULL)
{
engine_error("Failed to allocate memory for collectable head.");
@ -84,7 +84,7 @@ void engine_init()
engine.collectableHead->next = NULL;
engine.collectableTail = engine.collectableHead;
engine.debrisHead = malloc(sizeof(Object));
engine.debrisHead = malloc(sizeof(*engine.debrisHead));
if (engine.debrisHead == NULL)
{
engine_error("Failed to allocate memory for debris head.");

View File

@ -33,7 +33,9 @@ to change frames on a 21, 14, 7 basis.
*/
void explosion_add(float x, float y, int type)
{
Object *explosion = malloc(sizeof(Object));
Object *explosion;
explosion = malloc(sizeof(*explosion));
if (explosion == NULL)
{
engine_warn("Failed to allocate memory for explosion.");

View File

@ -233,7 +233,12 @@ static void game_addDebris(int x, int y, int amount)
for (int i = 0 ; i < amount ; i++)
{
debris = malloc(sizeof(Object));
debris = malloc(sizeof(*debris));
if (debris == NULL)
{
engine_warn("Failed to allocate memory for debris");
return;
}
debris->next = NULL;
debris->x = x;

View File

@ -44,7 +44,7 @@ SDL_Surface *gfx_messageBox;
void gfx_init()
{
screen_bufferHead = malloc(sizeof(LinkedRect));
screen_bufferHead = malloc(sizeof(*screen_bufferHead));
if (screen_bufferHead == NULL)
{
engine_error("Failed to allocate memory for buffer head.");

View File

@ -56,7 +56,14 @@ void screen_drawBackground()
void screen_addBuffer(int x, int y, int w, int h)
{
LinkedRect *rect = malloc(sizeof(LinkedRect));
LinkedRect *rect;
rect = malloc(sizeof(*rect));
if (rect == NULL)
{
engine_warn("Failed to allocate memor for screen buffer");
return;
}
rect->next = NULL;
rect->x = x;

View File

@ -663,7 +663,12 @@ void title_showCredits()
// FIXME: It would be nice for the size of this array to be determined
// by the number of lines in the text file. I'm not sure how to do
// that at the moment, so just giving it a very large number for now.
credit = malloc(sizeof(TextObject) * 300);
credit = malloc(300 * sizeof(*credit));
if (credit == NULL)
{
engine_warn("Failed to allocate memory for credits");
return;
}
while (fscanf(fp, "%d %[^\n]%*c", &yPos, text) == 2)
{