Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2004

Tip: Looking for answers? Try searching our database.

gc[] to gc* conversion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nanditha Chandra - 15 Sep 2004 14:56 GMT
Hi!

I am new to managed c++. I have a byte gc array and I want to pass it
to a function that takes a __gc* to unsigned char. How would I convert
my byte array to this?

Byte arBuf[] = __gc new Byte[nSize];

// I have code here to read from a file to this buffer.
// Now I need to pass this to a function which is defined as follows:

void Write(unsigned char __gc*);

How can I convert my byte array to unsigned char __gc*?

Thanks,
D
Brandon Bray [MSFT] - 15 Sep 2004 21:29 GMT
> How can I convert my byte array to unsigned char __gc*?

Take the address of the first array element. For example:

 void F(unsigned char __gc* p) {}

 Byte arr[] = new Byte[10];
 F(&arr[0]);

Cheerio!

Signature

Brandon Bray, Visual C++ Compiler         http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback?   http://msdn.microsoft.com/productfeedback/

Nanditha Chandra - 17 Sep 2004 12:59 GMT
"Brandon Bray [MSFT]" <branbray@online.microsoft.com> wrote >
> Take the address of the first array element. For example:
>
>   void F(unsigned char __gc* p) {}
>
>   Byte arr[] = new Byte[10];
>   F(&arr[0]);

Can you do that? I thought contiguious allocation is not guaranteed.
So, in Foo, Can I assume that the next 10 bytes is what I want.

Thanks,
D
Brandon Bray [MSFT] - 17 Sep 2004 21:42 GMT
> Can you do that? I thought contiguious allocation is not guaranteed.
> So, in Foo, Can I assume that the next 10 bytes is what I want.

Yes. Arrays are guaranteed to have contiguous allocation. Other than using
ExplicitLayout, arrays are the only way to get contiguous allocation.

Signature

Brandon Bray, Visual C++ Compiler         http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback?   http://msdn.microsoft.com/productfeedback/

Wild Wind - 18 Sep 2004 10:55 GMT
>> How can I convert my byte array to unsigned char __gc*?
>
[quoted text clipped - 6 lines]
>
> Cheerio!

Brandon,

Just wondering... do you not have to pin the &arr[0]
address before passing it to the F function, since arr
is managed and can be shifted around?

--
Akin

aknak at aksoto dot idps dot co dot uk
Brandon Bray [MSFT] - 20 Sep 2004 16:51 GMT
>>   void F(unsigned char __gc* p) {}
>>
[quoted text clipped - 4 lines]
> address before passing it to the F function, since arr
> is managed and can be shifted around?

Because the argument to F used __gc to qualify the pointer, it notes that
the garbage collector is going to track this pointer and update it if the
memory gets moved around.

Now, if __gc hadn't been there (or if the __nogc keyword were used), then
the pointer would not be tracked by the garbage collector. This allows the
pointer to be used by native code. In that situation, you are correct that
the pointer needs to be pinned first.

Hope that makes sense. Cheerio!

Signature

Brandon Bray, Visual C++ Compiler         http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback?   http://msdn.microsoft.com/productfeedback/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.