[C++] How to correctly allocate dynamically an array of dynamic elements

Hi guys,I need some help to well understand how to declare dynamically an array of records, where each of them is also allocated dynamically.

I'll try to better explain what I mean with this source (I commented the code on the fly):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// This is the header file

#ifndef HEADER_H
#define HEADER_H
#define DIM 100    // Here I define a costant I'm going to use in the declaration of the array (see next)

#include <iostream>

using namespace std;

// This is my record (it defines a "product"):

struct Product{
    char code[3];
    char description[50];
    int qty;
};

// Now, this is the delicate part... I know for sure this typedefs allow me to correctly allocate the array need, but I want to better understand why:

typedef Product* PrPtr; // This should define the pointer-to-product (record) 
typedef PrPtr Array[DIM]; // This should define a DIM-sized array of pointers-to-product
typedef Array *ArrPtr; // This should define a pointer to the previous array
                                 
/* ... prototypes ... */

#endif //End of the header file


// Functions file

#include "header.h"

using namespace std;

void Create_Array(ArrPtr array, int &n) // I give to the function the pointer to the array (I'm going to declare it globally) and the "size" of the array (see next)

{         
          array = new Array[DIM];   // This is the most delicate line
        
          /* ... rest of the function ...*/
         
          /* Then I'm going to use "n" NOT as size parameter for the array
           * declaration, but as the limit in a for cycle to insert the records
           * in the array! */                                                
}

/* ... Main file and rest of the program ...*/  


So, I got two questions:

1) Are those typedefs the correct way to define a dynamic array of dynamic elements? Is this the only way to get this result?

2) I don't think that using that costant in the header (DIM) is practically correct, because I'll allocate a big part of memory that I'm never going to use! Doesn't this make the dynamic allocation futile? Can I use the "n" parameter that I pass to the function as the size parameter for the array instead?

Regards and thank you very much for your answers!
Last edited on
have a look at the vector container. If I understood you correctly this is what you are after:
http://www.cplusplus.com/reference/stl/vector/
If I understood correctly this may solve my problem, but I cannot use external classes / templates, I'm binded to only use pointers and memory allocation operators (new/delete), anyway I thank you for getting interested (also, I'll save that link, I'll surely need it in future).
I tried to actuate my solution, but it doesn't work, the compiler says:

invalid use of array with unspecified bounds


And I'm sure it's because I modified the second typedef this way:

typedef PrPtr Array[];

(Also, I'm using the "n" parameter to build the array, as I supposed).

So, if this is not correct, how can I define it as an array pointer without defining immediately the boundaries of the array?

P.S.: I also receive another error from the compiler:

declaration of 'Prodotto* (* mag)[]' shadows a parameter


Is it possibile to exaplain me what does it mean without having to post the entire source code? ((1) May it be because of the bad pointer definition posted above?)

EDIT: (1) I think it's always because of that, because I get this error just on the array declaration in the function.
Last edited on
1
2
typedef PrPtr Array[DIM]; // This should define a DIM-sized array of pointers-to-product
Array a; //equivalent to Product *a[DIM] 
Why do you need an array of pointers?

PrPtr array = new Product[n]; An array of products of size n. To use it in your function you need to pass the pointer by reference void Create_Array(PrPtr &array, int n);

declaration of 'Prodotto* (* mag)[]' shadows a parameter
My guess is that you are doing something like
1
2
3
void foo(int n){
	int n;
}
Topic archived. No new replies allowed.