Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 97 - 98 of 354

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 97

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Differentiate between a deep copy and a shallow copy?

Explanation

  • Shallow copy:-Shallow copy copies the contents of one object into another instance of the same class creating a mirror image. Due to copying of references and pointers, the two objects share the same externally contained contents. When one object is destroyed the pointers of other object become unpredictable.
  • Deep copy:-Deep copy the contents of one object to create another instance of the same class. In a deep copy, the target object will have its own buffers and resources thus the destruction of either object does not affect remaining object. The overloaded assignment operator would create a deep copy of objects.
  • Copy constructor simply copies the data values member by member. This method of copying is called shallow copy. For simple classes, comprised of built in types and no pointers this is acceptable.
  • If the object has any pointers a deep copy would allocate memory and copy elements pointed to by the pointers.
  • A deep copy is used for objects that are returned from a function.

Question 98

C & C Plus Plus
Edit

Write in Short

Short Answer▾

Difference between strdup and strcpy?

Explanation

Difference between Strdup and Strcpy
strdupstrcpy
Allocates memory space for copying the string to.Does not allocate the memory space to copy.
Space needs to freed up later when it is not in use.A pointer to the “string to copy” and a pointer to “place to copy” should be given.

Developed by: