Add proper lines of code to PRINT the amount of memory allocated to dynamic vari
ID: 3665716 • Letter: A
Question
Add proper lines of code to PRINT the amount of memory allocated to dynamic variables (pointers).
Confused with how to print this information. Help would be much appreciated.
#include <stdio.h>
#include <string.h>
int *intPointer;
int main() {
char nameVar[30];
short exampleVar;
short shortVar1;
short shortVar2;
int intVar1;
int intVar2;
int intVar3;
char *charPointer;
float floatVar1;
float floatVar2;
exampleVar = 99;
shortVar1 = 12;
shortVar2 = 24;
intVar1 = 16;
intVar2 = 78;
intVar3 = 10;
floatVar1 = 6;
floatVar2 = 3;
charPointer = (char *) malloc(2000);
intPointer = (int *) malloc(1000);
strcpy(charPointer, "Yusuf Ozturk");
// Example: Below statement illustrates how to display address of the variable exampleVar
printf(" The address of variable exampleVar is : %p", &exampleVar);
// TODO – Please uncomment and complete each statement below to print the
// TODO memory location allocated to the respective variable
printf(" The address of variable shortVar1 is : %p", &shortVar1);
printf(" The address of variable shortVar2 is : %p", &shortVar2);
printf(" The address of variable charPointer is : %p", &charPointer);
printf(" The address of variable nameVar is : %p", &nameVar);
printf(" The address of variable intVar1 is : %p", &intVar1);
printf(" The address of variable intVar2 is : %p", &intVar2);
printf(" The address of variable intVar3 is : %p", &intVar3);
printf(" The address of variable intPointer is : %p", &intPointer);
printf(" The address of variable floatVar1 is : %p", &floatVar1);
printf(" The address of variable floatVar2 is : %p", &floatVar2);
// Add proper lines of code to print the location and the amount of memory allocated to dynamic variables (pointers)*/
Explanation / Answer
/*I HAVE MADE SOME MODIFICATIONS TO YOUR PROGRAM BY ADDING SOME MORE PREPROCESSING DIRECTIVES,WHICH WERE MISSING AND ALSO CORRECTED THE DYNAMIC MEMORY ALLOCATION WHICH YOU HAVE DONE FOR ALLOCATING MEMORY TO THE POINTER VARIABLE.NOW THE PROGRAM EXECUTES*/
//I HAVE HIGHLIGHED THE PART WHICH I MADE A CHANGE
#include <stdio.h>
#include <string.h>
using namespace std;
#include<iostream>
#include<conio.h>
int *intPointer;
int main()
{
char nameVar[30];
short exampleVar;
short shortVar1;
short shortVar2;
int intVar1;
int intVar2;
int intVar3;
char *charPointer;
float floatVar1;
float floatVar2;
exampleVar = 99;
shortVar1 = 12;
shortVar2 = 24;
intVar1 = 16;
intVar2 = 78;
intVar3 = 10;
floatVar1 = 6;
floatVar2 = 3;
charPointer = (char *) malloc((sizeof(char)*2000));
intPointer = (int *) malloc(1000*(sizeof(int)));
strcpy(charPointer, "Yusuf Ozturk");
// Example: Below statement illustrates how to display address of the variable exampleVar
printf(" The address of variable exampleVar is : %p", &exampleVar);
// TODO – Please uncomment and complete each statement below to print the
// TODO memory location allocated to the respective variable
printf(" The address of variable shortVar1 is : %p", &shortVar1);
printf(" The address of variable shortVar2 is : %p", &shortVar2);
printf(" The address of variable charPointer is : %p", &charPointer);
printf(" The address of variable nameVar is : %p", &nameVar);
printf(" The address of variable intVar1 is : %p", &intVar1);
printf(" The address of variable intVar2 is : %p", &intVar2);
printf(" The address of variable intVar3 is : %p", &intVar3);
printf(" The address of variable intPointer is : %p", &intPointer);
printf(" The address of variable floatVar1 is : %p", &floatVar1);
printf(" The address of variable floatVar2 is : %p", &floatVar2);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.