topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday April 16, 2024, 12:38 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Pointers to nullable types (C#)  (Read 6377 times)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Pointers to nullable types (C#)
« on: May 07, 2011, 08:43 AM »
I found something out that was pretty strange. 

I have an object that's based off the IEditableObject interface.  It maps to certain db fields- I'm trying to abstract the data layer from the application so that it can be replaced; we have a legacy system that we're going to try to get rid of down the line, so this is a first step in that direction.  I was going to do a bulk memory copy based on the memory location of each field in the object, but by necessity, since some of the fields are nullable, we use nullable types to represent those fields, i.e. int? instead of int. 

Going further, i realized that a nullable type is just a struct with a value and a bool for isnull, which makes sense, but I never thought of it that way.  So I was going to have to get pointers to the fields to get the memory locations- but you can't get a pointer to a nullable type.  I created a struct that was the exact same format as the nullable type, and was able to get a pointer to that...so I don't understand the logic behind this...

Thoughts?

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #1 on: May 07, 2011, 08:55 AM »
That's a very interesting problem. I've never used pointers for nullable types, so I can't say anything intelligent. But this thread will be interesting to watch...
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #2 on: May 07, 2011, 08:59 AM »
I've done no research on the subject, though I've done some stuff, including nullable types, in C#. But not in this direction. Nevertheless...

I kind of assume you read this msdn subject explaining nullable types?
It states:
T can be any value type including struct; it cannot be a reference type

This makes me suspect the actual implementation is something handled at compile-time, so pointers for getting at it won't exist during runtime?

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #3 on: May 07, 2011, 09:12 AM »
There's this blog article from around when nullable types where introduced (.NET 2.0), that has some more interesting info

And also this blog post that puts a claim around my original assumption... (I did not check these claims/examples though)

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #4 on: May 07, 2011, 09:27 AM »
And finally, a Stack Overflow article on the subject, showing a nice extension method that might come in useful

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #5 on: May 07, 2011, 08:47 PM »
I've done no research on the subject, though I've done some stuff, including nullable types, in C#. But not in this direction. Nevertheless...

I kind of assume you read this msdn subject explaining nullable types?
It states:
T can be any value type including struct; it cannot be a reference type

This makes me suspect the actual implementation is something handled at compile-time, so pointers for getting at it won't exist during runtime?

Nothing so esoteric.  A nullable type is a struct, and using reference types as fields in structs is A Bad Idea.

When you have a struct that contains a reference type and you copy an instance of the struct, the reference type inside isn't "deep" copied, because it is simply a reference (pointer) to a memory location containing the actual class. So if you copy a struct that contains class instances, the copy will be referencing the same instances as the original.

So I think that's why nullable types can't be reference types- and it would be pointless in any case, since any reference type is already nullable.

Thanks for the links also, but neither one of those explains why you can't get a pointer to System.Nullable.  It's implemented as a struct (as they point out in the links) so why is it special?  As I said, if I create a struct that has the exact same format as System.Nullable, I can get a pointer to it... so again, why is it special?
« Last Edit: May 07, 2011, 08:49 PM by wraith808 »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #6 on: May 08, 2011, 05:30 AM »
@wraith808: That all makes perfectly good sense.

so again, why is it special?
A few more thoughts:
  • They (MS) explicitly don't want you to do this
  • You could exchange the int? by Int32 and get a pointer to that

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #7 on: May 08, 2011, 06:27 AM »
@wraith808: That all makes perfectly good sense.

so again, why is it special?
A few more thoughts:
  • They (MS) explicitly don't want you to do this
  • You could exchange the int? by Int32 and get a pointer to that

That wouldn't work in this case.  It needs to be nullable to be able to store the database value, and the idea is to do a bulk memory copy, mapping the memory locations of the values over the memory locations of the structure.  And yeah, I think at this point that MS just explicitly made this impossible.

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #8 on: May 08, 2011, 07:32 PM »
IntPtr doesn't help, eh?
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Pointers to nullable types (C#)
« Reply #9 on: May 08, 2011, 09:38 PM »
IntPtr doesn't help, eh?

Unfortunately not. :(