Thread: memory question (pointer related)

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    memory question (pointer related)

    Quick [dumb] question: I have a huge data storage class (lets call it DataClass). If I declare a pointer to that class, would that pointer take up a lot of memory (I am having memory problems so I want to be careful), or since its just a pointer (only storing an address) would the amount of memory it uses be trival?

    example:

    Code:
    SomeClass{
    private:
       DataClass *ptrDataClass; // ptr to huge data class
    public:
       ...
    };
    
    SomeClass::SomeClass(DataClass* dc){
       // addition side question: do I have to say ptrDataClass = new DataClass() ???
       ptrDataClass = dc;
    }
    chris

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The pointer itself will not take up much space. It'll take up as much space as any other pointer. As you said, it only stores an address to an object in memory. The object that the pointer points to, however, is going to be whatever size your DataClass.

    As to the other point there, about how to initialize it, it depends on the rest of your code, but chances are, you do actually want to create a new DataClass object and copy it. The way you have it now, there may be multiple pointers to that one object that your pointer points to. If one of them decides to deallocate the memory, then that pointer, and any others will point to invalid memory, and the contents of that class will be lost.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    38
    (in reference to "the other point there"). Things only get screwed up if the "master/origonal" DataClass gets deallocated right? If I just create a pointer to that class, and the pointer gets deallocated, it shouldn't screw anything up...(right?).

    Also, if I did want to create a copy of the origonal class (from a pointer) is this how I would do it:

    Code:
    SomeClass{
    private:
       DataClass *ptrDataClass; // ptr to huge data class
    public:
       ...
    };
    
    SomeClass::SomeClass(DataClass* dc){
       ptrDataClass = new DataClass();
       *ptrDataClass = *dc; // does this give me a deep copy? 
    }
    (I'm still new to this deep copy thing, and copying the value that pointers point to.
    chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. pointers
    By xixpsychoxix in forum C Programming
    Replies: 4
    Last Post: 10-02-2008, 03:31 PM
  4. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  5. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM