struct pointers
-
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 :)
-
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 :)
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
-
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