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 / C++ / MFC
  4. Extracting objects to array

Extracting objects to array

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomsysadmindata-structurestutorial
3 Posts 2 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.
  • H Offline
    H Offline
    HomeNuke
    wrote on last edited by
    #1

    How do I extract a gob of class information to an array? For example say I have a class:

    class Box
    {
    int Right;
    int Left;
    int Bottom;
    };

    Let's say it's 96bytes in Size. There is another function that returns the class but it is now 480bytes in size. From that and doing calculations there is 5 objects of that size that was returned. How do I assign that information into an array? Or, what is the best way to extract each piece of the class? Thanks HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

    J 1 Reply Last reply
    0
    • H HomeNuke

      How do I extract a gob of class information to an array? For example say I have a class:

      class Box
      {
      int Right;
      int Left;
      int Bottom;
      };

      Let's say it's 96bytes in Size. There is another function that returns the class but it is now 480bytes in size. From that and doing calculations there is 5 objects of that size that was returned. How do I assign that information into an array? Or, what is the best way to extract each piece of the class? Thanks HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

      J Offline
      J Offline
      Jon Hulatt
      wrote on last edited by
      #2

      Tip: always name your classes C*

      class CBox
      {
      int Right;
      int Left;
      int Bottom;
      };

      // assuming SomeFunc is the function that returns the pointer
      // to a blob of memory with those classes in it. And it sets
      // nSize to be the size of the memory allocated.
      int nSize;
      CBox *p_BoxArray = (CBox *) SomeFunc(&nSize);

      int NumberOfObjectsInArray = nSize / sizeof(CBox);

      for (int i=0; i < NumberOfObjectsInArray ; i++)
      {
      cout << p_BoxArray[i]->Right << "\n";
      }

      // remember to free the memory when you're done with it.

      why does this work? well, you can use a pointer as if it were an array (there isn't really any difference between them, in fact.). Logically speaking, incrememnting a pointer does not make it point to that memory address + 1 byte, it makes it point to that memory address + (the size of whatever object it points to). Sorry to dissapoint you all with my lack of a witty or poignant signature.

      H 1 Reply Last reply
      0
      • J Jon Hulatt

        Tip: always name your classes C*

        class CBox
        {
        int Right;
        int Left;
        int Bottom;
        };

        // assuming SomeFunc is the function that returns the pointer
        // to a blob of memory with those classes in it. And it sets
        // nSize to be the size of the memory allocated.
        int nSize;
        CBox *p_BoxArray = (CBox *) SomeFunc(&nSize);

        int NumberOfObjectsInArray = nSize / sizeof(CBox);

        for (int i=0; i < NumberOfObjectsInArray ; i++)
        {
        cout << p_BoxArray[i]->Right << "\n";
        }

        // remember to free the memory when you're done with it.

        why does this work? well, you can use a pointer as if it were an array (there isn't really any difference between them, in fact.). Logically speaking, incrememnting a pointer does not make it point to that memory address + 1 byte, it makes it point to that memory address + (the size of whatever object it points to). Sorry to dissapoint you all with my lack of a witty or poignant signature.

        H Offline
        H Offline
        HomeNuke
        wrote on last edited by
        #3

        Jon, Thanks for your answer! It is exactly what I needed to point me in the right direction. Well, your answer practically kicked me in the right direction because it was so detailed. Thanks Again. Also did some further thought wouldn't this returned memory also be defined as an array of the object? Shouldn't we be able to access the returned information as an array? Or, is an array defined internally as something different or different flags are set in the memory space that an array occupies? Just questions... HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

        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