- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a problem. I want to use reference pointer(as array
for example:
class ApplyFoo {
char *const my_a;
public:
void operator( )( const blocked_range
char *a = my_a;
for( size_t i=r.begin(); i!=r.end( ); ++i )
Foo(a);
}
ApplyFoo( char a[] ) :my_a(a){}
};
void ParallelApplyFoo( char a[], size_t n ) {
parallel_for(blocked_range
}
I want to replace char* with array
Thanks.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a problem. I want to use reference pointer(as array
I want to replace char* with array
There are several ways.
First is to use gcroot<> wrapper which is able to hold a reference to an object in managed heap. It uses InteropServices internally to accomplish this task.
[cpp]#includeclass ApplyFoo { gcroot a; ApplyFoo(array ^ aa) { a = aa; } }; [/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are several ways.
First is to use gcroot<> wrapper which is able to hold a reference to an object in managed heap. It uses InteropServices internally to accomplish this task.
[cpp]#include
class ApplyFoo
{
gcroot a;
ApplyFoo(array^ aa)
{
a = aa;
}
};
[/cpp]
S@#$! Syntaxhighlighter eats templates!
#include
class ApplyFoo
{
gcroot
ApplyFoo(array
{
a = aa;
}
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are several ways.
Second is to use GCHandle class. Here you have to manualy allocate and free references to managed objects:
using namespace System;
using namespace System::Runtime::InteropServices;
class ApplyFoo
{
IntPtr a;
ApplyFoo(array^ aa)
{
a = static_cast(GCHandle::Alloc(aa));
}
~ApplyFoo()
{
static_cast(a).Free();
}
void f()
{
array^ aa = safe_cast ^>(static_cast (a).Target);
// use aa
}
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are several ways.
Third is to 'pin' managed object. When you pin the object, you are able to get native pointer to it. While there is a live pin_ptr for object, it won't move in managed heap. Native pointer is valid only while pin_ptr exists.
struct ApplyFoo
{
char* a;
ApplyFoo(char* aa)
{
a = aa;
}
void operator () (tbb::blocked_rangeconst& r) const {}
};
void ParallelApplyFoo(array^ a, size_t n)
{
pin_ptraa = &a[0];
tbb::parallel_for(tbb::blocked_range(0, n, 1), ApplyFoo(aa));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
And what about standard C++? I checked online, having never heard of these constructs... they all seem to be MS creations outside the standard.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your answer has a lot of favor for me. Thank you for your help very much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
And what about standard C++? I checked online, having never heard of these constructs... they all seem to be MS creations outside the standard.
Yes, these are MS creations outside standard. Exactly like objects in managed CLI heap themselves.
So we are talking here about so called C++/CLI, not ISO C++. Fortunately C++/CLI is a kind of superset of ISO C++, so TBB can be compiled as C++/CLI.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I almost had a heart attack myself, so I propose that this stuff be properly flagged in advance (as "C++/CLI", not "managed code").
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I almost had a heart attack myself, so I propose that this stuff be properly flagged in advance (as "C++/CLI", not "managed code").
C++/CLI is exactly managed code, or ".NET" if you want.
Or more precisely, it's "ISO C++" + ".NET".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
C++/CLI is exactly managed code, or ".NET" if you want.
Or more precisely, it's "ISO C++" + ".NET".
Anyone who knows that doesn't need to be warned anymore.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page