Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting size of a data structure

Getting size of a data structure

Scheduled Pinned Locked Moved C#
helpquestioncsharplounge
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LighthouseJ
    wrote on last edited by
    #1

    I'm writing a program and I need to figure out how big any single data structure is but the programs' signature simply allows any thing of "object" type because I want to allow anything. How can I figure out how many bytes whatever is sent to the program is long? I tried looking at sizeof and stuff but it appears sufficiently different from C so I'm stuck and it requires unsafe which doesn't seem like the best thing. I can't find anything using the Type class with <obj>.GetType() or typeof(<obj>). I've done searches and I guess my problem is too general to search so I'm stuck. This object can be a byte, character, string or an instance of a custom class or anything at all. I want to be as unrestrictive as possible. If it can't be done easily or at all, I'll probably revert back to C but I'm trying to learn C#. Thanks in advance, Nate.

    E S 2 Replies Last reply
    0
    • L LighthouseJ

      I'm writing a program and I need to figure out how big any single data structure is but the programs' signature simply allows any thing of "object" type because I want to allow anything. How can I figure out how many bytes whatever is sent to the program is long? I tried looking at sizeof and stuff but it appears sufficiently different from C so I'm stuck and it requires unsafe which doesn't seem like the best thing. I can't find anything using the Type class with <obj>.GetType() or typeof(<obj>). I've done searches and I guess my problem is too general to search so I'm stuck. This object can be a byte, character, string or an instance of a custom class or anything at all. I want to be as unrestrictive as possible. If it can't be done easily or at all, I'll probably revert back to C but I'm trying to learn C#. Thanks in advance, Nate.

      E Offline
      E Offline
      Expert Coming
      wrote on last edited by
      #2

      Search for overloading, that sounds like what you are trying to do.

      L 1 Reply Last reply
      0
      • L LighthouseJ

        I'm writing a program and I need to figure out how big any single data structure is but the programs' signature simply allows any thing of "object" type because I want to allow anything. How can I figure out how many bytes whatever is sent to the program is long? I tried looking at sizeof and stuff but it appears sufficiently different from C so I'm stuck and it requires unsafe which doesn't seem like the best thing. I can't find anything using the Type class with <obj>.GetType() or typeof(<obj>). I've done searches and I guess my problem is too general to search so I'm stuck. This object can be a byte, character, string or an instance of a custom class or anything at all. I want to be as unrestrictive as possible. If it can't be done easily or at all, I'll probably revert back to C but I'm trying to learn C#. Thanks in advance, Nate.

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        What exactly are you trying to do? Why do you want to know the size of objects, are you doing custom marshalling of any sort? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        L 1 Reply Last reply
        0
        • E Expert Coming

          Search for overloading, that sounds like what you are trying to do.

          L Offline
          L Offline
          LighthouseJ
          wrote on last edited by
          #4

          I know how to overload class and operators but that's not the goal. I'll state what I'm trying to do in the other thread so I only write it once.

          1 Reply Last reply
          0
          • S S Senthil Kumar

            What exactly are you trying to do? Why do you want to know the size of objects, are you doing custom marshalling of any sort? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            L Offline
            L Offline
            LighthouseJ
            wrote on last edited by
            #5

            I'm trying to read the size and then get to the bits of any instance of data so I can teach it to a neural network. That way I can not only teach it strings, but also pictures for facial recognition, or other things, like maybe voice, or I can use it for error correction. I'm sure someone else has done it but I'm trying to learn C# and this is my project. If no one else has, I'll likely make it my first submission on here for others to see. I've seen "marshall" in class roots on help pages but I don't know what that is.

            S 1 Reply Last reply
            0
            • L LighthouseJ

              I'm trying to read the size and then get to the bits of any instance of data so I can teach it to a neural network. That way I can not only teach it strings, but also pictures for facial recognition, or other things, like maybe voice, or I can use it for error correction. I'm sure someone else has done it but I'm trying to learn C# and this is my project. If no one else has, I'll likely make it my first submission on here for others to see. I've seen "marshall" in class roots on help pages but I don't know what that is.

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              Did you try Marshal.SizeOf[^]? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              L 1 Reply Last reply
              0
              • S S Senthil Kumar

                Did you try Marshal.SizeOf[^]? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                L Offline
                L Offline
                LighthouseJ
                wrote on last edited by
                #7

                Looks promising at a glance. I've been looking into just what marshaling is about and it's starting to gel, and this will help, thanks. Looking at the Marshal class seems to be just what I'm looking for, accessing the raw data.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups