summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2018-12-20 10:55:01 +0000
committerIan C <ianc@noddybox.co.uk>2018-12-20 10:55:01 +0000
commit678ea1bc35c83875ef563b607edab9224971a914 (patch)
tree16d372506fd365d6d2fdca248a74f1e81f4a73a5
parent4cc6d2dad430552a7df8a71bf541742539993fae (diff)
Fixed some memory handling bugs; realloc() of zero bytes.
-rw-r--r--csol.c41
1 files changed, 35 insertions, 6 deletions
diff --git a/csol.c b/csol.c
index 1444616..ad46064 100644
--- a/csol.c
+++ b/csol.c
@@ -86,7 +86,7 @@ static void Shuffle(Deck deck)
{
int f;
- for(f = 0; f < 500; f++)
+ for(f = 0; f < 1000; f++)
{
int c1 = rand() % 52;
int c2 = rand() % 52;
@@ -106,7 +106,7 @@ static void AddToPile(Pile *pile, Card card)
if (!pile->card)
{
- Fatal("Failled to reallocate memory");
+ Fatal("Failed to reallocate memory");
}
else
{
@@ -122,7 +122,7 @@ static void InsertBottomOfPile(Pile *pile, Card card)
if (!pile->card)
{
- Fatal("Failled to reallocate memory");
+ Fatal("Failed to reallocate memory");
}
else
{
@@ -147,11 +147,19 @@ static Card PopPile(Pile *pile)
pile->no--;
- pile->card = realloc(pile->card, sizeof *pile->card * pile->no);
+ if (pile->no)
+ {
+ pile->card = realloc(pile->card, sizeof *pile->card * pile->no);
- if (!pile->card)
+ if (!pile->card)
+ {
+ Fatal("Failed to reallocate memory");
+ }
+ }
+ else
{
- Fatal("Failled to reallocate memory");
+ free(pile->card);
+ pile->card = NULL;
}
}
else
@@ -195,6 +203,15 @@ static void SwapPile(Pile *a, Pile *b)
*b = t;
}
+static void FreePile(Pile *p)
+{
+ if (p->card)
+ {
+ free(p->card);
+ p->card = NULL;
+ }
+}
+
static void Centre(int y, const char *p)
{
size_t l = strlen(p);
@@ -796,6 +813,18 @@ static void Klondike(int draw)
{
WinScreen();
}
+
+ FreePile(&pile);
+ FreePile(&turned);
+ FreePile(&hearts);
+ FreePile(&diamonds);
+ FreePile(&clubs);
+ FreePile(&spades);
+ for(f = 0; f < 7; f++)
+ {
+ FreePile(&column_down[f]);
+ FreePile(&column_up[f]);
+ }
}
int main(int argc, char *argv[])