简体   繁体   中英

Pointers structures and dynamic memory allocation in c++

I have two structures in which the second structure has the first structure nested inside of it:

struct first
{
    int a;
};

struct second
{
    first nested;
};

Now the problem is that the second structure has to be dynamically allocated through a pointer. Also, the nested first structure has to be a dynamically allocated array through a pointer, whose array size has to be read in through an input file.

I know how to read it in but I don't know how to access it. For example lets suppose the size is 8. How would I go about specifying the values for the second structure given the pointer's format?

I tried assuming ptr points to first structure and ptr1 points to second structure ptr1->((ptr+count)->a) where we can process it through a loop. This doesn't work. So I was wondering how you would initialize the values the second structure whose member includes all the n structures in the n element array.

Vector is really easy just stick your struct where type is and use it like any other array. Although what you described really sounds like linked list but hey vector will probably be better for you :)

#include <vector>

//main
vector <Type> myArray(8); //set the number of elements you want

myArray[0] = blablabla

More specific example:

struct first
{
    int a;
};

vector <first> myArray(8);

first[0].a = 1; // you get the idea :)

EDIT From the comments this seems to me more up your alley.

struct bla { 
 int num;
};

//in main
bla *balBla = NULL;
blaBla = new(bla[8]); //There made on the fly dynamic man
blaBla[0].num = 7; 
//Don't forget to delete when done or scary memory leak!!!
delete[] blaBla;

Last Edit If this is not what you want then no one will ever understand what you mean

#include <iostream>
using namespace std;

struct b {
    int num;
};

struct a {
    b *nested = NULL;

    a(){} //Default Constructor 

    a(int elements) { 
        nested = new(b[elements]); 
    } //Lets you add elements to nested at initialization

    void addElem(int elements) { 
        if (nested != NULL) {
            delete[] nested;
        }
        nested = new(b[elements]);
    } //Redefine or make new array

    ~a() { 
        delete[] nested; 
    } //destructor
};


int main() {
    a myStupidObj(3);
    myStupidObj.nested[0].num = 69;
    myStupidObj.nested[1].num = 77;
    myStupidObj.nested[2].num = 666;

    cout << "Struct of one D array of structs" << endl;
    cout << myStupidObj.nested[0].num << endl;
    cout << myStupidObj.nested[1].num << endl;
    cout << myStupidObj.nested[2].num << endl;

    //Make 2d version
    a *my2DStupidObj = new(a[2]); 
    my2DStupidObj[0].addElem(3);
    my2DStupidObj[0].nested[0].num = 666;
    my2DStupidObj[0].nested[1].num = 6969;
    my2DStupidObj[0].nested[2].num = 80085;

    cout << "array of struct of one D array of structs" << endl;
    cout << my2DStupidObj[0].nested[0].num << endl;
    cout << my2DStupidObj[0].nested[1].num << endl;
    cout << my2DStupidObj[0].nested[2].num << endl;

    my2DStupidObj[1].addElem(3);
    my2DStupidObj[1].nested[0].num = 11;
    my2DStupidObj[1].nested[1].num = 111;
    my2DStupidObj[1].nested[2].num = 1111;

    cout << my2DStupidObj[1].nested[0].num << endl;
    cout << my2DStupidObj[1].nested[1].num << endl;
    cout << my2DStupidObj[1].nested[2].num << endl;

    delete [] my2DStupidObj;
    return 0;
}

In C, you sometimes see something like:

struct second {
   int count;
   first nested[1];
};

Then, you allocate memory. The amount you allocate is computed as sizeof(struct second) plus the size of first times the number of elements you want in the array minus 1 (since there is already one included).

ptr = (struct second *)malloc(sizeof(struct second) + ((n-1) * sizeof(first)));

Then you can use

ptr->nested[idx].a

or if you have to use pointers,

((ptr->nested)+idx)->a

Of course, you have to do all the housework and cleanup yourself. C++ and its std libraries do most of that for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM