473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pointer as return type is this code Vallid?

5
Hi,
I have the following issue. I need to know if the following code is not giving an issue when the garbage colector does his thing. say that aDemoByteBuff is containing a ushort. is the function GetAsUShort vallid?
and is GetAsPointer valid when i translate the buffer direct in the caller.
ushort test = *(ushort*)instance.GetAsPointer()

the code is compiling but does it give a problem?

Many thanks
Peter

Expand|Select|Wrap|Line Numbers
  1.     class JustAClass
  2.     {
  3.         public byte[] aDemoByteBuff = new byte[2];
  4.  
  5.         public JustAClass()            
  6.         {
  7.         }
  8.  
  9.         unsafe public ushort GetAsUShort()
  10.         {
  11.             fixed (byte* p = &aDemoByteBuff)
  12.                return *(ushort*)p;
  13.         }
  14.  
  15.         public override unsafe byte* GetAsPointer()
  16.         {
  17.              fixed (byte* p = &aDemoByteBuff[0])
  18.                  return p;
  19.         }
  20. }
  21.  
Oct 7 '10 #1
4 1317
GaryTexmo
1,501 Expert 1GB
Did you try this, or did you just type it out in notepad and are asking if it works?

I'm unable to compile your code, so I'm guessing you didn't... so I'm a little confused by your post.

To answer your question though, yes the garbage collector should still clean up anything you allocate in unsafe code. I don't see any delete function (ie, C++ style) but I could be wrong. Anybody else want to weigh in on this one?
Oct 7 '10 #2
pjurri
5
When you use unsafe code you need to tell the compiler that you allow unsafe code. Goto menuitem project and select project properties. Goto Build and select allow unsafe code. compile.

I use this in my software lib but edited a little sample class. I never have had problems with it (wrong results) but i had doubts if it is just luck.

I have my anwser. I am happy that the GetAsUShort() is ok (I started a tread also on Microsoft forum).

Thanks for helping
Peter
Oct 7 '10 #3
GaryTexmo
1,501 Expert 1GB
Oh no, I realize that I needed the u unsafe option and indeed had it checked... ah I see the problem now though, it was something unrelated.
Oct 7 '10 #4
pjurri
5
Thanks you for taking the effort to look at the issue i had. I have my anwser and realize that i still need some low level unmanaged code.
Peter
Oct 7 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nuno Barros | last post by:
Hello, I am writting a c++ code to crete a kind of a table in memory. Each table has an container of columns, which can be of any type. To do this i created a virtual class Column which has...
12
by: Sims | last post by:
Hi, Here is the code that .NET does not seem to like, but as far as i can see it is valid C++ code. Am i wrong? .... // vector headers ....
3
by: Christopher Benson-Manica | last post by:
#include <map> #include <vector> #include <iostream> using namespace std; class test { public: unsigned int data;
12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
31
by: CeZaR | last post by:
Hi, How can i specify the return type of a function returning a managed array of chars. If i try to write: "char __gc func()" i get an error! How can i do that? Thanks!
3
by: C++ | last post by:
According to Thinking in C++ "You cannot modify the return type of a virtual function during overriding.but there is a special case in which you can slightly modify the return type. If you¡¯re...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
17
by: Pietro Cerutti | last post by:
i Group, to my understanding, defining a function parameter as "const" means that the function is not going to change it. Why does the compiler says "return discards qualifiers from pointer...
3
by: .rhavin grobert | last post by:
guess you have the following: _________________________________________________ template <class T> class CQVector { public: // find an element, returns index or -1 if none is found int...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.