如何初始化这个可变大小的结构? 我如何初始化下面声明的可变大小C++结构体结构:< /p> #include <iostream> #include <string> typedef struct { char* a; int b; } StructA; typedef struct { StructA* pointers[]; int pcount; int numbers[]; int ncount; } StructB; int main() { StructB *sb = new StructB; // I need 'sb' to be allocated in the heap sb->pcount = 5; sb->ncount = 3; sb->pointers = new StructA*[sb->pcount]; sb->numbers = int[sb->ncount]; } 听,这不是好的C++方式,你在C++环境下做C 这是好的,如果它给你一些优势IO,打字检查等或学习。 C风格的动态内存分配完全是手动的。解除分配也是如此。 由于您使用的是typedef,typedef可以让您和编译器更清楚地了解情况。 这是一个折磨人的例子,我想这正是你想要的。 请注意,这是一种艰难的方式,在这种情况下,这实际上是一种简单的方式 C++中的C风格编码;p> #include <iostream> #include <string> typedef struct { char* a; int b; } StructA; typedef StructA* A_p; // A_p is "pointer to structA" (contains address of struct) typedef A_p* A_p_Array; // A_p_Array is "pointer to A_p" (aka StructA**) contains address of A_p typedef int* int_Array; // int_Array is "pointer to int" contains address of integer typedef struct { A_p_Array A_pointers; int pcount; int_Array numbers; int ncount; } StructB; int main() { int i; StructA *sa = new StructA; StructB *sb = new StructB; sb->pcount = 5; sb->ncount = 3; A_p_Array tmp_A_array; A_p tmp; for ( i = 0 ; i < sb->pcount; i++) { tmp_A_array = new A_p; // create a StructA pointer tmp = new StructA; // create a StructA tmp_A_array = &tmp; // put address of tmp in mp_A_array tmp_A_array++; // increment array address } sb->A_pointers = tmp_A_array; // A_pointers now contains address of dynamically created array tmp_A_array = NULL; // clear pointer but do NOT delete! int_Array tmp_i_array; for ( i = 0 ; i < sb->ncount; i++) { tmp_i_array = new int(0); // c++ can create ints with initial values tmp_i_array++; } sb->numbers = tmp_i_array; tmp_i_array = NULL; // clear pointer but do NOT delete! /****** USE Structs A & B *****/ // clean up the heap A_p Ap; for ( i = 0 ; i < sb->pcount; i++) { Ap = sb->A_pointers[i]; delete Ap; // each struct released separately } int* ip; for ( i = 0 ; i < sb->ncount; i++) { ip = & sb->numbers[i]; delete ip; //each int released separately } delete sb; return 0; } // main
有更好的方法来实现上述目标 这就是C++和高级语言的一个原因。 当你开始上课的时候,会比较容易 但是你也会遇到同样的问题 所以,把指针和指针放下来如何初始化这个可变大小的结构? 我如何初始化下面声明的可变大小C++结构体结构:< /p> #include <iostream> #include <string> typedef struct { char* a; int b; } StructA; typedef struct { StructA* pointers[]; int pcount; int numbers[]; int ncount; } StructB; int main() { StructB *sb = new StructB; // I need 'sb' to be allocated in the heap sb->pcount = 5; sb->ncount = 3; sb->pointers = new StructA*[sb->pcount]; sb->numbers = int[sb->ncount]; } 听,这不是好的C++方式,你在C++环境下做C 这是好的,如果它给你一些优势IO,打字检查等或学习。 C风格的动态内存分配完全是手动的。解除分配也是如此。 由于您使用的是typedef,typedef可以让您和编译器更清楚地了解情况。 这是一个折磨人的例子,我想这正是你想要的。 请注意,这是一种艰难的方式,在这种情况下,这实际上是一种简单的方式 C++中的C风格编码;p> #include <iostream> #include <string> typedef struct { char* a; int b; } StructA; typedef StructA* A_p; // A_p is "pointer to structA" (contains address of struct) typedef A_p* A_p_Array; // A_p_Array is "pointer to A_p" (aka StructA**) contains address of A_p typedef int* int_Array; // int_Array is "pointer to int" contains address of integer typedef struct { A_p_Array A_pointers; int pcount; int_Array numbers; int ncount; } StructB; int main() { int i; StructA *sa = new StructA; StructB *sb = new StructB; sb->pcount = 5; sb->ncount = 3; A_p_Array tmp_A_array; A_p tmp; for ( i = 0 ; i < sb->pcount; i++) { tmp_A_array = new A_p; // create a StructA pointer tmp = new StructA; // create a StructA tmp_A_array = &tmp; // put address of tmp in mp_A_array tmp_A_array++; // increment array address } sb->A_pointers = tmp_A_array; // A_pointers now contains address of dynamically created array tmp_A_array = NULL; // clear pointer but do NOT delete! int_Array tmp_i_array; for ( i = 0 ; i < sb->ncount; i++) { tmp_i_array = new int(0); // c++ can create ints with initial values tmp_i_array++; } sb->numbers = tmp_i_array; tmp_i_array = NULL; // clear pointer but do NOT delete! /****** USE Structs A & B *****/ // clean up the heap A_p Ap; for ( i = 0 ; i < sb->pcount; i++) { Ap = sb->A_pointers[i]; delete Ap; // each struct released separately } int* ip; for ( i = 0 ; i < sb->ncount; i++) { ip = & sb->numbers[i]; delete ip; //each int released separately } delete sb; return 0; } // main,c++,C++,有更好的方法来实现上述目标 这就是C++和高级语言的一个原因。 当你开始上课的时候,会比较容易 但是你也会遇到同样的问题 所以,把指针和指针放下来 现在,以后会对你有好处。你的代码不是有效的C++,标准C++不支持VLA。据我所知,int数[];在C++中不允许作为结构成员。它在C中是允许的,但只能作为最后一个成员——换句话说,int-ncount;紧接着也使其成为非法C。只需使其成为StructA**指针;和int*number;。或者,更好的是,使用向量,因为这是C++而不是C.@ CDHO
现在,以后会对你有好处。你的代码不是有效的C++,标准C++不支持VLA。据我所知,int数[];在C++中不允许作为结构成员。它在C中是允许的,但只能作为最后一个成员——换句话说,int-ncount;紧接着也使其成为非法C。只需使其成为StructA**指针;和int*number;。或者,更好的是,使用向量,因为这是C++而不是C.@ CDHOWIE:不,数组不是指针。不过数组会退化为指针。@EduardoLeón我不确定这在所有情况下都是正确的-例如,我非常确定void fooint x[]等同于void fooint*x。
#include <iostream>
#include <string>
typedef struct {
char* a;
int b;
} StructA;
typedef StructA* A_p; // A_p is "pointer to structA" (contains address of struct)
typedef A_p* A_p_Array; // A_p_Array is "pointer to A_p" (aka StructA**) contains address of A_p
typedef int* int_Array; // int_Array is "pointer to int" contains address of integer
typedef struct {
A_p_Array A_pointers;
int pcount;
int_Array numbers;
int ncount;
} StructB;
int main()
{
int i;
StructA *sa = new StructA;
StructB *sb = new StructB;
sb->pcount = 5;
sb->ncount = 3;
A_p_Array tmp_A_array;
A_p tmp;
for ( i = 0 ; i < sb->pcount; i++)
{
tmp_A_array = new A_p; // create a StructA pointer
tmp = new StructA; // create a StructA
tmp_A_array = &tmp; // put address of tmp in mp_A_array
tmp_A_array++; // increment array address
}
sb->A_pointers = tmp_A_array; // A_pointers now contains address of dynamically created array
tmp_A_array = NULL; // clear pointer but do NOT delete!
int_Array tmp_i_array;
for ( i = 0 ; i < sb->ncount; i++)
{
tmp_i_array = new int(0); // c++ can create ints with initial values
tmp_i_array++;
}
sb->numbers = tmp_i_array;
tmp_i_array = NULL; // clear pointer but do NOT delete!
/****** USE Structs A & B *****/
// clean up the heap
A_p Ap;
for ( i = 0 ; i < sb->pcount; i++)
{
Ap = sb->A_pointers[i];
delete Ap; // each struct released separately
}
int* ip;
for ( i = 0 ; i < sb->ncount; i++)
{
ip = & sb->numbers[i];
delete ip; //each int released separately
}
delete sb;
return 0;
} // main