CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2001
    Posts
    15

    Dinamic array of structures

    Hi, i would like to know if there is a way I can build a dinamic array of structures.
    I'v got this:

    struct m_Data{
    ULONG m_Size;
    ULONG m_Address;
    ULONG m_RawData;
    ULONG m_PointerToRawData;
    };

    is there a way I can build an array from this struct??

    I need to do something like this

    m_Data *m_DinamicData[] = new m_Data[y];

    where y is a value i get from another function.

    The compiler expect for a constant value instead of y, and gives me an error


    Tnx



  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Dinamic array of structures

    you must omit the [] symbols.

    and you don't want to do that anyway because you have std::vector


    The best things come to those who rate

  3. #3
    Join Date
    Apr 2002
    Posts
    11

    Re: Dinamic array of structures


    m_Data *m_DinamicData = new m_Data[y];







  4. #4
    Join Date
    May 2001
    Posts
    15

    Re: Dinamic array of structures

    nop it doesn't compile, y must be a constant value
    eg: 1,2,3 etc...


  5. #5
    Join Date
    Apr 2002
    Posts
    11

    Re: Dinamic array of structures

    Are you sure you use C++ compiler?

    this code:

    some_type *p = new some_type[some_var];




    must work - it is standard
    it's same as

    some_type *p = (some_type*) malloc(some_var * sizeof(some_type)); // this is C style allocation





  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Dinamic array of structures


    #include <vector>
    //...
    struct m_Data{
    ULONG m_Size;
    ULONG m_Address;
    ULONG m_RawData;
    ULONG m_PointerToRawData;
    };
    //...
    // Create a dynamic array using std::vector
    std::vector<m_Data> m_DynamicData(y);
    //..
    m_DynamicData[0].m_Size = whatever;
    //...



    No new, no delete, no problems.

    Regards,

    Paul McKenzie




  7. #7
    Join Date
    May 2001
    Posts
    15

    Re: Dinamic array of structures

    It worked! Tnx a lot


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