This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Can I define an array of structures depending on input?


2011/1/31 Anna Sidera:
> Ok, Thanks.
>
> Anna
>
> ----- Original Message -----
> From: Axel Freyn <axel-freyn@gmx.de>
> Date: Saturday, January 29, 2011 2:56 pm
> Subject: Re: Can I define an array of structures depending on input?
>
>> Hi anna,
>>
>> On Sat, Jan 29, 2011 at 01:26:46PM +0200, Anna Sidera wrote:
>> > Hello,
>> >
>> > I have a program that uses some input parameters. One of these
>> parameters, for example my_input_parameter, takes values 1, 2, 3
>> and 4. I want to define an array in which each element is a
>> structure. But I want the structure to depend on the value of
>> my_input_parameter. For example:
>> >
>> > struct my_structure {
>> > Â Â<things that depend on the value of my_input_parameter>
>> > };
>> > struct my_structure *my_array = (struct my_structure
>> *)malloc(10000*sizeof(struct my_structure));
>> >
>> > Can you tell me any way to do this?
>> Well, the problem is: the decision, what your structure looks like
>> exactly, has to be taken at compile-time -- the compiler has to
>> know it.
>> The input-parameter is only known at runtime -- and then it's to
>> late to
>> change the structure. So if I understand correctly, your goal is
>> impossible as far as I know in compiled languages like C, but
>> would only
>> be possible in interpreted languages like python (there, you can
>> changedata-types during runtime).
>> What you can however do:
>>
>> 1) Fix the input-parameter already at compile-time, and compile four
>> different programs (for my_input_parameter=1,2,3 and 4). Then you
>> couldwrite something like this:
>> struct my_structure{
>> #if my_input_parameter == 1
>> ...
>> #endif
>> #if my_input_parameter == 2
>> ...
>> #endif
>> #if my_input_parameter == 3
>> ...
>> #endif
>> #if my_input_parameter == 4
>> ...
>> #endif
>> };
>> and compile the code with e.g. "gcc -Dmy_input_parameter=2"
>> That will give you best runtime performance -- and inÑtead of passing
>> the input parameter, you have to call the appropriate program.
>> Of course, another possibility here would be to link these four cases
>> into a single program -- and the call the appropriate routine
>> dependingon your input parameter.
>>
>> 2) you can use C++ and virtual functions (however, this might be
>> bad for
>> the runtime-performance, depending on how you call the code.
>> Calling a
>> virtual function needs more time than "normal" functions (and in
>> generalthey can't be inlined) ). Defining an abstract base-class,
>> which only
>> contains your interface:
>> class my_structure{
>> Âpublic:
>> Â Âvirtual double get_element() = 0;
>> }
>> then you can again create 4 independent classes for your 4 parameters:
>> class my_structure_1{ Â Â Â Â // for my_input_parameter == 1
>> Âpublic:
>> Â Âdouble get_element(){ ....}
>> };
>> class my_structure_2{ Â Â Â Â // for my_input_parameter == 2
>> Âpublic:
>> Â Âdouble get_element(){ ....}
>> };
>> ...
>> 3) you can do something analogous in C by using a structure with
>> function pointers, which you define to behave differently for
>> my_input_parameter == 1,2,3 or 4.

If you were going to use C++, and the range of possible values
my_input_parameter can take is known at compile-time, then templates
might be a better solution:

switch (my_input_parameter) {
    case 1:
       do_something<1>();
    case 2:
       do_something<2>();
    case 3:
       do_something<3>();
    case 4:
       do_something<4>();
}

You can also make my_structure a template, with different
specializations for different values of the parameter:

template<int P>
struct my_structure;

template<>
struct my_structure<1>
{
  // ...
};

template<>
struct my_structure<2>
{
  // ...
};

...


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]