446,407 Members | 920 Online Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

# Dynamic Nested Structs Problem

 P: 5 Hello, Lets say I have: struct str1 { int var1, var2; float var3; }; struct str2 { int var4; str1 *A; } *B; then I dynamically allocate space for the structs: B = new str2[size]; //size is an int, obtained during run time B->A = new str1[size]; then I fill up the structs, for example, like this: for(int j = 0; j < size; j++) { B[j].A[j].var1 = rand(); B[j].A[j].var2 = rand(); B[j].A[j].var3 = rand(); } I know the code doesn't do anything. What I need help on is that the code compiled with no errors, but when I tried to run it, the program terminated with a "Please tell Microsoft about this problem" error message. I'm guessing that a pointer is pointing to something it's not suppose to. But I can't figure out how to fix it. Thanks in advance. Mar 17 '07 #1
5 Replies

 Expert 100+ P: 315 Hello, Lets say I have: struct str1 { int var1, var2; float var3; }; struct str2 { int var4; str1 *A; } *B; then I dynamically allocate space for the structs: B = new str2[size]; //size is an int, obtained during run time B->A = new str1[size]; then I fill up the structs, for example, like this: for(int j = 0; j < size; j++) { B[j].A[j].var1 = rand(); B[j].A[j].var2 = rand(); B[j].A[j].var3 = rand(); } I know the code doesn't do anything. What I need help on is that the code compiled with no errors, but when I tried to run it, the program terminated with a "Please tell Microsoft about this problem" error message. I'm guessing that a pointer is pointing to something it's not suppose to. But I can't figure out how to fix it. Thanks in advance. The problem is that you only assign an array of str1 to B, i.e. B. The member A in all other elements in your array pointed to by B (B.A, B.A, ... B[size-1].A) are NULL pointers. Hence the segfault. Mar 17 '07 #2

 100+ P: 2,476 i think u run this program in windows platform ....... B = new str2[size]; //size is an int, obtained during run time //Here remove the line .....B->A = new str1[size]; for(int j = 0; j < size; j++) { (B+j)->A = new str1[size]; //edit this line like this B[j].A[j].var1 = rand(); B[j].A[j].var2 = rand(); B[j].A[j].var3 = rand(); } after do this the exception will not occure ...... now try to undrerstand urself ... if not plz contact me again ... i m online best of luuckkk Mar 17 '07 #3

 P: 5 As you all can see, I'm not very experienced in the programming game. So let's see if I get this right. Let size = 3, then: B = new str2[size]; will create 3 structs of type str2, with B initially pointing to the first struct B. So is that why the statement: B->A = new str1[size]; will only create 3 structs of type str1 for the first str2 struct B, while structs B, B are undefined? So I'm guessing that's why dmjpro put the statement: (B+j)->A = new str1[size]; in the loop. But what I can't figure out is why "B+j" and what does it mean? Mar 17 '07 #4

 Expert 2.5K+ P: 3,652 Basically, writing Expand|Select|Wrap|Line Numbers (B + j) is the same as writing Expand|Select|Wrap|Line Numbers B[j] Mar 17 '07 #5

 P: 5 Ah, I see, well thanks a lot people. Mar 18 '07 #6 