Solved

How to assign array ?

Posted on 2014-02-13
6
219 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 32

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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
 

Author Closing Comment

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

Great alternate solutions.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now