Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3732 > unrolled thread

Passing an integer to a function that accepts a void pointer

Started bypozz <pozzugno@gmail.com>
First post2011-05-11 03:01 -0700
Last post2011-05-11 21:27 +0100
Articles 2 on this page of 62 — 21 participants

Back to article view | Back to comp.lang.c


Contents

  Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-11 03:01 -0700
    Re: Passing an integer to a function that accepts a void pointer Tom St Denis <tom@iahu.ca> - 2011-05-11 03:52 -0700
      Re: Passing an integer to a function that accepts a void pointer pete <pfiland@mindspring.com> - 2011-05-11 08:10 -0400
      Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-11 18:28 +0200
        Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-11 22:10 -0500
          Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-12 19:44 +0200
            Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 14:16 -0400
              Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-12 21:21 +0200
                Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 16:05 -0400
                  Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-13 23:39 +0200
                    Re: Passing an integer to a function that accepts a void pointer Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-14 09:41 +0100
            Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 22:07 -0400
              Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-13 23:12 +0200
                Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-13 23:30 -0400
    Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-11 04:03 -0700
      Re: Passing an integer to a function that accepts a void pointer Heikki Kallasjoki <see@end.of.message.invalid> - 2011-05-11 13:03 +0000
        Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-11 06:46 -0700
          Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-11 08:24 -0700
            Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-11 08:44 -0700
              Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-11 08:58 -0700
                Re: Passing an integer to a function that accepts a void pointer gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-11 17:38 +0000
                  Re: Passing an integer to a function that accepts a void pointer Kenneth Brody <kenbrody@spamcop.net> - 2011-05-11 14:57 -0400
                  Re: Passing an integer to a function that accepts a void pointer Paul N <gw7rib@aol.com> - 2011-05-11 14:06 -0700
                    Re: Passing an integer to a function that accepts a void pointer Michael Press <rubrum@pacbell.net> - 2011-05-12 13:59 -0700
                      Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 14:12 -0700
                        Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 15:27 -0700
                          Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-12 22:37 +0000
                            Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-13 10:41 +1200
                              Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 17:57 -0500
                                Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-13 11:05 +1200
                                  Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-12 23:19 +0000
                                    Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 16:32 -0700
                                      Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 19:13 -0500
                                        Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 18:18 -0700
                                Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 16:18 -0700
                                  Re: Passing an integer to a function that accepts a void pointer Tobias Blass <tobiasblass@gmx.net> - 2011-05-13 08:24 +0000
                                    Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-13 01:33 -0700
                                      Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-13 08:36 +0000
                                        Re: Passing an integer to a function that accepts a void pointer "madamemandm@yahoo.com" <madamemandm@yahoo.com> - 2011-05-13 07:08 -0700
                          Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 15:59 -0700
              Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-11 21:46 +0000
                Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 00:56 -0700
                  Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-12 20:06 +1200
                    Re: Passing an integer to a function that accepts a void pointer Heikki Kallasjoki <see@end.of.message.invalid> - 2011-05-12 08:59 +0000
                      Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-13 10:39 +1200
          Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 06:51 -0400
            Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 04:08 -0700
              Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 07:33 -0400
              Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 07:58 -0700
                Re: Passing an integer to a function that accepts a void pointer gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-12 15:23 +0000
                Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 13:36 -0400
                  Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 12:03 -0700
              Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-12 15:57 +0000
            Re: Passing an integer to a function that accepts a void pointer Michael Press <rubrum@pacbell.net> - 2011-05-12 14:14 -0700
              Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 22:20 -0400
                Re: Passing an integer to a function that accepts a void pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-14 02:39 +0100
    Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-11 07:37 -0400
    Re: Passing an integer to a function that accepts a void pointer James Kuyper <jameskuyper@verizon.net> - 2011-05-11 09:42 -0400
    Re: Passing an integer to a function that accepts a void pointer Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-11 11:53 -0400
      Re: Passing an integer to a function that accepts a void pointer Fred <fred.l.kleinschmidt@boeing.com> - 2011-05-11 10:44 -0700
        Re: Passing an integer to a function that accepts a void pointer Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-12 07:58 -0400
      Re: Passing an integer to a function that accepts a void pointer Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-11 21:27 +0100

