My code below compiles, runs, and executes properly, but there are memory leaks.
ID: 3544464 • Letter: M
Question
My code below compiles, runs, and executes properly, but there are memory leaks. Can you fix the memory leaks by adding free statements. Can you also list the swap pointers function.
o Place the free statements as early as possible without breaking the program (i.e. freeing memory when it will be used later).
o Create a simple function that takes two pointers and swaps them. The function should not return anything.
o Please check for NULL pointers before dereferencing!
o Example:
int main(void) {
int a, b;
int *pA, *pB;
pA = &a;
pB = &b;
// Call your swap function
// pA == &b
// pB == &a
}
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
typedef struct
{
int *pInt;
} MyStruct;
MyStruct *createMyStruct()
{
MyStruct *newStruct = (MyStruct *) malloc(sizeof(MyStruct));
/* Should check if malloc succeeded... */
newStruct->pInt = (int *) malloc(sizeof(int));
/* Should check if malloc succeeded... */
*newStruct->pInt = 0;
return newStruct;
}
void printMyStruct(MyStruct *pStruct)
{
if (pStruct == NULL )
{
printf("NULL struct pointer! ");
}
else
{
printf("{ ");
if (pStruct->pInt == NULL )
{
printf(" pInt = NULL ");
}
else
{
printf(" pInt = %p ", pStruct->pInt);
printf(" *pInt = %d ", *pStruct->pInt);
}
printf("} ");
}
}
int main(void)
{
MyStruct myStruct;
MyStruct *pStruct;
int iValue = 10;
pStruct = &myStruct;
myStruct.pInt = &iValue;
printf("Print #1: ");
printMyStruct(pStruct);
pStruct = (MyStruct *) malloc(sizeof(MyStruct));
pStruct->pInt = myStruct.pInt;
printf("Print #2: ");
printMyStruct(pStruct);
pStruct = createMyStruct();
*pStruct->pInt = 10;
printf("Print #3: ");
printMyStruct(pStruct);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct
{
int *pInt;
} MyStruct;
MyStruct *createMyStruct()
{
MyStruct *newStruct = (MyStruct *) malloc(sizeof(MyStruct));
/* Should check if malloc succeeded... */
newStruct->pInt = (int *) malloc(sizeof(int));
/* Should check if malloc succeeded... */
*newStruct->pInt = 0;
return newStruct;
}
void printMyStruct(MyStruct *pStruct)
{
if (pStruct == NULL )
{
printf("NULL struct pointer! ");
}
else
{
printf("{ ");
if (pStruct->pInt == NULL )
{
printf(" pInt = NULL ");
}
else
{
printf(" pInt = %p ", pStruct->pInt);
printf(" *pInt = %d ", *pStruct->pInt);
}
printf("} ");
}
}
int main(void)
{
MyStruct myStruct;
MyStruct *pStruct;
int iValue = 20;
pStruct = &myStruct;
myStruct.pInt = &iValue;
printf("Print #1: ");
printMyStruct(pStruct);
pStruct = (MyStruct *) malloc(sizeof(MyStruct));
pStruct->pInt = myStruct.pInt;
printf("Print #2: ");
printMyStruct(pStruct);
free(pStruct); //remove the newly allocated memory
pStruct= &myStruct;
free(pStruct); //remove myStruct allocated memory
pStruct = createMyStruct();
*pStruct->pInt = 10;
printf("Print #3: ");
printMyStruct(pStruct);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.