Solved

How to assign array ?

Posted on 2014-02-13
6
227 Views
Last Modified: 2014-02-14
class Parameters
{
  /// <summary>Information about all parameters, sorted by the parameter ID.</summary>
  public : static  ParameterDef InfoModelOne[788] ;
  public:  static  ParameterDef InfoModelTwo[816] ;
  public : static  ParameterDef InfoModelThree[788] ;  
  
};

Open in new window


During run-time we find the model of our embedded product.  Depending of the model, we use one of above arrays.  This array will be used by two different tasks.  I don't want to check the running model and use the associated array everytime the array is needed.  

I would like to get the product model, and assign array to some pointer variable and use that pointer variable whenever I need to access the array.

maybe possibilities are as follows:

ParameterDef  *  Info_Ptr;

info_Ptr = InfoModelOne;
   
            or

info_ptr = InfoModelTwo;

           or

info_ptr = InfoModelThree;

or can I create array as follows:

public : static  ParameterDef  Info [ ] ;

and then maybe I can do:      Info = InfoModelOne;    // or other two models
0
Comment
Question by:naseeam
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 350 total points
ID: 39858023
You could do that within your constructor, by either determining the model there or passing information about it to the constructor, e.g.

enum ModelParameters {
   ModelOne = 0,
   ModelTwo,
   ModelThree
};

class Parameters
{
  /// <summary>Information about all parameters, sorted by the parameter ID.</summary>
 //  public : - IMO no need to make that public static
   static  ParameterDef InfoModelOne[788] ;
   static  ParameterDef InfoModelTwo[816] ;
   static  ParameterDef InfoModelThree[788] ;  

  ParameterDef* Info_Ptr;
  
public:

  Parameters(const ModelParameters mp) 
  {
     switch(mp) 
    {
      case ModelOne:

        Info_Ptr = InfoModelOne;
        break;

      case ModelOne:

        Info_Ptr = InfoModelOne;
        break;

      case ModelTwo:

        Info_Ptr = InfoModelTwo;
        break;

      case ModelThree:

        Info_Ptr = InfoModelThree;
        break;

       default:

        Info_Ptr = NULL; // treat that case accordingly
        break;
    };
  }
};
                                  

Open in new window

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 75 total points
ID: 39858338
Why not just use a std::vector, set it's size once at runtime and that just pass it around by reference? There are many advantages of using a vector, least of which you can actually get it's size in a function it is passed to. You can't do this with a static array as they always decompose to pointers. Having multiple static arrays is wasteful and will artificially increase the size of your binary; you'll pay a cost for the one's you are not using! If I were reviewing this code as part of a peer review I would reject it as it stands.
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 75 total points
ID: 39858391
you may derive from class Parameters and handle different static arrays by means of virtual functions:

class Parameters
{
public:
       virtual size_t  getParameterInfo(ParameterDef * & infoArr) = 0;
       ....
};

class ModelOneParameters : public Parameters
{
       static ParameterDef  defArr[788];
public:
       size_t  getParameterInfo(ParameterDef * & infoArr)
       {
               infoArr = defArr;
               return sizeof(defArr)/sizeof(defArr[0]);
       }
       ...
};

// parameters.cpp
...
static ParameterDef ModelOneParameters::defArr[788] = 
     { 
          { 123, "abc", ...., },
          { 456, "xyz", ...., },
          ....
      };
static ParameterDef ModelTwoParameters::defArr[816] = 
     { 
          { 777, "HHHH", ...., },
          { 333, "OO", ...., },
          ....
      };

Open in new window


the above concept also would allow to have common (shared) parts of parameters handled in the base class and individual parts handled in the derived class.

note, definition arrays which have hundreds of elements normally is not so good a design. you might consider to load such data from a database or a resource file rather to define them in source code. if you do so, you easily could handle it with one class again and would use a dynamic container like a vector as evilrix has suggested.

Sara
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:naseeam
ID: 39858939
>> public : - IMO no need to make that public static
One of our tasks access class Parameters members as follows:

/* Before we only had one array Info. */
unsigned short infoSize = sizeof (Parameters : : Info ) ;
...
/* Another access to Info array from the same task */  
(void) _instance -> readParamList ( &Parameters : : Info [infoIndex], 0,  numParms);

I believe if it wasn't public, task wouldn't able to access member Info the Parameters class.  Am I correct ?

>> ParameterDef * Info_Ptr ;
so after  Info_Ptr is assigned one of arrays, then we use following notation to access array members ?
Info_Ptr [ 9 ],    Info_Ptr [ 3 ],    etc.

>> Why not just use a std::vector
I totally agree.  
This code base always had single array 'info' in the Parameters class.  My job is to expand ito to Three arrays.  I argued that we will waste memory but they say we have plenty of memory in this embedded product.  We are also schedule constraint.  So, this needs to get done today.  
Also, I have never used vectors in c++.  We'll need to defer this essential improvment to the future.


>> you may derive from class Parameters and handle different static arrays by means of virtual functions:

Another great idea.  Since code is setup for arrays already, decision has been made to continue using arrays.  But in future I'll fight for a better container approach.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39859591
>>I believe if it wasn't public, task wouldn't able to access member Info the Parameters class.  
>>Am I correct ?

Yes and no - you don't want to acces these values directly, you want to access

  ParameterDef* Info_Ptr;

which is assigned regarding to the device type. And for that one, add an accessor:

const  ParameterDef* const Get_Info_Ptr() const {return Info_Ptr};
ParameterDef* Get_Info_Ptr_ForReading() {return Info_Ptr};

Open in new window

Of course, the seize has to be taken into account, but that can be done via a similar enum:

enum ModelParameterSizes {
   ModelOneSize = 788,
   ModelTwoSize = 816,
   ModelThreeSize = 788
};

Open in new window

0
 
LVL 1

Author Closing Comment

by:naseeam
ID: 39859674
Great support!  Coded very good solution to somewhat unclear question.

Great alternate solutions.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question