Page 4 of 4 — ← Prev page 1 2 3 [4]


#3797

FromDatesfat Chicks <datesfat.chicks@gmail.com>
Date2011-05-12 07:58 -0400
Message-ID<ebins69i705tk8jpgs3ojejic56s6r4lks@4ax.com>
In reply to#3769
On Wed, 11 May 2011 10:44:39 -0700 (PDT), Fred
<fred.l.kleinschmidt@boeing.com> wrote:
>>
>> I must be missing something.
>>
>> What library callback are you referring to?
>>
>
>One example might be the XtCallback on Unix:
>
>void CallbackProc( Widget w, XtPointer clientData, XtPointer
>callData )
>
>where XtPointer is defined as a "void *".
>
>The app registers the callback using:
>XtAddCallback( myWidget, XtXxxxCallback, CallbackProc, clientData );
>
>where clientData was often some integer flag. Passing a pointer to
>an integer would require that integer to remain in scope globally,
>since the registration code and the callback code might be in
>different compilation units.
>
>Many of the examples in the Flannagan's O'Reilly series of
>manuals do this sort of thing.
>
>This type of coding, of course, should not be recommended, and
>can cause a lot of headaches when porting to a 64-bit platform
>where int and pointer are different sizes.

Thanks.  I understand what the OP was speaking of now.

The example you cited seems like bad interface design.  I'll explain
that remark in a moment.

I do a lot of work with embedded systems.  Some have unusual
architectures, such as:

a)A 24-bit program counter, AND

b)All RAM located in first 2^16 memory addresses (so that a 16-bit
pointer is adequate), AND

c)const data (i.e. lookup tables and so on) placed by the development
tools in FLASH in the first 2^16 memory addresses, so that a 16-bit
pointer is adequate.

The end result of this is that a pointer to a function and a pointer
to data are two different things.  One is 24 bits, and the other is 16
bits.

So, in a function interface, not allowing (void *) to point to a
function is reasonable.  Data only.

Also, using (void *) is reasonable.  That is the intent of (void *).
One needs a way to specify a "generic" data pointer.

But the interface for the function should specify (somehow) that the
data pointed to is copied out before the function returns.

Requiring permanent lifetime for the data whose pointer is passed is a
BAD interface decision.  It seems to make things awkward.

Passing an integer as a pointer, as you agree, is also a BAD decision.

The two bad decisions are related.  The first one leads to the
temptation to do the second one.

DFC

[toc] | [prev] | [next] | [standalone]


#3774

FromDr Nick <3-nospam@temporary-address.org.uk>
Date2011-05-11 21:27 +0100
Message-ID<87y62df0e1.fsf@temporary-address.org.uk>
In reply to#3764
Datesfat Chicks <datesfat.chicks@gmail.com> writes:

> On Wed, 11 May 2011 03:01:49 -0700 (PDT), pozz <pozzugno@gmail.com>
> wrote:
>
>>What do you think about this code?
>>
>>int foo(const void *Args) {
>>  printf("%d\n", (const int)Args);
>>  return 0;
>>}
>>
>>int main(int argc, char *argv[]) {
>>  foo((void *)10);
>>  return 0;
>>}
>>
>>It seems work and it should be nice if it is a sufficient portable way
>>to pass integers as pointers.
>>
>>Why don't I declare foo() to directly accept int? Because it is a
>>callback of a library I can't change and the prototype of the callback
>>is well defined by the library to accept void pointer.
>
> I must be missing something.
>
> What library callback are you referring to?

An example might be the one I was criticising qsort for not providing in
another thread.

It's generally useful to make any function that uses a callback take a
void * parameter that it passes untouched to the callback.  This can
contain any information the callback needs, or can even collect
information from the callback.

If you do need to pass an integer, you need some way to put it into a
void *.  This is one way - that I wouldn't use: I'd use an int variable
and take its address as previously suggested.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

[toc] | [prev] | [standalone]


Page 4 of 4 — ← Prev page 1 2 3 [4]

Back to top | Article view | comp.lang.c


csiph-web