0

I am trying to initialize an array of struct in C++.

This is my struct:

typedef ap_fixed<16,1> ap_fixed_data_type;
typedef struct {

    ap_fixed_data_type real_part;
    ap_fixed_data_type imaginary_part;

} my_data_struct;

And this is my array of structs:

static my_data_struct IFFT_output[1024];

I would like to initialize my array of struct using (if possible) the same "syntax" of standard arrays, as an example:

int my_array[1024] = {0};

This will initialize my array to all 0.

What I am trying to achieve is something like:

static my_data_struct IFFT_output[1024]={{0,0}};

Where this code should initialize each field (real_part and imaginary_part) in each struct to 0. With the above code I get this error:

terminate called after throwing an instance of '__gnu_cxx::recursive_init_error'

Which seems to be caused by bad initialized static variable (like here).

I know that I could initialize my data with a simple for loop, but I wanted to do something more "compact".

Is there a way to initialize my array of struct with the "syntax" I showed above?

1
  • My bad, typo. Fixed Commented Jun 25, 2021 at 15:36

1 Answer 1

4

This looks like C to me. If you want to use C++ you can have:

using ap_fixed_data_type = ap_fixed<16,1>;
struct my_data_struct
{
    my_data_struct()
        : real_part(/*initialization code here*/)
        , imaginary_part(/*initialization code here*/)
    {
    // more initialization code here
    }
    ap_fixed_data_type real_part;
    ap_fixed_data_type imaginary_part;

};

std::vector<my_data_struct> vec(1024);
std::array<my_data_struct, 1024> array;
Sign up to request clarification or add additional context in comments.

8 Comments

Suggestion: my_data_struct() : real_part{0}, imaginary_part{0} {} if ap_fixed doesn't have a default constructor.
The environment i am working with accept c++, I wrote the code in that way because it seemed the simplest way to do what I needed. Is there a way to initialize my array with the syntax i showed? Still a very useful answer
@MattiaSurricchio there is no performance benefit on using static my_data_struct IFFT_output[1024]; over std::array<my_data_struct, 1024> IFFT_output;, you just lose safety. The absence of constructors is also error prone. If you have a C++ compiler I don't see any benefit to write C code
This code is part of a bigger one that is meant to be synthesized and run on hardware, the easier/lower - level, the better. The static keyword is needed since it is a register which needs to be stored in memory
Those are just example on how to use multiple elements of type my_data_struct. std::vector uses dynamic memory allocation which is useful when you don't know the size in advance and std::array is basically a C style array with extra safety and features.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.