473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamic Nested Structs Problem

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 1617
arne
315 Expert 100+
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[0]. The member A in all other elements in your array pointed to by B (B[1].A, B[2].A, ... B[size-1].A) are NULL pointers. Hence the segfault.
Mar 17 '07 #2
dmjpro
2,476 2GB
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
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[0].

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[0], while structs B[1], B[2]
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
Ganon11
3,652 Expert 2GB
Basically, writing

Expand|Select|Wrap|Line Numbers
  1. (B + j)
is the same as writing

Expand|Select|Wrap|Line Numbers
  1. B[j]
Mar 17 '07 #5
Ah, I see, well thanks a lot people.
Mar 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Matthew Barnes | last post by:
I was wondering if there would be any interest in extending the struct.unpack format notation to be able to express groups of data with parenthesis. For example: >>> data =...
0
by: Alexandros__ | last post by:
Hello everyone, I am using the Configuration Application block to write an XML configuration file for my application. Most of it is working fine but I have a problem serialising some nested...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
4
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of...
0
by: Kurt Ng | last post by:
Need help quick!!! Am really stuck on this problem! I have a C dll, and it uses a nested struct. (see below) The struct has 2 layers of nested structs. The first field in the first struct is...
4
by: MSDousti | last post by:
Hi all, I read some Q&As in the Net, discussing Marshalization of nested structs in C# (or VB.NET). Some guys stated that .NET framework does not support this feature yet. Are they right? ...
9
by: johndale | last post by:
hi, I am pretty new to C#, but have some experience with other lang.(delphi,php,asp...). I wanted to create dynamic array of structs type, but it wont work. So I google it, and found that C#.NET...
4
by: henry | last post by:
Folks: Using Dreamweaver CS3... Consider a home page, "index.php" which conditionally REQUIREs one of 'N' HTML files of pure content. All site styles are specified in a master CSS file,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.