Problem
Draw the main memory diagram for this program. Specifically, (i) show where wordArray is stored; (ii) where is word pointing to before and after malloc; (iii) what is stored in the word? (iv) What happens to wordArray after free?
int main(int argc, const char * argv[]) {
char wordArray[MAX_WORD_LENGTH];
char *word;
printf("Enter a word (<%d chars): ", MAX_WORD_LENGTH);
scanf("%s", wordArray);
word = (char*)malloc(sizeof(char) * (strlen(wordArray) + 1));
/* allocate enough space */
if (word == NULL){
printf("ERROR - malloc failed: Exiting Program!nn");
return 1;
}
strcpy(word, wordArray); /* copy the string into the allocated space */
printf("You entered: %sn", word);
free(word); /* necessary? */
return 0;
}