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

Thread: C++ '101'

Threaded View

  1. #1
    Join Date
    Apr 2005
    Posts
    41

    C++ '101'

    Hi all

    I have a general question as to how I should implement the following in Visual C++ (v6):

    I would like to read in a whole bunch of data from a file and save to memory so I can then perform various calculations, etc. My preferred way is to create a custom structure and define a 1-D array at the start of the file such that it is accessible by all later functions (i.e. OnInitDialog, OnLoadFile, CalculateSomething, etc). A function "OnLoadFile" reads in the data and saves to the 1D array "itemlist" and other functions such as "CalculateSomething" takes this data and performs various calculations / manipulations.

    Code:
    struct StructItem
    {
         int value1;
         int value2;
         float values[10];
    };
    
    #define MAXSIZE 100000
    StructItem itemlist[MAXSIZE];
    
    BOOL CProgDlg::OnInitDialog()
    {
       .....
    }
    
    void CProgDlg::OnLoadFile()
    {
       // Code here to read in ASCII file and save data to the variables in itemlist[] ..
       ..
    }
    
    void CProgDlg::CalculateSomething()
    {
       // Code here to perform some task on the variables in itemlist[] ..
       ..
    }
    My problem is that I do not know how I can dynamically set the size of the variable itemlist. For example the data set might be really large or really small so I find myself needing to set MAXSIZE large enough to cover all possibilities. Does anyone know a way of defining a variable that can change its size dynamically as and when required? The variable has to be accessible throughout hence why I don't define the variable "itemlist" in the function "OnLoadFile" dynamically using "StructItem *itemlist = new StructItem[NUM_ITEMS];"

    Thanks

    Robbie
    Last edited by robbiegregg; January 25th, 2010 at 09:50 AM.

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