Managed arrays and pointer arithmetic 
Author Message
 Managed arrays and pointer arithmetic

Is there an easy (and safe) way to point at a specific region of a gc array
without pinning pointers? Something equivalent to the following (unmanaged)
code?

class CFoo
{
public:
    char m_cChars[32];
    char * m_pBegin;
    char * m_pMiddle;
    CFoo()
    {
        m_pBegin = m_cChars;
        m_pMiddle = m_cChars + 16;    // This is what I'm interested in.
        // Changes to the array need to be visible through the pointers and
        // changes to the values at the pointers need to be visible in the
array
    }

Quote:
};

Is array index notation the only way I can accomplish this in managed code?
I'm in the process of porting a class that relies heavily on pointer
arithmetic and it would be sort of inconvenient to have to rewrite a good
80% of the class. :-)

Any help would be greatly appreciated. Thanks in advance.



Mon, 11 Apr 2005 23:58:24 GMT  
 Managed arrays and pointer arithmetic
Ideally, that's how I'd like to do it. However, those would trigger the
ever-popular "cannot declare interior __gc pointer as a member..." error.
Unless, perhaps, you know of a way to hack around that.... ;-)



Quote:

> > Is there an easy (and safe) way to point at a specific region of a gc
> > array without pinning pointers?

> If you are using managed pointers (__gc *), then you do not need to pin.
> You only need to pin if you are going to use a native pointer (__nogc *).
> Note that native pointers are the only kind of pointer that native code
has,
> so pinning is necessary if you are going to pass a pointer to native code.

> > Something equivalent to the following (unmanaged) code?

> It would be written like this:

>     char m_cChars __gc[32];
>     char __gc * m_pBegin;
>     char __gc * m_pMiddle;

> Then the rest of your code is written as before.

> Cheerio!

> --
> Brandon Bray                                          Visual C++ Compiler
> This posting is provided AS IS with no warranties, and confers no rights.



Tue, 12 Apr 2005 04:41:04 GMT  
 Managed arrays and pointer arithmetic

Quote:

> Is there an easy (and safe) way to point at a specific region of a gc
> array without pinning pointers?

If you are using managed pointers (__gc *), then you do not need to pin.
You only need to pin if you are going to use a native pointer (__nogc *).
Note that native pointers are the only kind of pointer that native code has,
so pinning is necessary if you are going to pass a pointer to native code.

Quote:
> Something equivalent to the following (unmanaged) code?

It would be written like this:

    char m_cChars __gc[32];
    char __gc * m_pBegin;
    char __gc * m_pMiddle;

Then the rest of your code is written as before.

Cheerio!

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Tue, 12 Apr 2005 03:29:53 GMT  
 Managed arrays and pointer arithmetic

Quote:

> Ideally, that's how I'd like to do it. However, those would trigger the
> ever-popular "cannot declare interior __gc pointer as a member..." error.
> Unless, perhaps, you know of a way to hack around that.... ;-)

Doh! I wasn't thinking.

There really isn't going to be an easy way to change your code.  You'll end
up having to store indexes rather than pointers (as you already mentioned).
For the interested readers, an interior pointer is one that can only exist
on the stack.  When one points to something in the gc heap, the garbage
collector will update the pointer when the object it is pointing to moves.
If an interior pointer were part of another object, it would increase the
complexity of the garbage collector; so they disallowed it.

I wish I had a better suggestion for you, but without knowing the context
with which you use the pointers, I can't really say what would be the
"managed way" of doing things.

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Tue, 12 Apr 2005 05:30:16 GMT  
 Managed arrays and pointer arithmetic
You could replace your pointers with "Smart" pointer objects that know about
__gc arrays, indexes and have enough overloaded operators to get the job
done in the context of the legacy code.  If you really need to keep the
arrays from being pinned, you could double-indirectly refer to the array by
using an intermediate managed object whose sole purpose is to reference the
array.  This way, you can pin down the intermediate object instead of the
array using the gcroot<> template.

-Sean


Quote:
> Is there an easy (and safe) way to point at a specific region of a gc
array
> without pinning pointers? Something equivalent to the following
(unmanaged)
> code?

> class CFoo
> {
> public:
>     char m_cChars[32];
>     char * m_pBegin;
>     char * m_pMiddle;
>     CFoo()
>     {
>         m_pBegin = m_cChars;
>         m_pMiddle = m_cChars + 16;    // This is what I'm interested in.
>         // Changes to the array need to be visible through the pointers
and
>         // changes to the values at the pointers need to be visible in the
> array
>     }
> };

> Is array index notation the only way I can accomplish this in managed
code?
> I'm in the process of porting a class that relies heavily on pointer
> arithmetic and it would be sort of inconvenient to have to rewrite a good
> 80% of the class. :-)

> Any help would be greatly appreciated. Thanks in advance.



Tue, 12 Apr 2005 07:53:58 GMT  
 Managed arrays and pointer arithmetic
I've elected to go with the overloaded operators approach. The legacy code
in question is a Flex-generated scanner. Very pointer-heavy. Eventually I'd
like to teach Flex to generate managed scanners on its own, but first I have
to build a working scanner to use as a skeleton.

Anyway, it seems to be going smoothly (aside from the fact that
op_*Assignment operators don't map to infix operators *sigh*). One thing
concerns me, though. The Array base class methods appear to rely rather
heavily on System::Object. Considering the fact that I'm going to be
inserting a bunch of value types (namely, Bytes) is the boxing/unboxing
penalty going to be greater than the cost of marshalling across the
managed/unmanaged boundary? Or are arrays of value types somehow optimized?



Sat, 16 Apr 2005 01:00:43 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. qsorting & managing struct arrays, pointer arrays

2. Managed array of managed arrays of char

3. arrays of strings & possible pointer arithmetic

4. 3D arrays and pointer arithmetic

5. pointer arithmetic with 2d arrays?

6. Pointer Array Arithmetic.

7. pointer to managed array

8. arrays / pointers in managed classes

9. How to manage array of __gc pointers

10. moving arrays from un-managed to managed code.

11. pointer arithmetic with a pointer to void

12. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

 

 
Powered by phpBB® Forum Software