wyrd Posted March 13, 2004 Posted March 13, 2004 Is there a way to get the size of an object? C# has the sizeof() keyword, but unfortunately it only works for value types. I need something that'll work for reference types. Meh, posted in wrong forum. Oh well. :) Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Nerseus Posted March 14, 2004 *Experts* Posted March 14, 2004 I no of no way to get the size of a managed class. Here's a blog from Microsoft that talks about this topic though - it might be helpful (I didn't read it through): http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx Here's an article about memory, might also yield some results though again, I haven't read it: http://www.albahari.com/value%20vs%20reference%20types.html -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
wyrd Posted March 14, 2004 Author Posted March 14, 2004 I was afraid of that. I'll take a look at the links, thanks. Quote Gamer extraordinaire. Programmer wannabe.
dragon4spy Posted March 14, 2004 Posted March 14, 2004 For vb6, I can hack into memory and get the size by using ObjPtr() and CopyMemory(). But now vb.net doesn't support ObjPtr(). Quote Don't judge a man by his look. Don't judge a book by its cover. :D
*Experts* Nerseus Posted March 15, 2004 *Experts* Posted March 15, 2004 You can still pin memory and get a pointer to any managed object BUT to use copymemory you would have to know how big a chunk to copy. I think that's what wyrd is looking for. If it's a simple class, you could try and "guess", but I because of byte alignment I don't know if there's any guarantee that your program won't change on a different platform (when 64bit computers are more normal for instance). Tough call. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
wyrd Posted March 17, 2004 Author Posted March 17, 2004 There just has to be a way to get the size of an object. :( Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Nerseus Posted March 18, 2004 *Experts* Posted March 18, 2004 What object is it? If it's yours, why not just use a struct? -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Gurus* Derek Stone Posted March 18, 2004 *Gurus* Posted March 18, 2004 [msdn]System.Runtime.InteropServices.Marshal[/msdn].SizeOf() Quote Posting Guidelines
wyrd Posted March 18, 2004 Author Posted March 18, 2004 Unfortunately that method only works with an unmanaged object. And if I'm not mistaken, I'd have to allocated more memory in the unmanaged heap, get the size, then deallocate memory from the unmanaged heap, just to get the size (which isn't even accurate since it's the unmanaged size, and not the managed size). It's not what I'm looking for, but thanks anyway. Quote Gamer extraordinaire. Programmer wannabe.
wyrd Posted March 18, 2004 Author Posted March 18, 2004 What object is it? If it's yours, why not just use a struct? Of any managed object. Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* Derek Stone Posted March 19, 2004 *Gurus* Posted March 19, 2004 Unfortunately that method only works with an unmanaged object. And if I'm not mistaken, I'd have to allocated more memory in the unmanaged heap, get the size, then deallocate memory from the unmanaged heap, just to get the size (which isn't even accurate since it's the unmanaged size, and not the managed size). It's not what I'm looking for, but thanks anyway. The method gets the equivalent unmanaged size of any CLR object (useful for platform invocation). It doesn't (and can't) get the size of an unmanaged object. This is the closest you're going to get to what you want. Quote Posting Guidelines
*Experts* Nerseus Posted March 19, 2004 *Experts* Posted March 19, 2004 From my limited digging, you just can't get the size of a managed class - at least through no publicly exposed interface anyone is talking about. Maybe there's a hidden API, but no one's talking. I'm not sure if this will help, but you can get a pointer to any object through the use of a GCHandle object. Check the help on how to use it. Make sure you specify "pinned" memory when you create the handle and then you can safely pass the returned pointer (handle.AddrOfPinnedObject) to an API, if that's what you want/need. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.