Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12603 > unrolled thread
| Started by | Bint <bint@ign.com> |
|---|---|
| First post | 2011-12-16 18:46 -0600 |
| Last post | 2011-12-18 10:04 -0800 |
| Articles | 20 — 10 participants |
Back to article view | Back to comp.lang.c++
Simple argument question Bint <bint@ign.com> - 2011-12-16 18:46 -0600
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-17 01:13 +0000
Re: Simple argument question Bint <bint@ign.com> - 2011-12-17 12:06 -0600
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-17 18:45 +0000
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-17 23:16 +0000
Re: Simple argument question jt@toerring.de (Jens Thoms Toerring) - 2011-12-18 22:07 +0000
Re: Simple argument question "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-12-18 22:34 +0000
Re: Simple argument question 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 00:13 -0800
Re: Simple argument question Juha Nieminen <nospam@thanks.invalid> - 2011-12-17 08:16 +0000
Re: Simple argument question 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 00:51 -0800
Re: Simple argument question Juha Nieminen <nospam@thanks.invalid> - 2011-12-17 10:23 +0000
Re: Simple argument question Goran <goran.pusic@gmail.com> - 2011-12-17 23:08 -0800
Re: Simple argument question Heck <heckja_in@ordertostymieharvestersverizon.net> - 2012-01-05 09:59 -0500
Re: Simple argument question Krice <paulkp@mbnet.fi> - 2011-12-18 00:46 -0800
Re: Simple argument question jt@toerring.de (Jens Thoms Toerring) - 2011-12-18 22:14 +0000
Re: Simple argument question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-19 12:01 +0000
Re: Simple argument question Tobias Müller <troplin@bluewin.ch> - 2011-12-19 17:17 +0000
Re: Simple argument question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-20 00:33 +0000
Re: Simple argument question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-18 17:03 +0000
Re: Simple argument question 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 10:04 -0800
| From | Bint <bint@ign.com> |
|---|---|
| Date | 2011-12-16 18:46 -0600 |
| Subject | Simple argument question |
| Message-ID | <CB114269.B5F7%bint@ign.com> |
Hi can someone answer a simple question involving arguments to functions?
I need to call a library function which expects an argument like this
Function(double pointlist[][3]);
My question is how do I make one of those if I am just wanting to malloc the
memory? Can I use a double pointer?
Can I have something like this:
double *points = (double *)malloc(10*3*sizeof(double));
for int (j = 0; j < 10; j++) {
*(points + j*3 + 0) = x;
*(points + j*3 + 1) = y;
*(points + j*3 + 2) = z;
}
And then call the function with points as the argument? It doesn't compile
but I was wondering if I could cast it? Or do I need something else instead
of a double pointer for my points?
Does that make sense? I am confused about how to call the function with
anything but something like:
double array[500][3];
I don't want that because I don't know the number of points, see?
Thanks for any help!
B
[toc] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-12-17 01:13 +0000 |
| Message-ID | <jcgqb9$9qu$1@adenine.netfront.net> |
| In reply to | #12603 |
"Bint" <bint@ign.com> wrote in message news:CB114269.B5F7%bint@ign.com...
> Hi can someone answer a simple question involving arguments to functions?
>
> I need to call a library function which expects an argument like this
> Function(double pointlist[][3]);
>
> My question is how do I make one of those if I am just wanting to malloc
> the
> memory? Can I use a double pointer?
>
> Can I have something like this:
> double *points = (double *)malloc(10*3*sizeof(double));
>
> for int (j = 0; j < 10; j++) {
> *(points + j*3 + 0) = x;
> *(points + j*3 + 1) = y;
> *(points + j*3 + 2) = z;
> }
>
> And then call the function with points as the argument? It doesn't
> compile
> but I was wondering if I could cast it? Or do I need something else
> instead
> of a double pointer for my points?
>
> Does that make sense? I am confused about how to call the function with
> anything but something like:
> double array[500][3];
>
> I don't want that because I don't know the number of points, see?
>
Hi.
When you create 2d arrays using new you may use pointer to pointer types ,
but this is a different type from the built in array type.
In your function argument your 2d array will decay into a pointer of type:
double (*)[3]
Each dimensional array is pointed to by a pointer of type one dimension
less. For example a one dimensional array of doubles is pointed to by a
double*( the same as a pointer to a single double ). A 2-d array is pointed
to by a type T(*)[S] . a 3d array is pointed to by a type T(*)[S1][S2] etc
etc.
When you create your pointer with malloc you are creating a pointer of type
double* which is a pointer to a 1d array, or a pointer to a single double.
But the function argument needs you to pass a 2d array.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | Bint <bint@ign.com> |
|---|---|
| Date | 2011-12-17 12:06 -0600 |
| Message-ID | <CB123632.B739%bint@ign.com> |
| In reply to | #12604 |
> "Bint" <bint@ign.com> wrote in message news:CB114269.B5F7%bint@ign.com...
>> Hi can someone answer a simple question involving arguments to functions?
>>
>> I need to call a library function which expects an argument like this
>> Function(double pointlist[][3]);
>>
>> My question is how do I make one of those if I am just wanting to malloc
>> the
>> memory? Can I use a double pointer?
>>
>> Can I have something like this:
>> double *points = (double *)malloc(10*3*sizeof(double));
>>
>> for int (j = 0; j < 10; j++) {
>> *(points + j*3 + 0) = x;
>> *(points + j*3 + 1) = y;
>> *(points + j*3 + 2) = z;
>> }
>>
>> And then call the function with points as the argument? It doesn't
>> compile
>> but I was wondering if I could cast it? Or do I need something else
>> instead
>> of a double pointer for my points?
>>
>> Does that make sense? I am confused about how to call the function with
>> anything but something like:
>> double array[500][3];
>>
>> I don't want that because I don't know the number of points, see?
>>
> Hi.
> When you create 2d arrays using new you may use pointer to pointer types ,
> but this is a different type from the built in array type.
> In your function argument your 2d array will decay into a pointer of type:
> double (*)[3]
>
> Each dimensional array is pointed to by a pointer of type one dimension
> less. For example a one dimensional array of doubles is pointed to by a
> double*( the same as a pointer to a single double ). A 2-d array is pointed
> to by a type T(*)[S] . a 3d array is pointed to by a type T(*)[S1][S2] etc
> etc.
>
> When you create your pointer with malloc you are creating a pointer of type
> double* which is a pointer to a 1d array, or a pointer to a single double.
> But the function argument needs you to pass a 2d array.
>
So how would I create and pass something valid to this function, say for an
array of [10][3], but specifying that 10 at run-time? Do I use new instead
of malloc?
Thanks
B
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-12-17 18:45 +0000 |
| Message-ID | <jcio0k$1tni$1@adenine.netfront.net> |
| In reply to | #12623 |
"Bint" <bint@ign.com> wrote in message news:CB123632.B739%bint@ign.com...
>
>> "Bint" <bint@ign.com> wrote in message news:CB114269.B5F7%bint@ign.com...
>>> Hi can someone answer a simple question involving arguments to
>>> functions?
>>>
>>> I need to call a library function which expects an argument like this
>>> Function(double pointlist[][3]);
>>>
>>> My question is how do I make one of those if I am just wanting to malloc
>>> the
>>> memory? Can I use a double pointer?
>>>
>>> Can I have something like this:
>>> double *points = (double *)malloc(10*3*sizeof(double));
>>>
>>> for int (j = 0; j < 10; j++) {
>>> *(points + j*3 + 0) = x;
>>> *(points + j*3 + 1) = y;
>>> *(points + j*3 + 2) = z;
>>> }
>>>
>>> And then call the function with points as the argument? It doesn't
>>> compile
>>> but I was wondering if I could cast it? Or do I need something else
>>> instead
>>> of a double pointer for my points?
>>>
>>> Does that make sense? I am confused about how to call the function with
>>> anything but something like:
>>> double array[500][3];
>>>
>>> I don't want that because I don't know the number of points, see?
>>>
>> Hi.
>> When you create 2d arrays using new you may use pointer to pointer types
>> ,
>> but this is a different type from the built in array type.
>> In your function argument your 2d array will decay into a pointer of
>> type:
>> double (*)[3]
>>
>> Each dimensional array is pointed to by a pointer of type one dimension
>> less. For example a one dimensional array of doubles is pointed to by a
>> double*( the same as a pointer to a single double ). A 2-d array is
>> pointed
>> to by a type T(*)[S] . a 3d array is pointed to by a type T(*)[S1][S2]
>> etc
>> etc.
>>
>> When you create your pointer with malloc you are creating a pointer of
>> type
>> double* which is a pointer to a 1d array, or a pointer to a single
>> double.
>> But the function argument needs you to pass a 2d array.
>>
>
> So how would I create and pass something valid to this function, say for
> an
> array of [10][3], but specifying that 10 at run-time? Do I use new
> instead
> of malloc?
>
To solve your problem you need to cast the return value from the allocation
function to a suitable pointer type, which is compatable with the function
argument.
IIRC malloc returns a void pointer so intead of casting it to a double*, as
you have done in your example, you can cast it to a double(*)[3]. This
pointer type would then be suitable to pass to the function:
double (*points)[3] = (double (*)[3])malloc(10*3*sizeof(double));
Function( points);
HTH.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-12-17 23:16 +0000 |
| Message-ID | <jcj7t9$2clu$1@adenine.netfront.net> |
| In reply to | #12624 |
"Paul or@yahoo.co.uk>" <pchrist<nospam> wrote in message
news:jcio0k$1tni$1@adenine.netfront.net...
>
> "Bint" <bint@ign.com> wrote in message news:CB123632.B739%bint@ign.com...
>>
>>> "Bint" <bint@ign.com> wrote in message
>>> news:CB114269.B5F7%bint@ign.com...
>>>> Hi can someone answer a simple question involving arguments to
>>>> functions?
>>>>
>>>> I need to call a library function which expects an argument like this
>>>> Function(double pointlist[][3]);
>>>>
>>>> My question is how do I make one of those if I am just wanting to
>>>> malloc
>>>> the
>>>> memory? Can I use a double pointer?
>>>>
>>>> Can I have something like this:
>>>> double *points = (double *)malloc(10*3*sizeof(double));
>>>>
>>>> for int (j = 0; j < 10; j++) {
>>>> *(points + j*3 + 0) = x;
>>>> *(points + j*3 + 1) = y;
>>>> *(points + j*3 + 2) = z;
>>>> }
>>>>
>>>> And then call the function with points as the argument? It doesn't
>>>> compile
>>>> but I was wondering if I could cast it? Or do I need something else
>>>> instead
>>>> of a double pointer for my points?
>>>>
>>>> Does that make sense? I am confused about how to call the function with
>>>> anything but something like:
>>>> double array[500][3];
>>>>
>>>> I don't want that because I don't know the number of points, see?
>>>>
>>> Hi.
>>> When you create 2d arrays using new you may use pointer to pointer types
>>> ,
>>> but this is a different type from the built in array type.
>>> In your function argument your 2d array will decay into a pointer of
>>> type:
>>> double (*)[3]
>>>
>>> Each dimensional array is pointed to by a pointer of type one dimension
>>> less. For example a one dimensional array of doubles is pointed to by a
>>> double*( the same as a pointer to a single double ). A 2-d array is
>>> pointed
>>> to by a type T(*)[S] . a 3d array is pointed to by a type T(*)[S1][S2]
>>> etc
>>> etc.
>>>
>>> When you create your pointer with malloc you are creating a pointer of
>>> type
>>> double* which is a pointer to a 1d array, or a pointer to a single
>>> double.
>>> But the function argument needs you to pass a 2d array.
>>>
>>
>> So how would I create and pass something valid to this function, say for
>> an
>> array of [10][3], but specifying that 10 at run-time? Do I use new
>> instead
>> of malloc?
>>
> To solve your problem you need to cast the return value from the
> allocation function to a suitable pointer type, which is compatable with
> the function argument.
> IIRC malloc returns a void pointer so intead of casting it to a double*,
> as you have done in your example, you can cast it to a double(*)[3]. This
> pointer type would then be suitable to pass to the function:
>
> double (*points)[3] = (double (*)[3])malloc(10*3*sizeof(double));
>
> Function( points);
>
>
> HTH.
>
Note: your for loop will need to be modified as each increment on this type
of pointer will increment it by a complete row of 3 elelments.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2011-12-18 22:07 +0000 |
| Message-ID | <9l76heFm6gU1@mid.uni-berlin.de> |
| In reply to | #12629 |
"Paul" <pchrist <nospam>or@yahoo.co.uk> wrote:
> "Paul or@yahoo.co.uk>" <pchrist<nospam> wrote in message
> news:jcio0k$1tni$1@adenine.netfront.net...
> >
> > "Bint" <bint@ign.com> wrote in message news:CB123632.B739%bint@ign.com...
> >>
> >>> "Bint" <bint@ign.com> wrote in message
> >>> news:CB114269.B5F7%bint@ign.com...
> >>>> Hi can someone answer a simple question involving arguments to
> >>>> functions?
> >>>>
> >>>> I need to call a library function which expects an argument like this
> >>>> Function(double pointlist[][3]);
> >>>>
> >>>> My question is how do I make one of those if I am just wanting to
> >>>> malloc
> >>>> the
> >>>> memory? Can I use a double pointer?
> >>>>
> >>>> Can I have something like this:
> >>>> double *points = (double *)malloc(10*3*sizeof(double));
> >>>>
> >>>> for int (j = 0; j < 10; j++) {
> >>>> *(points + j*3 + 0) = x;
> >>>> *(points + j*3 + 1) = y;
> >>>> *(points + j*3 + 2) = z;
> >>>> }
> >>>>
> >>>> And then call the function with points as the argument? It doesn't
> >>>> compile
> >>>> but I was wondering if I could cast it? Or do I need something else
> >>>> instead
> >>>> of a double pointer for my points?
> >>>>
> >>>> Does that make sense? I am confused about how to call the function with
> >>>> anything but something like:
> >>>> double array[500][3];
> >>>>
> >>>> I don't want that because I don't know the number of points, see?
> >>>>
> >>> Hi.
> >>> When you create 2d arrays using new you may use pointer to pointer types
> >>> ,
> >>> but this is a different type from the built in array type.
> >>> In your function argument your 2d array will decay into a pointer of
> >>> type:
> >>> double (*)[3]
> >>>
> >>> Each dimensional array is pointed to by a pointer of type one dimension
> >>> less. For example a one dimensional array of doubles is pointed to by a
> >>> double*( the same as a pointer to a single double ). A 2-d array is
> >>> pointed
> >>> to by a type T(*)[S] . a 3d array is pointed to by a type T(*)[S1][S2]
> >>> etc
> >>> etc.
> >>>
> >>> When you create your pointer with malloc you are creating a pointer of
> >>> type
> >>> double* which is a pointer to a 1d array, or a pointer to a single
> >>> double.
> >>> But the function argument needs you to pass a 2d array.
> >>>
> >>
> >> So how would I create and pass something valid to this function, say for
> >> an
> >> array of [10][3], but specifying that 10 at run-time? Do I use new
> >> instead
> >> of malloc?
> >>
> > To solve your problem you need to cast the return value from the
> > allocation function to a suitable pointer type, which is compatable with
> > the function argument.
> > IIRC malloc returns a void pointer so intead of casting it to a double*,
> > as you have done in your example, you can cast it to a double(*)[3]. This
> > pointer type would then be suitable to pass to the function:
> >
> > double (*points)[3] = (double (*)[3])malloc(10*3*sizeof(double));
> >
> > Function( points);
> >
> >
> > HTH.
> >
> Note: your for loop will need to be modified as each increment on this type
> of pointer will increment it by a complete row of 3 elelments.
I guess you mean that he should remove the multiplication of
'j' by three. But then, as an added bonus due to getting the
types right, he now could rewrite the initialization loop more
naturally as
for ( size_t j = 0; j < 10; ++j ) {
points[ j ][ 0 ] = x;
points[ j ][ 1 ] = y;
points[ j ][ 2 ] = z;
}
What I am still wondering about though is how the function re-
ceiving the 2-d array is supposed to know how many "points"
it will get - all it receives is a pointer to (the first three
coordinates of) the first point, but no information about how
many more there are...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-12-18 22:34 +0000 |
| Message-ID | <jclppd$27em$1@adenine.netfront.net> |
| In reply to | #12643 |
"Jens Thoms Toerring" <jt@toerring.de> wrote in message
news:9l76heFm6gU1@mid.uni-berlin.de...
> "Paul" <pchrist <nospam>or@yahoo.co.uk> wrote:
>
>> "Paul or@yahoo.co.uk>" <pchrist<nospam> wrote in message
>> news:jcio0k$1tni$1@adenine.netfront.net...
>> >
>> > "Bint" <bint@ign.com> wrote in message
>> > news:CB123632.B739%bint@ign.com...
>> >>
>> >>> "Bint" <bint@ign.com> wrote in message
>> >>> news:CB114269.B5F7%bint@ign.com...
>> >>>> Hi can someone answer a simple question involving arguments to
>> >>>> functions?
>> >>>>
>> >>>> I need to call a library function which expects an argument like
>> >>>> this
>> >>>> Function(double pointlist[][3]);
>> >>>>
>> >>>> My question is how do I make one of those if I am just wanting to
>> >>>> malloc
>> >>>> the
>> >>>> memory? Can I use a double pointer?
>> >>>>
>> >>>> Can I have something like this:
>> >>>> double *points = (double *)malloc(10*3*sizeof(double));
>> >>>>
>> >>>> for int (j = 0; j < 10; j++) {
>> >>>> *(points + j*3 + 0) = x;
>> >>>> *(points + j*3 + 1) = y;
>> >>>> *(points + j*3 + 2) = z;
>> >>>> }
>> >>>>
>> >>>> And then call the function with points as the argument? It doesn't
>> >>>> compile
>> >>>> but I was wondering if I could cast it? Or do I need something else
>> >>>> instead
>> >>>> of a double pointer for my points?
>> >>>>
>> >>>> Does that make sense? I am confused about how to call the function
>> >>>> with
>> >>>> anything but something like:
>> >>>> double array[500][3];
>> >>>>
>> >>>> I don't want that because I don't know the number of points, see?
>> >>>>
>> >>> Hi.
>> >>> When you create 2d arrays using new you may use pointer to pointer
>> >>> types
>> >>> ,
>> >>> but this is a different type from the built in array type.
>> >>> In your function argument your 2d array will decay into a pointer of
>> >>> type:
>> >>> double (*)[3]
>> >>>
>> >>> Each dimensional array is pointed to by a pointer of type one
>> >>> dimension
>> >>> less. For example a one dimensional array of doubles is pointed to by
>> >>> a
>> >>> double*( the same as a pointer to a single double ). A 2-d array is
>> >>> pointed
>> >>> to by a type T(*)[S] . a 3d array is pointed to by a type
>> >>> T(*)[S1][S2]
>> >>> etc
>> >>> etc.
>> >>>
>> >>> When you create your pointer with malloc you are creating a pointer
>> >>> of
>> >>> type
>> >>> double* which is a pointer to a 1d array, or a pointer to a single
>> >>> double.
>> >>> But the function argument needs you to pass a 2d array.
>> >>>
>> >>
>> >> So how would I create and pass something valid to this function, say
>> >> for
>> >> an
>> >> array of [10][3], but specifying that 10 at run-time? Do I use new
>> >> instead
>> >> of malloc?
>> >>
>> > To solve your problem you need to cast the return value from the
>> > allocation function to a suitable pointer type, which is compatable
>> > with
>> > the function argument.
>> > IIRC malloc returns a void pointer so intead of casting it to a
>> > double*,
>> > as you have done in your example, you can cast it to a double(*)[3].
>> > This
>> > pointer type would then be suitable to pass to the function:
>> >
>> > double (*points)[3] = (double (*)[3])malloc(10*3*sizeof(double));
>> >
>> > Function( points);
>> >
>> >
>> > HTH.
>> >
>> Note: your for loop will need to be modified as each increment on this
>> type
>> of pointer will increment it by a complete row of 3 elelments.
>
> I guess you mean that he should remove the multiplication of
> 'j' by three. But then, as an added bonus due to getting the
> types right, he now could rewrite the initialization loop more
> naturally as
>
> for ( size_t j = 0; j < 10; ++j ) {
> points[ j ][ 0 ] = x;
> points[ j ][ 1 ] = y;
> points[ j ][ 2 ] = z;
> }
Yes I was thinking of suggesting this to the OP. It's much nicer this way.
>
> What I am still wondering about though is how the function re-
> ceiving the 2-d array is supposed to know how many "points"
> it will get - all it receives is a pointer to (the first three
> coordinates of) the first point, but no information about how
> many more there are...
This was also what I thought. I guess the OP has simplified the function
and the 'actual' lib function will also have a size parameter. Or maybe the
function has access to the runtime size parameter by some other channel,
IDK.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2011-12-17 00:13 -0800 |
| Message-ID | <5614179.530.1324109622097.JavaMail.geo-discussion-forums@prol32> |
| In reply to | #12603 |
On Saturday, December 17, 2011 8:46:01 AM UTC+8, Bint wrote:
> Hi can someone answer a simple question involving arguments to functions?
>
> I need to call a library function which expects an argument like this
> Function(double pointlist[][3]);
>
> My question is how do I make one of those if I am just wanting to malloc the
> memory? Can I use a double pointer?
>
> Can I have something like this:
> double *points = (double *)malloc(10*3*sizeof(double));
>
> for int (j = 0; j < 10; j++) {
> *(points + j*3 + 0) = x;
> *(points + j*3 + 1) = y;
> *(points + j*3 + 2) = z;
> }
>
> And then call the function with points as the argument? It doesn't compile
> but I was wondering if I could cast it? Or do I need something else instead
> of a double pointer for my points?
>
> Does that make sense? I am confused about how to call the function with
> anything but something like:
> double array[500][3];
>
> I don't want that because I don't know the number of points, see?
>
> Thanks for any help!
> B
The size of a double is platform dependent.
Is the compiler smart enough to figure out the correct value of
points + j*3 ?
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-12-17 08:16 +0000 |
| Message-ID | <4eec4fcd$0$3118$7b1e8fa0@news.nbl.fi> |
| In reply to | #12613 |
88888 Dihedral <dihedral88888@googlemail.com> wrote: > Is the compiler smart enough to figure out the correct value of > points + j*3 ? If it weren't, then the compiler would be non-standard. What do you think "points + j*3" means?
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2011-12-17 00:51 -0800 |
| Message-ID | <15988506.513.1324111864310.JavaMail.geo-discussion-forums@prj1> |
| In reply to | #12614 |
On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote: > 88888 Dihedral <dihedr...@googlemail.com> wrote: > > Is the compiler smart enough to figure out the correct value of > > points + j*3 ? > > If it weren't, then the compiler would be non-standard. > > What do you think "points + j*3" means? points+1 means add to ponits 1 size of a double but if a double has to start at some limited address, e.g. 8 byte boundaries only , some strange thing might happen if the compiler is not smart enough
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-12-17 10:23 +0000 |
| Message-ID | <4eec6d8e$0$2763$7b1e8fa0@news.nbl.fi> |
| In reply to | #12616 |
88888 Dihedral <dihedral88888@googlemail.com> wrote: > On Saturday, December 17, 2011 4:16:13 PM UTC+8, Juha Nieminen wrote: >> 88888 Dihedral <dihedr...@googlemail.com> wrote: >> > Is the compiler smart enough to figure out the correct value of >> > points + j*3 ? >> >> If it weren't, then the compiler would be non-standard. >> >> What do you think "points + j*3" means? > > points+1 means add to ponits 1 size of a double but if a double has to start at some limited address, e.g. 8 byte boundaries only , some strange thing might happen if the compiler is not smart enough If "points" is a valid pointer to an array (of at least 2 elements), then why wouldn't "points + 1" be a valid pointer?
[toc] | [prev] | [next] | [standalone]
| From | Goran <goran.pusic@gmail.com> |
|---|---|
| Date | 2011-12-17 23:08 -0800 |
| Message-ID | <aae8891f-d0e5-4edf-9dda-4fd7eba92cd7@f1g2000yqi.googlegroups.com> |
| In reply to | #12603 |
On Dec 17, 1:46 am, Bint <b...@ign.com> wrote:
> Hi can someone answer a simple question involving arguments to functions?
>
> I need to call a library function which expects an argument like this
> Function(double pointlist[][3]);
>
> My question is how do I make one of those if I am just wanting to malloc the
> memory? Can I use a double pointer?
>
> Can I have something like this:
> double *points = (double *)malloc(10*3*sizeof(double));
>
> for int (j = 0; j < 10; j++) {
> *(points + j*3 + 0) = x;
> *(points + j*3 + 1) = y;
> *(points + j*3 + 2) = z;
>
> }
>
> And then call the function with points as the argument? It doesn't compile
> but I was wondering if I could cast it? Or do I need something else instead
> of a double pointer for my points?
>
> Does that make sense? I am confused about how to call the function with
> anything but something like:
> double array[500][3];
>
> I don't want that because I don't know the number of points, see?
Problem decomposition to the rescue! ;-)
Here's a possibly better way to look at things: double pointlist [][3]
says that "Function" receives an array of double[3] (do you know of
cdecl.org? Try it, it helps with such things). So let's call that D3:
typedef double D3[3];
(More precisely, Function receives a pointer to a D3 (D3* pointlist),
but one can write that as double pointlist[][3], or D3 pointlist[];
that is, all of
void Function(double pointlist[][3]);
void Function(D3 pointlist[]);
void Function(D3* pointlist);
are one and the same as far as compiler is concerned. IMO, the third
form is best because it conveys the meaning the best, and avoids much
of mental juggling about the "feature" of C in play here, the array
decaying to a pointer when appearing in an expression.)
Now... If you decide to go for C++ route, things get easier on the
call side:
void Function(D3* pointlist) {...}
D3* p = new D3[10];
Function(p);
If you still want pure C, things are again a bit easier (IMO):
D3* p = malloc(10*sizeof(D3));
Function(p);
Finally, as others noted, "points + j*3 + 0" is wrong. You need
points[j][0]. Again, this is because "points" is a pointer to a D3,
and when you increment it, you get a pointer to the next D3 (not a
pointer to a double). And, of course, when you dereference that, you
get a D3, not double, so about that casting: no, that's the wrong
idea. Further, IMO, in pointer manipulation such as the above, a cast
indicates a bug.
Goran.
[toc] | [prev] | [next] | [standalone]
| From | Heck <heckja_in@ordertostymieharvestersverizon.net> |
|---|---|
| Date | 2012-01-05 09:59 -0500 |
| Message-ID | <gpdbg7hbv3rlcra7t0roma4hc64i7vdc8l@4ax.com> |
| In reply to | #12635 |
I would have answered, use double **points = (double **)malloc(10*3*sizeof(double)); , to illustrate the principles involved, but I know better. Building a type up with successive typedefs is also an old C technique and here it illustrates the principles, too, no quarrel. On Sat, 17 Dec 2011 23:08:30 -0800 (PST), Goran <goran.pusic@gmail.com> wrote: >Problem decomposition to the rescue! ;-) > >typedef double D3[3];
[toc] | [prev] | [next] | [standalone]
| From | Krice <paulkp@mbnet.fi> |
|---|---|
| Date | 2011-12-18 00:46 -0800 |
| Message-ID | <8ddcc541-8997-4a44-8f25-a2eaa49a74bd@l29g2000yqf.googlegroups.com> |
| In reply to | #12603 |
On 17 joulu, 02:46, Bint <b...@ign.com> wrote: > And then call the function with points as the argument? It doesn't compile Let's rewrite this to C++: 1. Create a point class or struct with x, y, z variables 2. Use std::vector to store them if you don't know the size of the array 3. Pass vector's reference to a function processing the array
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2011-12-18 22:14 +0000 |
| Message-ID | <9l76ttFm6gU2@mid.uni-berlin.de> |
| In reply to | #12636 |
Krice <paulkp@mbnet.fi> wrote:
> On 17 joulu, 02:46, Bint <b...@ign.com> wrote:
> > And then call the function with points as the argument? It doesn't compile
> Let's rewrite this to C++:
> 1. Create a point class or struct with x, y, z variables
> 2. Use std::vector to store them if you don't know the size of the
> array
> 3. Pass vector's reference to a function processing the array
Fine as long as the function receiving the array of points isn't
some external function from a library written in C that can't be
simply made to work with C++ classes and vectors...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-12-19 12:01 +0000 |
| Message-ID | <slrnjeu9sp.1ls.grahn+nntp@frailea.sa.invalid> |
| In reply to | #12644 |
On Sun, 2011-12-18, Jens Thoms Toerring wrote: > Krice <paulkp@mbnet.fi> wrote: >> On 17 joulu, 02:46, Bint <b...@ign.com> wrote: >> > And then call the function with points as the argument? It doesn't compile > >> Let's rewrite this to C++: > >> 1. Create a point class or struct with x, y, z variables >> 2. Use std::vector to store them if you don't know the size of the >> array >> 3. Pass vector's reference to a function processing the array > > Fine as long as the function receiving the array of points isn't > some external function from a library written in C that can't be > simply made to work with C++ classes and vectors... Surely we cannot assume that is the case? He /did/ post to comp.lang.c++ and said nothing about such limitations as far as I can tell. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | Tobias Müller <troplin@bluewin.ch> |
|---|---|
| Date | 2011-12-19 17:17 +0000 |
| Message-ID | <1429617533346006136.052558troplin-bluewin.ch@news.aioe.org> |
| In reply to | #12657 |
Jorgen Grahn <grahn+nntp@snipabacken.se> wrote: > Surely we cannot assume that is the case? He /did/ post to > comp.lang.c++ and said nothing about such limitations as far as I can > tell. > > /Jorgen He /did/ say that he wants to call a library function. Whether that library is in C or C++ is not important, since the signature is valid C++. Normally if someone speaks of library function, he means a function that he cannot modify himself. And even if he could, he asked how to /call/ that function not how to turn the function into beautiful C++. Of course it is not bad to give advice how to write beautiful C++ code, but you cannot say that the question was not detailed enough. Tobi
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-12-20 00:33 +0000 |
| Message-ID | <slrnjevlvg.1ls.grahn+nntp@frailea.sa.invalid> |
| In reply to | #12664 |
On Mon, 2011-12-19, Tobias Müller wrote: > Jorgen Grahn <grahn+nntp@snipabacken.se> wrote: > >> Surely we cannot assume that is the case? He /did/ post to >> comp.lang.c++ and said nothing about such limitations as far as I can >> tell. >> >> /Jorgen > > He /did/ say that he wants to call a library function. Whether that library > is in C or C++ is not important, since the signature is valid C++. Oops, my mistake. He did write: >>> I need to call a library function which expects an argument like this >>> Function(double pointlist[][3]); It's an unfortunate choice of interface even for a C function IMHO, but it's probably not something he has control over. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-12-18 17:03 +0000 |
| Message-ID | <slrnjes76d.1ls.grahn+nntp@frailea.sa.invalid> |
| In reply to | #12603 |
On Sat, 2011-12-17, Bint wrote: > Hi can someone answer a simple question involving arguments to functions? > > I need to call a library function which expects an argument like this > Function(double pointlist[][3]); > > My question is how do I make one of those if I am just wanting to malloc the > memory? Can I use a double pointer? ... Switch to a datatype which uses std::vector, or perhaps vectors of vectors. Much easier and safer to handle. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2011-12-18 10:04 -0800 |
| Message-ID | <11732365.338.1324231464496.JavaMail.geo-discussion-forums@prmw6> |
| In reply to | #12637 |
On Monday, December 19, 2011 1:03:09 AM UTC+8, Jorgen Grahn wrote: > On Sat, 2011-12-17, Bint wrote: > > Hi can someone answer a simple question involving arguments to functions? > > > > I need to call a library function which expects an argument like this > > Function(double pointlist[][3]); > > > > My question is how do I make one of those if I am just wanting to malloc the > > memory? Can I use a double pointer? > ... > > Switch to a datatype which uses std::vector, or perhaps vectors of > vectors. Much easier and safer to handle. > > /Jorgen But that means slower most of the time. There's no free lunch. I prefer to re-size my own pointer manually at certain situations only but this not for a novice.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web