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. struct pointers

struct pointers

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancetutorialquestion
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.
  • G Offline
    G Offline
    ginjikun
    wrote on last edited by
    #1

    hi once again... tnx for the answers to my previous posts... again i have another qxn to ask... will the code below post any memory leak or any pointer problems? i am unsure as how to write the code for a struct to have an indefinite size of array of structs inside it. so what i did is to just use a pointer and have a member which holds the size of the array pointed to. is there any efficient way to go about this? tnx a lot! typedef struct { int number; int number2; } TEST, *LPTEST; typedef struct { LPTEST pTest; int number; } TEST_CONT; TEST test[2]; test[0].number = 123; test[0].number2 = 45; test[1].number = 678; test[1].number2 = 910; TEST_CONT testC; testC.pTest = test; testC.number = 98765; int n = testC.pTest[0].number; int n2 = testC.pTest[0].number2; cout << n << " " << n2 << " " << testC.number << endl; n = testC.pTest[1].number; n2 = testC.pTest[1].number2; cout << n << " " << n2 << " " << testC.number << endl; newbie :)

    A 1 Reply Last reply
    0
    • G ginjikun

      hi once again... tnx for the answers to my previous posts... again i have another qxn to ask... will the code below post any memory leak or any pointer problems? i am unsure as how to write the code for a struct to have an indefinite size of array of structs inside it. so what i did is to just use a pointer and have a member which holds the size of the array pointed to. is there any efficient way to go about this? tnx a lot! typedef struct { int number; int number2; } TEST, *LPTEST; typedef struct { LPTEST pTest; int number; } TEST_CONT; TEST test[2]; test[0].number = 123; test[0].number2 = 45; test[1].number = 678; test[1].number2 = 910; TEST_CONT testC; testC.pTest = test; testC.number = 98765; int n = testC.pTest[0].number; int n2 = testC.pTest[0].number2; cout << n << " " << n2 << " " << testC.number << endl; n = testC.pTest[1].number; n2 = testC.pTest[1].number2; cout << n << " " << n2 << " " << testC.number << endl; newbie :)

      A Offline
      A Offline
      Arman S
      wrote on last edited by
      #2

      No memory leak in the code just because there is no object created by 'new'. TEST_CONT testC; testC.pTest = test; testC.number = 98765; here testC.number should be the number of elements of the test, right? So why 98765? C++ provides std::vector class which is a dynamically growing array. You might use it like so; typedef struct { std::vector tests; } TEST_CONT; TEST_CONT testC; testC.tests.push_back(test); //... int n = testC.tests[0].number; int n2 = testC.tests[0].number2; cout << n << " " << n2 << " " << testC.tests.size()<< endl;

      -- ===== Arman

      G 1 Reply Last reply
      0
      • A Arman S

        No memory leak in the code just because there is no object created by 'new'. TEST_CONT testC; testC.pTest = test; testC.number = 98765; here testC.number should be the number of elements of the test, right? So why 98765? C++ provides std::vector class which is a dynamically growing array. You might use it like so; typedef struct { std::vector tests; } TEST_CONT; TEST_CONT testC; testC.tests.push_back(test); //... int n = testC.tests[0].number; int n2 = testC.tests[0].number2; cout << n << " " << n2 << " " << testC.tests.size()<< endl;

        -- ===== Arman

        G Offline
        G Offline
        ginjikun
        wrote on last edited by
        #3

        hi Arman! tnx for the reply. oooppss... yes that should be the count... i had that in mind but when i was trying out the code didnt really put it in the implementation... tnx for the suggestion regarding vector... i'll consider it. again tnx! newbie :)

        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