-3

So what i want to achieve is global variable which represents pointer to dynamic array of structs which has other pointers to dynamic arrays inside.

So i've declared everything in the header file like that:

/*** "GLOB.h" ***/

//Dynamic Array of fixed Arrays of 4 floats each
typedef float DArray1[][4];

//Dynamic Array of Integers
typedef int DArray2[];

//Struct
struct MyStruct
{
    //pointer to DArray1
    DArray1 * A1;
    //pointer to DArray2
    DArray2 * A2;
    //Length of A1 and A2
    int len = 1;
    //Some other simple elements of fixed size
};

//Dynamic Array of Structs
typedef MyStruct DAStructs[];

namespace GLOB
{
    //pointer to DAStructs
    extern DAStructs * SS;
    //Few other simple variables initialized in place
};

Because i don't rly sure if i declared everything properly - first question is: how to declare such data-structure in C/C++?

And second(+third) is: how to properly initialize and finalize it with some variable length?

I guess initialization and finalization should be something like that:

#include "GLOB.h"

namespace GLOB
{
    void Init_SS(int len) {
        SS = new MyStruct*[len];
        for(int k=0; k<len; k++){
            SS[k].A1 = new float*[1][4];
            for(int j=0; j<4; j++) SS[k].A1[0][j] = 0.0;
            SS[k].A2 = new int*[1];
            SS[k].A2[0] = 0;
        }
    }
    void Fin_SS() {
        int len = length(SS);
        for(int k=0; k<len; k++){
            delete [] SS[k].A1;
            delete [] SS[k].A2;
        }
        delete [] SS;
    }
}

But obviously it's just a mess of nonsense and not the appropriate code...

P.S. i'd rather avoid using or any other solutions cuz i want to learn first of all and second i need as much control over how elements will be placed in memory as possible cuz later i'll need to pass pointer to such structure to other functions imported from DLLs...

Markus_13
  • 328
  • 2
  • 14
  • well C then as i mentioned i don't care C/C++ – Markus_13 Nov 30 '17 at 22:48
  • What you posted is C++ code. There is no such thing as "C/C++". – melpomene Nov 30 '17 at 22:49
  • Are you just looking for something like `int *A2 = new int[n]`? – melpomene Nov 30 '17 at 22:50
  • IDE such as Visual Studio accepts both C and C++. Am i wrong? – Markus_13 Nov 30 '17 at 22:50
  • Urgent read: [Are there any valid use cases to use new and delete, raw pointers or c-style arrays with modern C++?](https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr) – user0042 Nov 30 '17 at 22:50
  • 1
    I speak English and Latin. That doesn't mean they're the same language. – melpomene Nov 30 '17 at 22:52
  • Never said they're same, said i don't care which one of them can be used to accomplish such task. – Markus_13 Nov 30 '17 at 22:56
  • The thing is i don't want to use any complex classes or any other libraries. I believe such simple task as filling some dynamically sized arrays with bunch of numerical values to pass it to other module should be easily accomplished with raw language possibilities will it be C or C++. – Markus_13 Nov 30 '17 at 22:59
  • If it's so easy, why do you need to ask for help with it? (Also, I'd like to see you allocate dynamic memory in C without using any library functions.) – melpomene Nov 30 '17 at 23:04
  • Well memory allocation is a basic feature of any compilable language, isn't it? I mean "new" should allocate memory of len*sizeof(element_type) if i understood correctly... – Markus_13 Nov 30 '17 at 23:10
  • No, brainfuck is a compilable language with no memory allocation. – melpomene Nov 30 '17 at 23:11
  • Dude, it's not an erudition contest or discussion about programming languages, so that comment section gone completely offtopic... – Markus_13 Nov 30 '17 at 23:12
  • @Markus_13 In C you'd do it with dynamic allocation, in C++ you should use `std::vector`. – Barmar Nov 30 '17 at 23:48
  • Well, i removed "c++" tag, so well, how how it should be done in C?)) – Markus_13 Dec 01 '17 at 00:18

1 Answers1

0

Well nobody answered, but actually the answer was really simple, cuz i did everything almost properly =)

My first (and main) mistake was that i forgot that arrays in C are just pointers to first elements, so instead of

typedef float DArray1[][4];
typedef int DArray2[];
typedef MyStruct DAStructs[];

it should be

typedef float (*DArray1)[4];
typedef int * DArray2;
typedef MyStruct * DAStructs;

And it's not needed to make pointer to array, cuz array is already pointer lol

And my second mistake was that i forgot (well it's not like i knew it =) that length of array in C not stored anywhere, so instead of length(SS) it should be another variable where length should be stored manually upon allocation.

But the whole logic which i assumed seem to be legit, and it works fine. Even tho i didn't tested my project for memory leaks, when i use finalization procedure it seem to free allocated memory without any problems...

Markus_13
  • 328
  • 2
  • 14
  • Arrays are not just "pointers to first element". But you are correct that the best way to handle dynamically-sizing arrays is to use a pointer to the first element, instead of yoru attempted array typedefs – M.M Dec 04 '17 at 20:15
  • Well i guess it's not that arrays ARE pointers internally, but from programmer's perspective it seems they are programmatically... – Markus_13 Dec 06 '17 at 00:31