Question

Memory Consider a process running the following program #include estdlib.h> #include #define const char* int int <stdio.h> TO PRINT toPrintCPtr Good luck! TO PRINT; main for (i -0; i < sizeof (TO PRINT)-1; i++) printf(%c %c\n, toPrintCPt r [1], toupper(toPrintCPt r [i])); return(EXIT SUCCESS); Please tell where the following objects are stored in memory. Your choices are a. ROM BIOS b. kernal Memory (the OS) c. shared library memory (the glibc library) d. .text segment e. .rodata segment f. .data segment g. .bss segment h. the heap i. the stack Where is: 1. (4 Points) the function main)? 2. (4 Points) the memory for variable i? 3. (4 Points) the string %c %c\n? 4. (4 Points) the code that is given string G Gln and actually prints its pixels to the screen?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) Memory Allocation in C

  • The Global and static variables are stored into the data segment
  • The Dynamically allocated variables are stored into the heap memory, in C the variables are dynamically allocated using malloc, calloc or realloc.
  • The stack segment consists of all non-constants and non static and local variables and function parameters are declared inside a function.

Program

1) #include <stdlib.h>
2) #include <stdio.h>
3) #define TO_PRINT "Good luck"

4) const char* toPrintCPtr=TO_PRINT;
5) int i=0;
6) int main()
7) {
8) for (i=0; i<sizeof(TO_PRINT-1; i++)
9) printf("%c %c\n", toPrintCPtr[i], toupper(toPrintCPtr[i]));
10) return(EXIT_SUCCESS);
11) }

a)The main function is on line 6.

b) "i" is a global variable since it is defined outside of all the functions and as mentioned above, all the global variables are stored in the data segment.

c) the string is stored in the function TO_PRINT, whose address is further stored in the pointer and it is called in the line no 9.

d) line number 9 calls for the string to be printed.

printf("%c %c\n", toPrintCPtr[i], toupper(toPrintCPtr[i]));

here the first %c prints the string as it is

the second %c prints it in Uppercase because the "toupper" case library is used.

Add a comment
Know the answer?
Add Answer to:
Memory Consider a process running the following program #include estdlib.h> #include #define const char* int int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //codes in DynamicString.cpp #include "DynamicString.h" int DynamicString::myStringLen(const char* str){ //TODO::Implement me return -1; } DynamicString::DynamicString(){ //TODO::1::Implement...

    //codes in DynamicString.cpp #include "DynamicString.h" int DynamicString::myStringLen(const char* str){ //TODO::Implement me return -1; } DynamicString::DynamicString(){ //TODO::1::Implement me } DynamicString::DynamicString(const char* str){ //TODO::1::Implement me } int DynamicString::len() const{ //TODO::1::Implement me return -1; } const char* DynamicString::c_str() const{ //TODO::1::Implement me return nullptr; } char& DynamicString::char_at(int position){ //TODO::1::Implement me char* a = new char('a'); return *a; } char DynamicString::char_at(int position) const{ //TODO::1::Implement me return 'a'; } char& DynamicString::operator[](int position){ //TODO::1::Implement me char* a = new char('a'); return *a; } char DynamicString::operator[](int position) const{...

  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...

  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

  • Q.25. Given the following program, you are asked to discuss memory leaks caused in its execution....

    Q.25. Given the following program, you are asked to discuss memory leaks caused in its execution. Determine which memory locations get uncontrolled. (2 points) 4 6 7 8 9 10 11 12 B 13 14 15 16 17 18 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 5 void main() { char *aString = "Memory leaks?"; char **strList; int i, n = 5; strlist = (char**)malloc(n*sizeof(char*)); for (i=0; i<n; i++) { printf("\nstring %d ", i+1); strList[i] = (char*)malloc(50*sizeof (char));...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include #include #include #include #include #include #include using namespace std; const int MAX = 26; string...

    #include #include #include #include #include #include #include using namespace std; const int MAX = 26; string addRandomString(int n) {    char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',    'h', 'i', 'j', 'k', 'l', 'm', 'n',    'o', 'p', 'q', 'r', 's', 't', 'u',    'v', 'w', 'x', 'y', 'z' };    string res = "";    for (int i = 0; i < n; i++)    res = res + alphabet[rand() % MAX];    return res;...

  • C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

    C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT