Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Shallow copy

Shallow copy

Scheduled Pinned Locked Moved C / C++ / MFC
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bhangie
    wrote on last edited by
    #1

    hi can someone tell me exactly what a shallow copy does it or is it a refrence is it good or bad to use ;)

    T J 2 Replies Last reply
    0
    • B bhangie

      hi can someone tell me exactly what a shallow copy does it or is it a refrence is it good or bad to use ;)

      T Offline
      T Offline
      Taka Muraoka
      wrote on last edited by
      #2

      Say you have a structure that looks like this:

      struct
      {
      int x ;
      char* p ;
      } ;

      If you copy an instance of this structure, the int will copy fine since it is a simple data type but the char* pointer causes problems. If you just copy the value of p over (a shallow copy), you will have two instances of your structure that have a pointer to the same string. This causes problems when cleaning up - who has responsibility for free'ing the string? You don't want to do it twice. A deep copy does an intelligent copy - it will make copies of the structure's data members where necessary. In this case, it will allocate a new block of memory and copy the string data over.


      "Sucks less" isn't progress - Kent Beck [^] Awasu 1.1.3 [^]: A free RSS reader with support for Code Project.

      1 Reply Last reply
      0
      • B bhangie

        hi can someone tell me exactly what a shallow copy does it or is it a refrence is it good or bad to use ;)

        J Offline
        J Offline
        Johann Gerell
        wrote on last edited by
        #3

        bhangie wrote: what a shallow copy does The copy is shallow when only the pointer is copied:

        void ShallowCopy(int* a)
        {
        int* b;
        b = a;
        }

        The copy is deep when the value at which the pointer points is copied:

        void DeepCopy(int* a)
        {
        int* b;
        *b = *a;
        }

        bhangie wrote: is it good or bad to use Depends on the situation. If a shallow copy is deleted, the original pointer no longer points to the expected data, which of course can be disasterous. That's one reason to implement copy constructors in C++, to ensure member pointers are copied correctly. -- Human beings, who are almost unique in having the ability
        to learn from the experience of others, are also remarkable
        for their apparent disinclination to do so. (Douglas Adams)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups