Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic creation of objects

Thomas Hede Jensen
Guest
 
Posts: n/a
#1: Jul 22 '05
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



Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Dynamic creation of objects


Thomas Hede Jensen wrote:[color=blue]
>
> 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"[/color]

Thats a pointer to a pointer or a pointer to an array of pointers.
[color=blue]
> As far as I can tell from the example of usage this type definition requires
> a const int array,[/color]

It requires a pointer to an array of pointers
[color=blue]
> 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.[/color]

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[i] = new int [ Size2 ];

foo( pArg );

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

delete [] pArg;

return 1;
}

--
Karl Heinz Buchegger
kbuchegg@gascad.at
tom_usenet
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Dynamic creation of objects


On Wed, 28 Apr 2004 13:31:24 +0200, "Thomas Hede Jensen"
<thomashedejensen@hotmail.com> wrote:
[color=blue]
>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"[/color]

That's a pointer to a const pointer to const int.
[color=blue]
>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 *'[/color]

You need to pass in the address of your array.
[color=blue]
>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.[/color]

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).
[color=blue]
>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?[/color]

Just pass their addresses.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Karl Heinz Buchegger
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Dynamic creation of objects


tom_usenet wrote:[color=blue]
>
> 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).[/color]

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.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
tom_usenet
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Dynamic creation of objects


On Wed, 28 Apr 2004 15:29:36 +0200, Karl Heinz Buchegger
<kbuchegg@gascad.at> wrote:
[color=blue]
>tom_usenet wrote:[color=green]
>>
>> 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).[/color]
>
>That's not logical.
>Why pass the address of the pointer instead of the pointer value itself?[/color]

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.
[color=blue]
>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.[/color]

Agreed.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Old Wolf
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Dynamic creation of objects


Karl Heinz Buchegger <kbuchegg@gascad.at> wrote:[color=blue]
> tom_usenet wrote:[color=green]
> > <thomashedejensen@hotmail.com> wrote:[color=darkred]
> > >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"[/color]
> >
> > 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).[/color]
>
> That's not logical.
> Why pass the address of the pointer instead of the pointer value itself?[/color]

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.
Closed Thread