New'ing a pointer to an array [Solved]
-
Without using typedef, what is the syntax for dynamically allocating a pointer to an array? I tried the following, but can't get it to work:
long (*const val)[16] = new long()[16]; // C2440: Cannot convert from long* to long(*const)[16]
long (*const val)[16] = new long[16](); // This creates a (long*) with 16 elementsThis is a question for the sake of curiosity, as I am actually using a typedef in the code to make it more clear. Thanks, EDIT: I actually can't figure out how to do this even when using typedefs :(. I have tried the following:
typedef long *myType[16]; // myType is an array of 16 pointers to long
myType *val = new myType(); // C2440: Cannot convert from long** to myType(*)Any help with or without typedef's is appreciated. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
modified on Monday, November 29, 2010 3:27 PM
-
Without using typedef, what is the syntax for dynamically allocating a pointer to an array? I tried the following, but can't get it to work:
long (*const val)[16] = new long()[16]; // C2440: Cannot convert from long* to long(*const)[16]
long (*const val)[16] = new long[16](); // This creates a (long*) with 16 elementsThis is a question for the sake of curiosity, as I am actually using a typedef in the code to make it more clear. Thanks, EDIT: I actually can't figure out how to do this even when using typedefs :(. I have tried the following:
typedef long *myType[16]; // myType is an array of 16 pointers to long
myType *val = new myType(); // C2440: Cannot convert from long** to myType(*)Any help with or without typedef's is appreciated. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
modified on Monday, November 29, 2010 3:27 PM
A pointer to an array is simple -
long* ptr = new long[16];
What you've done above is to create an array of pointers. So you actually have 16 pointers.
«_Superman_» _I love work. It gives me something to do between weekends.
-
A pointer to an array is simple -
long* ptr = new long[16];
What you've done above is to create an array of pointers. So you actually have 16 pointers.
«_Superman_» _I love work. It gives me something to do between weekends.
I simplified my example. I am actually trying to dynamically create a 2D array, where one dimension is known (the unknown dimension needs to be consecutive in memory), hence I have:
long *var[16];
However, I would like to create a value for "var" in a function, and return it. Hence I have:
long *(&myFunc())[16] {
long *(*rval)[16] = new ?????;
...
return *rval;
}Then I declare my variable as a reference and delete it at the end of the code
long *(&var)[16] = myFunc();
...
delete &var;Is there some better way to go about this? I could just make it doubly-dynamic, but feel like I should retain known dimensions for the sake of readability. Any ideas? Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
I simplified my example. I am actually trying to dynamically create a 2D array, where one dimension is known (the unknown dimension needs to be consecutive in memory), hence I have:
long *var[16];
However, I would like to create a value for "var" in a function, and return it. Hence I have:
long *(&myFunc())[16] {
long *(*rval)[16] = new ?????;
...
return *rval;
}Then I declare my variable as a reference and delete it at the end of the code
long *(&var)[16] = myFunc();
...
delete &var;Is there some better way to go about this? I could just make it doubly-dynamic, but feel like I should retain known dimensions for the sake of readability. Any ideas? Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
Here is how it can be done, but its real ugly -
#define FIXED_SIZE 16
void allocate(long (**ptr)[FIXED_SIZE])
{
*ptr = new long[10][FIXED_SIZE];
}int _tmain(int argc, _TCHAR* argv[])
{
long (*ptr)[FIXED_SIZE];allocate(&ptr); delete \[\] ptr; return 0;
}
The clean and recommended way is to use a standard container -
std::vector<std::vector<long>> longArr;
«_Superman_» _I love work. It gives me something to do between weekends.
modified on Monday, November 29, 2010 2:46 PM
-
Here is how it can be done, but its real ugly -
#define FIXED_SIZE 16
void allocate(long (**ptr)[FIXED_SIZE])
{
*ptr = new long[10][FIXED_SIZE];
}int _tmain(int argc, _TCHAR* argv[])
{
long (*ptr)[FIXED_SIZE];allocate(&ptr); delete \[\] ptr; return 0;
}
The clean and recommended way is to use a standard container -
std::vector<std::vector<long>> longArr;
«_Superman_» _I love work. It gives me something to do between weekends.
modified on Monday, November 29, 2010 2:46 PM
I guess the answer is, "you can't allocate a single pointer to an array without the new[] operator" (with brackets). So, to allocate a single pointer to an array you have to do:
long (*val)[16] = new long[1][16];
...
delete[] val;Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
I guess the answer is, "you can't allocate a single pointer to an array without the new[] operator" (with brackets). So, to allocate a single pointer to an array you have to do:
long (*val)[16] = new long[1][16];
...
delete[] val;Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
This is a single pointer to an array -
long* ptr = new long[16];
delete [] ptr;Now I'm not sure what you're looking for.
«_Superman_» _I love work. It gives me something to do between weekends.
-
This is a single pointer to an array -
long* ptr = new long[16];
delete [] ptr;Now I'm not sure what you're looking for.
«_Superman_» _I love work. It gives me something to do between weekends.
Your last post solved it... I just can't allocate a SINGLE pointer to an array of pointers without dynamically allocating an array of 1 pointer to the array. For example:
// Allocate a single pointer to an array of 16 pointers to long
// Can't be done without using operator new[]
long *(*val)[16] = new ???;
...
delete val;There is nothing I can put in the "???" area to make the above code valid, because there is no valid syntax to dynamically create a single pointer to array using operator new(). I must do the following instead:
// Allocate a single pointer to an array of 16 pointers to long
long *(*val)[16] = new long*[1][16];
...
delete[] val;In short, you have answered my question. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
Your last post solved it... I just can't allocate a SINGLE pointer to an array of pointers without dynamically allocating an array of 1 pointer to the array. For example:
// Allocate a single pointer to an array of 16 pointers to long
// Can't be done without using operator new[]
long *(*val)[16] = new ???;
...
delete val;There is nothing I can put in the "???" area to make the above code valid, because there is no valid syntax to dynamically create a single pointer to array using operator new(). I must do the following instead:
// Allocate a single pointer to an array of 16 pointers to long
long *(*val)[16] = new long*[1][16];
...
delete[] val;In short, you have answered my question. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
:thumbsup:
«_Superman_» _I love work. It gives me something to do between weekends.