Question

Create a function, void allocate_mem(unsigned int** input_one, unsigned int** input_two, unsigned long int** output, int num_ints)...

Create a function, void allocate_mem(unsigned int** input_one, unsigned int** input_two, unsigned long int** output, int num_ints) in C.

This function allocates enough memory to the three arrays to store num_ints elements each.

This function should exit with EXIT_FAILURE if the program fails to allocate the memory.

input parameters:

unsigned int* input_one

unsigned int* input_two

unsigned long int* output

int num_ints

return parameters:

none

Do NOT hard code the size of the unsigned (long) integer arrays

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

Two things:

  1. It is NOT possible to explicitly check whether a pointer has been allocated memory or not so we'll check by allocating values and displaying.
  2. Secondly, we don't have exception handling (try..catch) in C so we'll use a #define workaround.

//Program in C

#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>

#define FALSE 0
//define try block for exception handling simulation
#define TRY_BLOCK do{ jmp_buf buffer; if( setjmp(buffer) == FALSE ){
//define catch block for exception handling simulation
#define CATCH_BLOCK } else {
//define exit block for exception handling simulation
#define EXIT_BLOCK } }while(FALSE)

//function to allocate memory
void allocate_mem(unsigned int** input_one, unsigned int** input_two,
unsigned long int** output, int num_ints)
{
//start try block
TRY_BLOCK
{
*input_one = (unsigned int*)malloc(num_ints * sizeof(input_one));
*input_two = (unsigned int*)malloc(num_ints * sizeof(input_one));
*output = (unsigned long int*)malloc(num_ints * sizeof(input_one));
}
//exit using catch block
CATCH_BLOCK
{
exit(EXIT_FAILURE);
}
EXIT_BLOCK;
//assign values to arrays
for(int i=0; i<num_ints; i++)
*(*input_one+ i) = i*1;
for(int i=0; i<num_ints; i++)
*(*input_two+ i) = i*2;
for(int i=0; i<num_ints; i++)
*(*output+ i) = i*3;
}

//main function
int main()
{
//declare pointers to arrays
unsigned int *ptr1,*ptr2;
unsigned long *ptr3;
//declare size variable
int size;
//get size of array
printf("Enter Size: ");
scanf("%d", &size);
//call the function to allocate memory
allocate_mem( &ptr1 , &ptr2 , &ptr3 , size );
//print the arrays
printf("\nArray 1 is: ");
for(int i=0; i<size ;i++)
printf("%d\t", ptr1[i]);
printf("\nArray 2 is: ");
for(int i=0; i<size ;i++)
printf("%d\t", ptr2[i]);
printf("\nArray 3 is: ");
for(int i=0; i<size ;i++)
printf("%ld\t", ptr3[i]);
free(ptr1);
free(ptr2);
free(ptr3);
return 0;
}

//Output

Enter Size: 6
Array 1 is: 0   1   2   3   4   5  
Array 2 is: 0   2   4   6   8   10  
Array 3 is: 0   3   6   9   12   15  

//Screenshot of Output & Code (For reference to indentation)

> Run C++10 co c o S Reset Copy Copy * > Run+URL (Generates URL as C++ C++14 C# 42 Java Perl PHP Time(sec): 0 49 Memory(MB):

Add a comment
Know the answer?
Add Answer to:
Create a function, void allocate_mem(unsigned int** input_one, unsigned int** input_two, unsigned long int** output, int num_ints)...
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
  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned...

    In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned int * flag-holder, int flag-position); int check-flag (unsigned int flag-holder , int flag-position); void display -32_flags (unsigned int flag-holder); int main(int argc, char* argv (1) unsigned int flag -holder = 0; set-flag (& flag-holder, 3); set-flag (& flag-holder, 16); set-flag (& flag-holder, 31); display-32-flags (flag-holder); unset-flag(& flag-holder , 31); unset-flag (& flag-holder, 3); set-flag (& flag-holder , 9); display-32-flags (flag-holder ); return 0; Write...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • -Create a function output() in C that takes the pointer to the array and its size...

    -Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (malloc format) (function prototype : void output(int *arrayPtr, int size); -Create a function check() in C that takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

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