Re: [OT] Re: Passing a std::vector pointer as a long ???

Discussion in 'C++' started by Jorgen Grahn, Nov 11, 2010.

  1. Jorgen Grahn

    Jorgen Grahn Guest

    On Wed, 2010-11-10, Paavo Helde wrote:
    > Peter Olcott <> wrote in
    > news::
    >> On 11/10/2010 2:17 PM, Paavo Helde wrote:
    >>> Peter Olcott<> wrote in
    >>> news::
    >>>> std::vector<int> Integers;
    >>>> I have to interface with a Microsoft callback function and must cast
    >>>> every parameter to (LPARAM) which is defined as a long.
    >>> In Windows, LPARAM is defined as LONG_PTR, which is (despite the name)
    >>> not a pointer, but an integral type capable of holding a pointer. It may
    >>> be finally defined as long, but this is incidental and does not hold on
    >>> all platforms Windows is supporting.
    >>>> I need to pass a std::vector to this function. I always do this using
    >>>> references, and not pointers. In this case it seems that I have to do it
    >>>> using a pointer.
    >>>> What is the syntax for casting a pointer to a std::vector to and from a
    >>>> long?
    >>>> (long)&Integers; // to long, compiles
    >>> Wrong, this is not portable even in the niche of Windows. If the
    >>> interface is defined in terms of LPARAM, then this is what you must use:
    >>> LPARAM x = (LPARAM)&Integers;

    >> I phrased it the way that I did because I wanted people that knew
    >> nothing about Windows to be able to answer my question.

    > Unfortunately, people who do not know anything about Windows cannot help
    > you here as cross-casting pointers to integers and back is not portable
    > and some architectures do not support that at all IIRC.

    We can help by saying "don't do that". Mr Persson mentioned elsewhere
    about passing an index into a std::vector. I bet that is feasible and
    turns out a cleaner solution in the end.


    // Jorgen Grahn <grahn@ Oo o. . .
    \X/> O o .
    Jorgen Grahn, Nov 11, 2010
    1. Advertisements

  2. Leigh Johnston <> wrote:
    > There is nothing fundamentally wrong with using reinterpret_cast for
    > perform integer/pointer conversion. From the draft standard:
    > "A pointer can be explicitly converted to any integral type large enough
    > to hold it.

    The problem is that it's possible for a pointer to be larger than long
    (and this is not just theoretical, as it happens in actual compilers;
    more precisely, if you compile a 64-bit executable for Windows using
    Microsoft's compiler, pointers will be 64-bit but long will be 32-bit.)
    Juha Nieminen, Nov 11, 2010
    1. Advertisements

  3. Peter Olcott <> wrote:
    > Writing portable code that utterly depends on Microsoft specific
    > functions does not make as much sense as other cases. The code in
    > question was used to enumerate all of the fonts on the system. This is
    > OS dependent. There is not much sense (except in forming good coding
    > habits) of writing portable code that is OS dependent (thus inherently
    > not portable).

    But sometimes you need to take into account portability even inside
    the same system. In this instance in particular, if you use a long to
    store a pointer, it will work ok if you compile a 32-bit Windows binary
    but not if you compile a 64-bit Windows binary. The underlying OS can be
    exactly the same, but the result may work or not depending on which type
    of binary you are compiling.
    Juha Nieminen, Nov 12, 2010
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    Feb 18, 2006
  4. Rune Allnor
    Rune Allnor
    Dec 11, 2008
  5. Joshua Maurice
    Joshua Maurice
    Nov 12, 2010

Share This Page