Question

In: Computer Science

A. What can happen if pointer is uninitialized. Explain the concept and what should ideally be...


A. What can happen if pointer is uninitialized. Explain the concept and what should ideally be done
in such case

B. Memory was allocated initially for 5 elements, later on two more elements added to list. How can
space be managed in such case. Implement the scenario in C.

Solutions

Expert Solution

A. What can happen if pointer is uninitialized. Explain the concept and what should ideally be done in such case.

An uninitialized pointer stores an undefined garbage value. The pointer would be initialized to a non-NULL garbage value that doesn't really point to anything real. An uninitialized pointer can cause a system crash.

Suppose we have declared a pointer without initializing it for example

int* ptr;

Now after this declaration we don’t know which location in memory this pointer is pointing. It might be pointing towards system stack,program’s code space or into operating system.

Now if you without initializing assign a value to it like

*ptr=50;

It can change the value at any random location towards which ptr is pointing without you knowing or giving an error which might be difficult to bug and your system might crash.

So, it is always advisable and good programming practice to initialized a pointer variable.

int* ptr = NULL;

B. Memory was allocated initially for 5 elements, later on two more elements added to list. How can
space be managed in such case. Implement the scenario in C.

To handle such scenario we need to use dynamic memory allocation. It enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.

  1. malloc() : The malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of void which can be casted into pointers of any form.
  2. calloc() : This method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. The default value is ‘0’.
  3. realloc() : It reallocates the memory occupied by malloc() or calloc() functions.
  4. free() : This function is used to free the space allocated in the memory.

The below code can be used in demonstration.

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

int main()
{

   // This pointer will hold the
   // base address of the block created
   int* ptr=NULL;
   int n=0;

   // Get the number of elements for the array from user
   printf("Enter number of elements: ");
   scanf("%d",&n);

   // Dynamically allocate memory using malloc()
   ptr = (int*)malloc(n * sizeof(int));

   // Check if the memory has been successfully allocated by malloc or not
   if (ptr == NULL) {
       printf("Memory not allocated.\n");
       exit(0);
   }
   else {
   // If memory is allocated then getting the elements of the array 2,3,4, ..
       printf("Memory successfully allocated !!! \n");

       for (int i = 0; i < n; ++i) {
           ptr[i] = i + 2;
       }

       // Print the elements of the array
       printf("The elements of the array are: ");
       for (int i = 0; i < n; ++i) {
           printf("%d ", ptr[i]);
       }
   }

   return 0;
}


Related Solutions

1)Ideally, what should the slope of the F- ISE’s calibration curve be? What should this value...
1)Ideally, what should the slope of the F- ISE’s calibration curve be? What should this value be for an H+ ISE (i.e., a pH electrode)? For a S2- ISE? 2)What is the composition of the F- ISE’s active surface? Why is it important to control the pH of the solution for the analysis with the F- ISE?
How do you dereference a pointer? Explain the difference between a dereferenced pointer and the pointer...
How do you dereference a pointer? Explain the difference between a dereferenced pointer and the pointer itself.
Explain why Rawls thinks the rules for an ideally just society should be negotiated behind a...
Explain why Rawls thinks the rules for an ideally just society should be negotiated behind a veil of ignorance? In other words, what purpose does the veil serve?
Explain what would happen to the terms of trade should home offer a subsidy to home...
Explain what would happen to the terms of trade should home offer a subsidy to home producers that export to the rest of the world under the two set of assumptions below (10 points, 5 points for each case): Home is considered a large country (hence, it is able to affect world prices) Home is considered a small country (unable to affect world prices) (Note: Assume no counter-action by foreign countries.)
10. In what circumstances should a Will revision be considered? What should happen to the original...
10. In what circumstances should a Will revision be considered? What should happen to the original Will? 11. List typical types of non probate assets. 12. Why is an Independent Administration beneficial for estate planning purposes? 13. Under Texas law, define incapacity for legal purposes. 14. What is the purpose of a Statutory Durable Power of Attorney? 15. What is the purpose of a Medical Power of Attorney? 16. Why should we consider signing a Medical Directive?
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer...
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays. 1) Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision...
Please describe the concept of investment spending, as well as what will happen to the aggregate...
Please describe the concept of investment spending, as well as what will happen to the aggregate demand curve if investment spending is increased autonomously. Also provide an example of spending that a macroeconomist would consider “investment spending.”
In these two cases, you can use any example.             (a) Explain what would happen to...
In these two cases, you can use any example.             (a) Explain what would happen to prices in a market equilibrium if there were an increase in the demand for a product. Give an example of a real life situation pertaining to this.             (b) Explain what would happen to prices in a market equilibrium if there were an increase in the supply for a product. Give an example of a real life situation pertaining to this.
Explain the concept of Elasticity of demand. How is it measured? What can it tell a...
Explain the concept of Elasticity of demand. How is it measured? What can it tell a marketing professional about how to market a product or service? Give some examples of products or services for which elasticity would be high. Give some examples where elasticity would be low.
What is accumulated depreciation? Can someone explain this concept with EASY English?
What is accumulated depreciation? Can someone explain this concept with EASY English?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT