#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    14

    How to allocate array size


    I need to increase an array without making the array size constant


    In PHP i can do this


    Code:
    
    $array[] = $n++

    How can i achieve the same in C++
    The array size should increase dynamically when needed


    Any ideas?
    100 trillion calculations per nanosecond
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,641
    Rep Power
    4247
    There are plenty of ways to do something equivalent in C++.

    * First, there's the Standard Template Library, which has the Vector class. A vector class object dynamically sizes itself, as you add/delete items from it. See http://www.sgi.com/tech/stl/index.html and google for Standard Template Library for more information.

    The advantage of using STL is that it is available with pretty much any C++ compiler

    * If you're using Microsoft's VC++ compiler, there's a CObArray class. You can inherit from it, or use a predefined class (such as CStringArray), that inherits from CObArray.

    * Borland C++ Builder/Delphi have a similar TStringList class.

    Pick whichever you feel like using :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    14
    Yes i know this can easily be acheived using templates but can it be done in native C++ without having to to use templates
    100 trillion calculations per nanosecond
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    You can't, unless you dynamically allocate a new one and copy. This can be optimized by allocating more than what is needed, so it doesn't need to be performed for every length increase.

    Remember, PHP's code has to perform something similar to this or similar to vectors when you get down to machine code. So, just because native C++ code does not allow it, it will still be as fast or faster than high level PHP code...
  8. #5
  9. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    This will dynamically alloc mem for a Windows app.

    Code:
    MY_STRUC        *pMyArray=NULL;
    HGLOBAL           hMem=NULL;
    
    //alloc first 
    hMem = GlobalAlloc( GHND, sizeof(MY_STRUCT));
    //error check
    if(hMem == NULL)
    {
         iError = GetLastError();
         return FALSE;
    }
    //lock down
    pMyArray=(MY_STRUCT*) GlobalLock( hMem );
    //use
    
    //realloc
    hMem=GlobalHandle(pMyArray);
    hMem=GlobalReAlloc( hMem, iRecords*sizeof( MY_STRUCT ), GMEM_ZEROINIT);//zero new memory
    //error check
    
    //lock down again
    
    //free
    hMem=GlobalHandle(pMyArray);
    hMem = GlobalFree( hMem );
    //error check
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo