Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Runtime variable creation

Runtime variable creation

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdata-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    emrosa
    wrote on last edited by
    #1

    Has anybody faced the need to create up to k n-dimensional arrays at runtime? Here is an example: Suppose that at the moment of running the program, it needs 3 2-dimensional arrays, each of them has different dimensions. The dimensions of each table are stored in another 2-dimensional array. Then, you start creating the names of each array obtaining array1, array2 and array3. Then, how do you: a) Declare array1, array2 and array3 as dynamic arrays (i.e. double *array1, *array2, array2;) b) How do you re-declare them, i.e. array1 = new double[newdimension]; The problem is not how to create the name of the variables, because you can do it by handling strings and concatenating a number so they can be identified. The main problems are (a) and (b). If someone has a potential response, please emailme... Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

    P 2 Replies Last reply
    0
    • E emrosa

      Has anybody faced the need to create up to k n-dimensional arrays at runtime? Here is an example: Suppose that at the moment of running the program, it needs 3 2-dimensional arrays, each of them has different dimensions. The dimensions of each table are stored in another 2-dimensional array. Then, you start creating the names of each array obtaining array1, array2 and array3. Then, how do you: a) Declare array1, array2 and array3 as dynamic arrays (i.e. double *array1, *array2, array2;) b) How do you re-declare them, i.e. array1 = new double[newdimension]; The problem is not how to create the name of the variables, because you can do it by handling strings and concatenating a number so they can be identified. The main problems are (a) and (b). If someone has a potential response, please emailme... Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      First, you will need to create an array to hold your dynamic number of variables. Lets say that you are working with doubles, just for the example. You can either mess with dynamic array allocation like this:

      //A) declare the array dynamically and allocate space for the variables.
      double vars[]; // this is also equivalent to: double *vars;
      vars = new double[3];
      ...
      //B) reallocate the array, to say 5 variables
      double newVars[];
      newVars = double[5];
      // copy the data from the existing array before you delete the old array.
      memcpy(newVars, vars, sizeof(double) * 3); // three is the current number of vars in the array
      delete[] vars;
      // copy the newVars array into your old array, now you can comtinue to use your old pointer.
      vars = newVars;


      Checkout my Guide to Win32 Paint for Intermediates

      1 Reply Last reply
      0
      • E emrosa

        Has anybody faced the need to create up to k n-dimensional arrays at runtime? Here is an example: Suppose that at the moment of running the program, it needs 3 2-dimensional arrays, each of them has different dimensions. The dimensions of each table are stored in another 2-dimensional array. Then, you start creating the names of each array obtaining array1, array2 and array3. Then, how do you: a) Declare array1, array2 and array3 as dynamic arrays (i.e. double *array1, *array2, array2;) b) How do you re-declare them, i.e. array1 = new double[newdimension]; The problem is not how to create the name of the variables, because you can do it by handling strings and concatenating a number so they can be identified. The main problems are (a) and (b). If someone has a potential response, please emailme... Eric Manuel Rosales Pena Alfaro PhD student Unversity of Essex Wivenhoe Park Colchester, CO4 3SQ Essex, Uk email: emrosa@essex.ac.uk tel: +44-01206-87311

        P Offline
        P Offline
        Paul M Watt
        wrote on last edited by
        #3

        Depending on what you are doing, I would recommend that you check out the Standard Template Library (STL). It makes dynamic memory allocation and management a snap, and there are a few tricks that you can do to make accessing the varaibles much easier. At first glance the vector seems to be what you are looking for, because it is an array that will dynamically grow and manage its own memory. However there are problems with using an array, even if you are doing it the straight C++ way. As long as you will be using STL, I would recommend the map. A map you have a key, and a value that the key represents. You could make the key a string, and the value a double. Here is an example.

        //There is a little bit of setup work here, but do not let this intimidate you.
        //This goes in your header file.
        #include using std::map;

        // This is a function object that is declared to help sort the string names in your map.
        struct ltstr
        {
        bool operator()(const char* s1, const char* s2) const
        {
        return strcmp(s1, s2) < 0;
        }
        };

        typedef map mapVars;

        Here is how you will use what we just setup:

        mapVars vars;
        // I read in your question that creating a name for your variables is not an issue, so for
        // simplicity I am going to use a statically defined string.
        // Insert a new variable.
        vars.insert("variable1", double*);
        // Lookup the new variable.
        double value = vars["variable1"];
        //Remove this variable. when you erase a variable from a vector or a dynamic array, you will have
        //gaps, this is transparently handled with a map.
        vars.erase("variable1");

        Now you have a set of dynamically defined variables with names. Now you need a way to dynamically allocate the array of variables. This case would either be good for a vector or a map. Both of these objects will manage their own memory, and grow automatically for your. Here is how you would use the vector:

        // Once again, this goes into your header file.
        // Also use the same code as above for the map.
        #include
        using std::vector;

        typedef vector vecDouble;
        typedef map, ltstr> mapVecVars;
        // Here is how to use your new dynamic 2 dymensional variable vectors.
        // First create the new varaible, which will actually be a vector.
        mapVecVars vars;
        vecDouble newVar;
        // Add new doubles values into your new var.
        newVar.push_back(1.0);
        newVar.push_back(3.14);
        newVar.push_back(98.6);
        // A

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups