Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include /**Lab purpose: to learn the functions that perform dynamic memory allocation provided in the stdlib.h, including * malloc - allocate memory of

#include  #include  #include  /**Lab purpose: to learn the functions that perform dynamic memory allocation provided in the stdlib.h, including * malloc - allocate memory of size requested. * Return a pointer to the allocated memory, or NULL if the request fails Argument: the size of the memory block in request, in bytes * calloc - perfrom malloc and also initialize the allocated memory to zero * realloc- resize a memory block allocated by malloc() or calloc(). The existing data will be kept * free - frees the memory allocated by the above functions * * Your Job: response to the questions in Part 2 and Part 3. Submit the program with response. */ void printBinary(char * p, int numOfBytes); void printArray(int numberOfElements); void getBinary(char * p, int numOfBits); void printBytes(char * msg, int length); static int arr[8]; int main(){ /*************************************************************************************************  * Part 1: - Demo: allocate memory for strings *************************************************************************************************/ int i = 0; //a running index //a char type pointer char *msg; int numOfBytes = 5; /* request an allocation of 10 bytes memory, and return a pointer * convert the pointer to type char */ msg = (char *) malloc(numOfBytes); //put content into the memory strcpy(msg, "123"); printBytes(msg, strlen(msg)); printBinary(msg, numOfBytes); /*Need a larger memory? Reallocate it*/ numOfBytes = 10; msg = (char *) realloc(msg, numOfBytes); //append more stuffs strcat(msg, "Hello world!"); printBytes(msg, strlen(msg)); printBinary(msg, numOfBytes); //when done, free it up free(msg); printf("After free():%s ", msg); //it's gone printBinary(msg, numOfBytes); /*************************************************************************************************  * Part 2: Understanding the byte order for a larger datatype items in the memory * Response to the following questions: * a). Write down the single precision binary for the following number (seperate every 8-bits with a space) * (You may follow the decimal-to-floating point conversion procedurre to find the binary): * 0.0 - * 1.0 - * 2.0 - * 3.0 - * 4.0 - * b). Write down the binary given by the program for the following numbers(seperate every 8-bits with a space): * (You may copy from the program output) * 0.0 - * 1.0 - * 2.0 - * 3.0 - * 4.0 - * c). Explain why the byte order of the binary produced by the program (as those in b) are different from * the natural order as shown in a. * Hints: consider 1) the order of the addresses when visiting the bytes for output; 2) the endianness. * *************************************************************************************************/ //create an array of five float elements puts("-----------------------------Part 2-----------------------------"); int numOfElements = 5; numOfBytes = sizeof(float) * numOfElements;//5 floats float * pFloat = (float *) malloc(numOfBytes); for(i = 0; i < numOfElements; i ++){ * (pFloat + i) = 0.0 + i;//initialize the array: 0.0, 1.0, 2.0, 3.0, 4.0 } printBinary((char *)pFloat, numOfBytes);//You should see the binarys for all five float elements /************************************************************************************************* * Part 3: In the following block of code, 16 bytes were allocated for the pointer pInt to access legally. * a). If this chunk of memory were intepreted as short type, how many short type numbers are there? * Answer: * b). What are the short type numbers? {write the numbers in curly bracket below, seperate them by comma} * Answer: {} * Hint: you may creat another pointer of short which points to the same location, and then print out the numbers *************************************************************************************************/ numOfElements = 4; numOfBytes = sizeof(int) * numOfElements;//16 bytes int * pInt = (int*) malloc(numOfBytes); for(i = 0; i < numOfElements; i ++){ * (pInt + i) = 10 + i;//initialize the array: {10, 11, 12, 13, 14} } return 1; } //print each byte as int-value(character) void printBytes(char * p, int length){ int i = 0; for(; i < length; i ++){ printf("%5d(%c) ", *(p + i), *(p + i)); } printf(" "); } /** printBinary byte by byte * The byte order is determined by the endianness of the system *My system is a little-endian system which stores the least-significant byte at the smallest address. * so I have to output the bytes from the greatest address to the smallest * in order to view the binary correnctly*/ void printBinary(char * p, int numOfBytes){ int i = 0; char * currentPointer = p; for(; i < numOfBytes; i ++){ getBinary(currentPointer, 8); printArray(8); currentPointer ++; } printf(" ---------------------------------------------------------- "); } /*getBinary - get the binary representation of number into the array arr. Arguments: arr - the integer array to hold the binary representation number - the integer to get binary from numOfBits - the size of the arr and the number of bits for the binary Algorithm requirement: make use of bitwise shifting and masking to get the bit values for the number */ void getBinary(char * p, int numOfBits){ int i = numOfBits - 1; int num = *p; //take 1 byte as integer for(; i >= 0; i --){ arr[i] = num & 01; //Apply mask 01 to keep only the lowest 1 bit (i.e., the right-most bit), and store the result to the array num = num >> 1; //Right-shift 1 bit } } /* printArray: print numberOfElements in the argument integer array */ void printArray(int numberOfElements){ int i = 0; for(; i < numberOfElements; i ++){ printf("%d", arr[i]); if((i + 1) % 32 == 0 ) printf(" "); } printf(" "); }

ANSWER THE QUESTIONS

IN C

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

=+differences in home- and host-country costs of living?

Answered: 1 week ago

Question

=+derived from the assignment will balance the costs?

Answered: 1 week ago