Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Main Driver File Provided: #define INSTRUCTOR_FILE #ifdef INSTRUCTOR_FILE #include #include #include #define Elements(arrayDesig) (sizeof(arrayDesig)/sizeof((arrayDesig)[0])) void SwapObjects(void *pa, void *pb, size_t size); int main(void) { //

image text in transcribed
Main Driver File Provided:
#define INSTRUCTOR_FILE
#ifdef INSTRUCTOR_FILE
#include
#include
#include
#define Elements(arrayDesig) (sizeof(arrayDesig)/sizeof((arrayDesig)[0]))
void SwapObjects(void *pa, void *pb, size_t size);
int main(void)
{
// Create some test arrays whose elements will get swapped.
int iArray[] = {0x5555, 0xAAAA};
int iArrayCopy[Elements(iArray)];
struct xyz {char c; double d;};
struct xyz sArray[] = {{'0', 1.234}, {'Z', 5.678}};
struct xyz sArrayCopy[Elements(sArray)];
// Copy initialized arrays into uninitialized arrays.
memcpy((void *)iArrayCopy, (void *)iArray, sizeof iArray);
memcpy((void *)&sArrayCopy, (void *)&sArray, sizeof sArray);
// Swap elements in test arrays.
SwapObjects((void *)&iArray[0], (void *)&iArray[1], sizeof *iArray);
SwapObjects((void *)&sArray[0], (void *)&sArray[1], sizeof *sArray);
// Test if swaps worked.
if (iArray[0] != iArrayCopy[1] || iArray[1] != iArrayCopy[0] ||
sArray[0].c != sArrayCopy[1].c || sArray[1].c != sArrayCopy[0].c ||
sArray[0].d != sArrayCopy[1].d || sArray[1].d != sArrayCopy[0].d)
{
fprintf(stderr, "SwapObjects failed ");
return EXIT_FAILURE;
}
else
printf("SwapObjects succeeded ");
return EXIT_SUCCESS;
}
#endif
CZASE1 (4 points - Program) Exclude any existing source code files that may already be in your IDE project and add a new one. naming it C2A5E1_Swap Objects.c. Also add instructor-supplied source code file C2A5E1_main-Driver.c. Do not write a main function! main already exists in the instructor-supplied file and it will use the code you write. File C2A5E1_Swap Objects.c must contain a function named Swapobjects. Swapobjects syntax void Swapobjects(void *pa, void *pb, size_t size); Parameters: pa - a pointer to one of the objects to be swapped pb - a pointer to the other object to be swapped size - the number of bytes in each object Synopsis: Swaps the objects in pa and pb. Return: void Do not use any kind of looping statement. Do not call any function that is not from the standard library If Swapobjects dynamically allocates memory it must also free it before returning. All dynamic allocation results must be tested for success/failure before the memory is used. If allocation fails an error message is output to stderr and the program is terminated with an error code. Submitting your solution Send both source code files to the assignment checker with the subject line C2A5E1_ID, where ID is your 9-character UCSD student iD. See the course document titled "How to Prepare and Submit Assignments" for additional exercise formatting, submission, and assignment checker requirements. Hints: 1. Merely swapping pointers pa and pb does not swap the objects to which they point 2. The only case where dynamically-allocated memory is freed automatically is when a program exits. Good programming practice dictates that dynamically-allocated memory always be explicitly freed by the program code as soon as it is no longer needed. Relying upon a program exit to free it is a bad programming practice

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

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions

Question

Understand the role of internal marketing and communications.

Answered: 1 week ago