Dynamic memory confusion

This is a discussion on Dynamic memory confusion within the C++ Programming forums, part of the General Programming Boards category; I was "playing around" with pointers on the heap when a suddenly got an idea (probably a stupid one). Is ...

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Dynamic memory confusion

    I was "playing around" with pointers on the heap when a suddenly got an idea (probably a stupid one). Is it possible to allocate more dynamic memory on an already dynamic allocated memory? I will explain it with an example so you hopefully understand what I mean. For instanse if I use a (predefind or user-defined) string class through a pointer like this

    Code:
    string *pname = new string;
    This means that somewhere on the heap a string-object has been created (or allocated). Doesn´t this mean that all data-members are on the heap or free store??? Usually is a string class rather dynamic which means that it has some kind of pointer. That pointer allocates new dynamic memory on another place on the heap. Is it possible to to call new on a object thats on the heap as many times you wish???

    Code:
    //Pointer to a string, works good, string handles everthing "internally"
    string *pek = new string;
    
    //Pointer to a pointer-string, why doesn´t this work???
    //*string *ppek = new *string;
    I just need some clarification.

    I hope you understand what I mean.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    > Doesn´t this mean that all data-members are on the heap or free store???<

    Yes.

    >Is it possible to to call new on a object thats on the heap as many times you wish??? <

    If by this you mean that your object is allocated using heap memory and it contains pointers that point to memory also allocated on the heap then yes. In this sense it doesn't matter where something is allocated, it's just an address in memory.

    >//Pointer to a pointer-string, why doesn´t this work???
    //*string *ppek = new *string;
    <

    It doesn't work because it's not valid C++. I'm not sure what you're trying to do. Something like -

    string** ppek = new string*;
    *ppek = new string;
    ?
    Joe

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Perfect clear now!

    Thx

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hey, would there be a difference in how fast these would crash a computer?
    Code:
    //Case 1:
    while(true)
         double** pp = new double*;
    
    //Case 2:
    while(true)
         double* p = new double;
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    It would depend of the sizeof each one.
    Joe

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    hmm... somehow, I had the impression that pointers took less memory than doubles though... maybe 2 bytes? 4 bytes? But a double takes 8 bytes, doesn't it? Oh well, I guess I'll just have to create a test proggie
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 03:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21