1

Using C++, I am implementing an array of structure HiddenLayer defined as

    struct HiddenLayer
    {
        int prev;     ///Number of Rows in node
        int next;     ///Number of Columns in node

        float **node; ///2D array pointer
    };

The array of structure is initialized in the main routine and node is the pointer to the 2D array inside the structure. I am initializing this array as

    int main()
    {
        struct HiddenLayer HLayer[1]; 

        HLayer[0].prev = 1;  //Num of rows
        HLayer[0].next = 3;  //num of col

        HLayer[0].node = (float *) malloc((HLayer[0].prev) * sizeof(float *));  
 
        for(int i=0;i<HLayer[0].prev;i++)
            HLayer[0].node[i] = malloc(HLayer[0].next * sizeof(float));

    
        return 0;
    }

But I get this error:

In function ‘int main()’:
main.cpp:22:73: error: cannot convert ‘float*’ to ‘float**’ in assignment
     HLayer[0].node = (float *) malloc((HLayer[0].prev) * sizeof(float *));  
                                                                         ^

>main.cpp:25:35: error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]
         HLayer[0].node[i] = malloc(HLayer[0].next * sizeof(float));

I followed the answers given Here and Here

What wrong am I doing?

Community
  • 1
  • 1
Pe Dro
  • 2,651
  • 3
  • 24
  • 44
  • 2
    So this is not [tag:c], right? – Sourav Ghosh Dec 27 '19 at 13:57
  • `(float *) malloc` should at least be `(float **) malloc` – Sourav Ghosh Dec 27 '19 at 13:57
  • 1. You are trying to affect a `float *` to a `float **`. 2. You are not casting to the correct type. – Storm Dec 27 '19 at 13:58
  • … and your other `malloc` should cast to `(float *)` - simples! – Adrian Mole Dec 27 '19 at 13:58
  • 1
    Sorry. Was thrown off by the use of malloc. https://stackoverflow.com/a/184540/1216776 – stark Dec 27 '19 at 14:08
  • You are not writing C++ code and the answers you are following are not talking about C++. They are talking about C. These two are different languages in which very different programming styles are considered good and which behave differently. For example using the pointer returned by `malloc` without a placement-new following it is technically undefined behavior in C++ and in C++ you would use `std::vector` instead of manual memory allocation. Please re-evaluate whether you *really* want to ask about C++. – walnut Dec 27 '19 at 15:44
  • @walnut, I intend to avoid vectors, due to the limitations of my application. So, yeah, anything else is fine! – Pe Dro Dec 28 '19 at 13:02

1 Answers1

3

The error messages are self-explanatory: you mixed up the types.

HLayer[0].node = (float *) malloc((HLayer[0].prev) * sizeof(float *));

should be

HLayer[0].node = (float **) malloc((HLayer[0].prev) * sizeof(float *));

and

HLayer[0].node[i] = malloc(HLayer[0].next * sizeof(float));

should be

HLayer[0].node[i] = (float *) malloc(HLayer[0].next * sizeof(float));

But that said, why'd you want to use malloc() and family in C++?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) Isn't this true for C++ ? And why not `new` instead of `malloc` – TruthSeeker Dec 27 '19 at 14:18
  • @TruthSeeker (1) No. (2) That's the same as the last line in my answer. – Sourav Ghosh Dec 27 '19 at 14:28
  • @SouravGhosh, thnks for the answer! I am bound to avoid C++ features like new and STL containers. – Pe Dro Dec 28 '19 at 13:06
  • But how should I add and manipulate data to this array? @SouravGhosh – Pe Dro Dec 28 '19 at 13:07
  • @PeDro You cannot reliably use dynamic memory allocation in C++ without `new` or the standard library. `malloc` without `new` causes undefined behavior technically always and for sure if the types used are non-trivial. Having a restriction on using `new`, but allowing `malloc`, makes no sense in C++. – walnut Dec 28 '19 at 13:47