CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Dynamic Arrays

  1. #1
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    41

    Dynamic Arrays

    This is an easy one. I have a dynamic array of a certain size. I want to resize my array. Do I lose all the array members of the original array? I am looking for something similar to VB's redim preserve for c++ or do I have to save my original array create a new one then copy the original into the new one with the new data member.
    Thanks in advance,
    Matt

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    You would have to allocate the new memory, copy the data to the new memory, and delete the old memory.

    However, why not use std::vector<>? It does all of this work for you.
    Code:
    #include <vector>
    std::vector<int> IA; // A dynamic array of int's
    //...
    // Add
    IA.push_back(20);
    //...
    //... Redimension
    IA.resize(100); // Array now has 100 elements,  keeps old elements
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    int MyArray[10] = {0,1,2,3,4,5,6,7,8,9 };
    int *pDynamicArray = NULL;


    // allocate a new array
    pDynamicArray = new int[100];

    // Initialize array to -1.
    memset( pDynamicArray, -1, sizeof(int) *100 );

    // copy in your old array
    memcpy( pDynamicArray, &MyArray, sizeof(MyArray));

    // Verify the copy worked
    for ( int i = 0; i < 100; i++ )
    {
    if ( pDynamicArray[i] != -1 )
    printf("%d\n", pDynamicArray[i]);
    else
    break;
    }

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Please use the STL vector class as Paul mentioned.

    However, if you really do want redimension a C array, and I'm almost embarassed to mention this, look up realloc().

    Jeff

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Ditto.

    Use std::vector.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Jul 2002
    Posts
    788
    almost all guru are suggesting using std::vector as dynamic array.. i do love std::vector... even since Paul Mckenzie suggested me a few years back in codeguru when i was asking some array question.. it is so easy and clean.

    But now i am facing a problem towards using std::vector, i want to put std::vector in a shared memory. Can somebody tell me how to do this, and if this is not possible, what is the alternative?
    Am i facing a dead end.. now..? I want to enjoy the dynamic array as std::vector, but shared.
    If i can use C array, i need to use new delete to have dynamic array, well, if i can use new delete here, why can't i just declare a std::vector pointer in my shared section and thus have my vector shared?

    OR may be someone is going to tell me that i can do shared momory on fixed C style array only...
    Last edited by mce; July 23rd, 2002 at 01:40 AM.

  7. #7
    Join Date
    May 2001
    Posts
    472
    The shared memory sector has a fixed size. Make it the largest size you will need it to be and put a statically allocated vector into it.
    Ce n'est que pour vous dire ce que je vous dis.

  8. #8
    Join Date
    Jul 2002
    Posts
    788
    do you mind showing me some code for your suggestion? Thanks...

  9. #9
    Join Date
    May 2001
    Posts
    472
    Honostly, I have never done this, but the following should work:
    Code:
    #pragma comment(linker, "/SECTION:.Shared,RWS)
    #define SHARED_SIZE 512
    
    #pragma data_seg("Shared")
    BSTR array[SHARED_SIZE];
    #pragma data_seg()
    In my opinion, shared sections do not suit your needs. Try using an alternative method of interprocess communication, such as memory mapped files.
    Ce n'est que pour vous dire ce que je vous dis.

  10. #10
    Join Date
    Jul 2002
    Posts
    788
    i thought of using memory mapped files before..
    but the problem is.. say that i am using stl::vector,
    how would you suggest to write stl object to a file?
    by copying stl vector to a buffer first?
    if so how am i going to write something like below?

    memcpy( buf, &stlObj, sizeof( stlObj) );......??
    let's say i am using stl::vector<CComBSTR> m_vector

    Also, how i know how many bytes in the vector?

    Or more straight to the point, could you show me some codes how you write functions like CreateFileMapping(.....).. ? And how to access the data in the memory block pointed by MapViewOfFile??

    One more point, what is the safe maximum size of the memory map file i can use declare or use? so that i can use dynamic stl object sizing smoothly?

  11. #11
    Join Date
    May 2001
    Posts
    472
    Here is how to share data between two processes A and B using memory mapped files:
    1. Process A creates a named file map, passing 0xFFFFFFFF as the hFile parameter. This tells the system that the storage for the map will reside in a paging file - CreateFileMapping.
    2. Process B opens the named file map - OpenFileMapping.
    3. A process maps a view of the map to write to it or read from it, and closes it when it is done - MapViewOfFile, UnmapViewOfFile.
    4. When the processes are done sharing data they close the file map handles - CloseHanlde.
    Win32 guarantees coherence between views of a file mapping object. Be sure to study each of the functions you are going to call. It is very important that you check for failure after calling each function.

    Do not share the STL vector. As well, you can not share an array of strings. You have to copy whole strings into the view, not just pointers to them.
    Last edited by Alexey B; July 23rd, 2002 at 01:39 PM.
    Ce n'est que pour vous dire ce que je vous dis.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured