public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Can I define an array of structures depending on input?
@ 2011-01-29 12:56 Anna Sidera
  2011-01-29 14:49 ` Axel Freyn
  0 siblings, 1 reply; 4+ messages in thread
From: Anna Sidera @ 2011-01-29 12:56 UTC (permalink / raw)
  To: gcc-help

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? 

Below I am explaining the reason I want to do this. My program currently uses many one dimensional arrays. For example buffer1, buffer2, buffer3, .... When the program needs to use the ith element of one of these arrays, usually, it also needs to use the ith element of some others of these arrays. I want to make my program faster. I want to try to store the data in such a way that data that are accessed together are physically near. However for some values of my_input_parameter some of these one dimensional arrays are not used at all. So I want my_stucture to depend on my_input_parameter.

Thanks,
anna

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I define an array of structures depending on input?
  2011-01-29 12:56 Can I define an array of structures depending on input? Anna Sidera
@ 2011-01-29 14:49 ` Axel Freyn
  2011-01-31 14:33   ` Anna Sidera
  0 siblings, 1 reply; 4+ messages in thread
From: Axel Freyn @ 2011-01-29 14:49 UTC (permalink / raw)
  To: gcc-help

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 change
data-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 could
write 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 depending
on 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 general
they 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.


Axel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I define an array of structures depending on input?
  2011-01-29 14:49 ` Axel Freyn
@ 2011-01-31 14:33   ` Anna Sidera
  2011-01-31 18:33     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Anna Sidera @ 2011-01-31 14:33 UTC (permalink / raw)
  To: Axel Freyn; +Cc: gcc-help

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.
> 
> 
> Axel
> 
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I define an array of structures depending on input?
  2011-01-31 14:33   ` Anna Sidera
@ 2011-01-31 18:33     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2011-01-31 18:33 UTC (permalink / raw)
  To: Anna Sidera; +Cc: Axel Freyn, gcc-help

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>
{
  // ...
};

...

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-01-31 14:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-29 12:56 Can I define an array of structures depending on input? Anna Sidera
2011-01-29 14:49 ` Axel Freyn
2011-01-31 14:33   ` Anna Sidera
2011-01-31 18:33     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).