473,498 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gc[] to gc* conversion

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
Nov 17 '05 #1
5 1034
Nanditha Chandra wrote:
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!

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Nov 17 '05 #2
"Brandon Bray [MSFT]" <br******@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
Nov 17 '05 #3
Nanditha Chandra wrote:
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.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Nov 17 '05 #4
Brandon Bray [MSFT] wrote:
Nanditha Chandra wrote:
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!


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
Nov 17 '05 #5
Wild Wind wrote:
void F(unsigned char __gc* p) {}

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


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?


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!

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3810
by: daking | last post by:
I'm trying to track down a Java performance issue, and believe it may be related to GC behavior. Quick background: j2se app server running Caucho Resin 2.1.12, on RHEL3 with Sun JDK 1.4.2_04,...
2
3340
by: Cesar | last post by:
Hi, I am new with python, and I am having a look to program that leaks. The first thing I have to do is to determine if what leaks it is the python code of my company. I have set the...
8
1108
by: Chris Mayers | last post by:
I am trying to track a suspected memory leak in an application I'm writing and through repeated simplification of my project, I have found the following which is quite easily replicated should you...
6
1777
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
14
3452
by: Gotisch | last post by:
Hi, we are currently writing an mostly singlethreaded (game)server application in c#. But we are encountering problems with the Garbage Collector: After a certain time but also apparently...
6
5813
by: Rainer Queck | last post by:
Hi NG, I am not sure to evaluate what happens if I call the GC.SuppressFinalize(this) for a object. My question is: In case I call GC.SuppressFinalize(this) from with in one of my object, do I...
62
2684
by: Born | last post by:
GC is really garbage itself Reason 1: There is delay between the wanted destruction and the actual destruction.
5
1887
by: 7stud | last post by:
gc.garbage returns an empty list even though the command: gc.set_debug(gc.DEBUG_LEAK) produces the following output: gc: uncollectable <Dog 0x56e10> gc: uncollectable <Cat 0x56e30> gc:...
3
2445
by: crazy420fingers | last post by:
I'm running a python program that simulates a wireless network protocol for a certain number of "frames" (measure of time). I've observed the following: 1. The memory consumption of the program...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.