Dynamic creation of objects

T

Thomas Hede Jensen

Hi,

I'm currently working on a project in which I link to a library where the
function definitions takes parameters of the type: "const int *const
*paramName"
As far as I can tell from the example of usage this type definition requires
a const int array, however, I do not know the size of those arrays at
compile time (due to the nature of other parts of my code), thus I have to
dynamically allocate the arrays by using "new". Passing parameters of this
type results in the compiler error:
'Create' : none of the 4 overloads can convert parameter 3 from type 'int *'

I have tried type casting or reinterpret_cast, none of which works, also I
tried passing the parameters as "**int", this compiles, but this throws an
exception at runtime.

My question is if there is a way to work around this? Or any way to cast the
dynamically allocated arrays to fit the type definition of the functions?

I'm relatively new to C++, so any help will be greatly appreciated.

Best Regards

Thomas Hede Jensen
 
K

Karl Heinz Buchegger

Thomas said:
Hi,

I'm currently working on a project in which I link to a library where the
function definitions takes parameters of the type: "const int *const
*paramName"

Thats a pointer to a pointer or a pointer to an array of pointers.
As far as I can tell from the example of usage this type definition requires
a const int array,

It requires a pointer to an array of pointers
however, I do not know the size of those arrays at
compile time (due to the nature of other parts of my code), thus I have to
dynamically allocate the arrays by using "new". Passing parameters of this
type results in the compiler error:
'Create' : none of the 4 overloads can convert parameter 3 from type 'int *'

I have tried type casting or reinterpret_cast, none of which works, also I
tried passing the parameters as "**int", this compiles, but this throws an
exception at runtime.

casting won't help if the argument types don't fit in principle.

Assuming: The function wants a pointer to an array of pointers.

void foo( const int *const *paramName )
{
}

int main()
{
int Size = 20;
int Size2 = 40;

int** pArg = new int* [ Size ];

for( int i = 0; i < Size; ++i )
pArg = new int [ Size2 ];

foo( pArg );

for( int i = 0; i < Size; ++i )
delete [] pArg;

delete [] pArg;

return 1;
}
 
T

tom_usenet

Hi,

I'm currently working on a project in which I link to a library where the
function definitions takes parameters of the type: "const int *const
*paramName"

That's a pointer to a const pointer to const int.
As far as I can tell from the example of usage this type definition requires
a const int array, however, I do not know the size of those arrays at
compile time (due to the nature of other parts of my code), thus I have to
dynamically allocate the arrays by using "new". Passing parameters of this
type results in the compiler error:
'Create' : none of the 4 overloads can convert parameter 3 from type 'int *'

You need to pass in the address of your array.
I have tried type casting or reinterpret_cast, none of which works, also I
tried passing the parameters as "**int", this compiles, but this throws an
exception at runtime.

int* i = new int[100];
libfunc(&i);
will compile, but you'll have to read the documentation of the library
to see what the passed pointer is meant to be pointing at (here I've
assumed it is just meant to be pointing to a single pointer to a 100
ints).
My question is if there is a way to work around this? Or any way to cast the
dynamically allocated arrays to fit the type definition of the functions?

Just pass their addresses.

Tom
 
K

Karl Heinz Buchegger

tom_usenet said:
int* i = new int[100];
libfunc(&i);
will compile, but you'll have to read the documentation of the library
to see what the passed pointer is meant to be pointing at (here I've
assumed it is just meant to be pointing to a single pointer to a 100
ints).

That's not logical.
Why pass the address of the pointer instead of the pointer value itself?

The function can't do anything with that address other then dereference it.
It would be much simpler for the function, if it just takes the pointer value.
That's why I believe that the function expects a '2D' array (pointer to
array of pointers)

On the other side: It could be that way and it would be a valid way to
match that parameter.
Moral of the story: Just knowing the parameter types is often not enough.
One also needs to know what the function is expecting from that parameter.
 
T

tom_usenet

tom_usenet said:
int* i = new int[100];
libfunc(&i);
will compile, but you'll have to read the documentation of the library
to see what the passed pointer is meant to be pointing at (here I've
assumed it is just meant to be pointing to a single pointer to a 100
ints).

That's not logical.
Why pass the address of the pointer instead of the pointer value itself?

It could be an in/out parameter, but, as you go on to say, there are
other possibilities, hence my advice to read the documentation.
Moral of the story: Just knowing the parameter types is often not enough.
One also needs to know what the function is expecting from that parameter.

Agreed.

Tom
 
O

Old Wolf

Karl Heinz Buchegger said:
tom_usenet said:
I'm currently working on a project in which I link to a
library where the function definitions takes parameters of
the type: "const int *const *paramName"

int* i = new int[100];
libfunc(&i);
will compile, but you'll have to read the documentation of the library
to see what the passed pointer is meant to be pointing at (here I've
assumed it is just meant to be pointing to a single pointer to a 100
ints).

That's not logical.
Why pass the address of the pointer instead of the pointer value itself?

It's more logical than having a parameter called "paramName" that
expects an array of pointers to int.

Another possibility would be something like:
typedef wchar_t const *CWSTRING;
void libfunc(CWSTRING const *str);
where wchar_t was defined as int, and the library wanted a consistent
convention (since some lib functions would in act be required to
reallocate the string). There are large APIs which use this style.

Of course, the OP should read his documentation (or if there is none,
read the header file where libfunc is defined for some clues),
as you say.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top