Thread: Help assigning memory to structure arrays

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    Post Help assigning memory to structure arrays

    I need some help assigning memory to hold an array of structures with variable member arrays. Here is a part of the code

    Code:
    typedef struct 
    {
      double *t_stp;
      int    *grid_id;
      double *lv_x;
      double *lv_y;
      double *lv_z;
    }AA;
    
    int idx;
    
    static AA *load;
    
    
      if (idx == 0) {
        load = new AA[idx+1];
      }else{
        delete [] load;
        load = new AA[idx+1];
      }
    
      loadcase[idx].grid_id = new int[nc];
      loadcase[idx].lv_x    = new double[nc];
      loadcase[idx].lv_y    = new double[nc];
      loadcase[idx].lv_z    = new double[nc];
    idx and nc are passed in. Depending on the value of idx nc can hold different values. I am sure the way I am assigning memory to structure load is incorrect. But not sure how to increment it based on the value of idx.

    Please help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered the use of std::vector?

    You do not need to use the typedef as you did since this is C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    I couldn't get vectors to work. I was running into following messages which I didn't understand. If someone can provide the syntax, I am more than willing to switch to vectors

    LINK : test.dll not found or not built by the last incremental link; performing full link
    mysolver.def : warning LNK4102: export of deleting destructor 'public: virtual void * __ptr64 __cdecl std::bad_alloc::`scalar deleting destructor'(unsigned int)
    __ptr64'; image may not run correctly
    mysolver.def : warning LNK4102: export of deleting destructor 'public: virtual void * __ptr64 __cdecl std::length_error::`scalar deleting destructor'(unsigned i
    nt) __ptr64'; image may not run correctly
    mysolver.def : warning LNK4102: export of deleting destructor 'public: virtual void * __ptr64 __cdecl std::logic_error::`scalar deleting destructor'(unsigned in
    t) __ptr64'; image may not run correctly
    Creating library test.lib and object test.exp

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    OK. I converted my function to use vectors. Following is how I changed the function

    Code:
    struct AA 
    {
      vector<int>grid_id;
      vector<double>lv_x;
      vector<double>lv_y;
      vector<double>lv_z;
    };
    
    static AA *load;
    static int idx=1;
    
    vector<AA>load(5);
    
    
         for(ii=0;ii<n_loads;ii++) {
    	 for(jj=0;jj<ncases;jj++) {
                load[idx].grid_id.push_back(grids[ii][jj]);
                load[idx].lv_x.push_back(x_vec[ii][jj]);
         	    load[idx].lv_y.push_back(y_vec[ii][jj]);	  
    	    load[idx].lv_z.push_back(z_vec[ii][jj]);
             }        
         }
    The program crashes on the line "load[idx].grid_id.push_back(grids[ii][jj]);" Would appreciate if anyone can point me what I am doing wrong here.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    static AA *load;
    vector<AA>load(5);

    Which load is the right load? Depends on their scope. If they're both in the same scope you'd probably get a compiler error. If the load defined as static AA * is local, then you dereference an uninitialized pointer when you execute

    load[idx].grid_id.push_back(grids[ii][jj]);

    And what is the point of static int idx = 1; do you know that arrays and vectors start at the zeroth place?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    Thanks whiteflags. vector<AA>load(5) is the correct declaration. I fixed those errors and don't get the crash anymore. Now I want to increment the number of structures based on user input and not hard code it to 5. Any idea on what syntax for that would be.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    push_back an AA instance on load until the "user input" indicates you should stop.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    That's what I thought. But it results in the following error

    Code:
    load.push_back(AA);
    
    test.cxx(139) : error C2275: 'AA' : illegal use of this type as an expression
            test.cxx(18) : see declaration of 'AA'
    
    AA is declared as a structure
    
    struct AA 
    {
      vector<int>grid_id;
      vector<double>lv_x;
      vector<double>lv_y;
      vector<double>lv_z;
    }; 
    
    and load is declared as
    
    vector<AA>load;

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    My bad. I had to create an instance of AA. So I declared

    AA test;

    and then added the line

    load.push_back(test);

    Is this the correct way to code it. Now if I want to assign values to the members of each array, how do I do that.

    load[0].grid_id.push_back(10) doesn't work. It

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    load[0] would not be where the new element is anyway. If you execute load.push_back(temp); before assigning everything temp needs to have, then the new element is referred to at load.back(); So create a reference to that and have fun doing what you could have done beforehand.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    Hi Whiteflags,
    Thanks for your continued help. One of the things I noticed while reading more about vectors is the resize method. I have modified to use resize to increase the structure arrays based on user input. This seems to be more straightforward and works as expected. Do you see any drawback of using this method to resize arrays? Following is my updated code segments


    Code:
    struct AA 
    {
      vector<int>grid_id;
      vector<double>lv_x;
      vector<double>lv_y;
      vector<double>lv_z;
    };
    
    static vector<AA>load;
    int idx;
    
    
    // increase the structure array based on the size of idx
    
          load.resize(idx);
    
    // initialize members of structure load for the idx structure element of load
    
          load[idx].grid_id.assign(ncases,0);
          load[idx].lv_x.assign(ncases,0.0);
          load[idx].lv_y.assign(ncases,0.0);
          load[idx].lv_z.assign(ncases,0.0);
    
         for(ii=0;ii<n_loads;ii++) {
    	 for(jj=0;jj<ncases;jj++) {
                load[idx].grid_id.push_back(grids[ii][jj]);
                load[idx].lv_x.push_back(x_vec[ii][jj]);
         	    load[idx].lv_y.push_back(y_vec[ii][jj]);	  
    	    load[idx].lv_z.push_back(z_vec[ii][jj]);
             }        
         }
    
    
    // load the structure member arrays with values
    
         for(ii=0;ii<n_loads;ii++) {
    	 for(jj=0;jj<ncases;jj++) {
                load[idx].grid_id.at(jj) = grids[ii][jj];
                load[idx].lv_x.at(jj)    = x_vec[ii][jj];
         	    load[idx].lv_y.at(jj)    = y_vec[ii][jj];	  
    	    load[idx].lv_z.at(jj)    = z_vec[ii][jj];
             }
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure of arrays
    By zeebo17 in forum C Programming
    Replies: 2
    Last Post: 06-23-2010, 10:11 AM
  2. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 04:43 PM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Zeroing out member arrays in a Structure
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2004, 03:50 AM
  5. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 05:53 PM