473,785 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct in struct

Hello.
My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
}
So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?
Jul 22 '05 #1
14 2567
On Mon, 31 May 2004 22:04:19 GMT, Gunnar G <de****@comhem. se> wrote:
Hello.
My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
}
So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?


Ah, but not /yet/. At the point where pp is declared, there's nothing yet
happening where the vector "needs to know" the size of X. Go take a look at
std::vector's class definition in whatever lib (gcc, I guess) you're using,
and you'll see that it probably makes use of a pointer to X, but you won't
see any X objects themselves declared in the class definition. By the time
it needs to specialize member functions such as push_back [say, for
example, if you were to add this line to main:
a.pp.push_back( X());
] then X is a complete type and it works.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Gunnar G wrote:

My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
}
So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain? cat self.cc #include <iostream>
#include <vector>

struct X{
int a,b,c;
std::vector<X> pp(1);
};

int main(int argc, char* argv[]){
X a;
return 0;
}
g++ -Wall -ansi -pedantic -o self self.cc self.cc:6: error: invalid data member initialization
self.cc:6: error: (use `=' to initialize static data members) g++ --version

g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
Jul 22 '05 #3
Leor Zolman wrote:
Gunnar G wrote:
My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
return 0;
}

So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?
Ah, but not /yet/. At the point where pp is declared,
there's nothing yet happening where the vector "needs to know" the size of X.
Go take a look at std::vector's class definition
in whatever lib (gcc, I guess) you're using,
and you'll see that it probably makes use of a pointer to X,
but you won't see any X objects themselves declared in the class definition.


Are you saying that this behavior is implementation dependent?
By the time it needs to specialize member functions such as push_back
[say, for example, if you were to add this line to main:
a.pp.push_back( X());
] then X is a complete type and it works.

Jul 22 '05 #4
On Mon, 31 May 2004 15:33:42 -0700, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
Gunnar G wrote:

My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
}
So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?
> cat self.cc

#include <iostream>
#include <vector>

struct X{
int a,b,c;
std::vector<X> pp(1);


Um, the OP's original example somehow seems to have grown a "(1)" by the
time it reached your computer. I can't explain why. Using the original
example,

d:\src\learn>g+ + -Wall -ansi -pedantic x.cpp
d:\src\learn>

-leor
};

int main(int argc, char* argv[]){
X a;
return 0;
}
> g++ -Wall -ansi -pedantic -o self self.cc

self.cc:6: error: invalid data member initialization
self.cc:6: error: (use `=' to initialize static data members)
> g++ --version

g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5
On Mon, 31 May 2004 15:41:10 -0700, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
Leor Zolman wrote:
Gunnar G wrote:
My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
return 0;
}

So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?
Ah, but not /yet/. At the point where pp is declared,
there's nothing yet happening where the vector "needs to know" the size of X.
Go take a look at std::vector's class definition
in whatever lib (gcc, I guess) you're using,
and you'll see that it probably makes use of a pointer to X,
but you won't see any X objects themselves declared in the class definition.


Are you saying that this behavior is implementation dependent?


No I'm not; nor am I saying it isn't. I don't truly know. I was suggesting
how it could be possible, on the OP's platform, for that declaration of pp
to work with the incomplete (at that point) type X.
-leor
By the time it needs to specialize member functions such as push_back
[say, for example, if you were to add this line to main:
a.pp.push_back( X());
] then X is a complete type and it works.


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #6
Leor Zolman wrote:
E. Robert Tisdale wrote:
Gunnar G wrote:
My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
}
So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?
> cat self.cc

#include <iostream>
#include <vector>

struct X{
int a,b,c;
std::vector<X> pp(1);

Um, the OP's original example somehow seems to have grown a "(1)" by the
time it reached your computer. I can't explain why. Using the original
example,

d:\src\learn>g+ + -Wall -ansi -pedantic x.cpp
d:\src\learn>


Which, I believe,
is what Gunnar G was reporting for an *empty* container.
cat self.cc #include <iostream>

template <typename T>
struct myVector {
T a[1];
};

struct X{
int a,b,c;
myVector<X> pp;
};

int main(int argc, char* argv[]){
X a;
return 0;
}
g++ -Wall -ansi -pedantic -o self self.cc

self.cc: In instantiation of `myVector<X>':
self.cc:10: instantiated from here
self.cc:5: error: `myVector<T>::a ' has incomplete type
self.cc:8: error: forward declaration of `struct X'
self.cc: In function `int main(int, char**)':
self.cc:14: warning: unused variable `X a'

is probably the case that Gunnar G was concerned about.
Jul 22 '05 #7
On Mon, 31 May 2004 19:00:06 -0700, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
Leor Zolman wrote:
E. Robert Tisdale wrote:
Gunnar G wrote:

My compiler (GCC 3.3.*) does not complain about the following:

#include <iostream>
#include <vector>
using namespace std;

struct X{
int a,b,c;
vector<X> pp;
};

int main(){
X a;
}
So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?

> cat self.cc
#include <iostream>
#include <vector>

struct X{
int a,b,c;
std::vector<X> pp(1);

Um, the OP's original example somehow seems to have grown a "(1)" by the
time it reached your computer. I can't explain why. Using the original
example,

d:\src\learn>g+ + -Wall -ansi -pedantic x.cpp
d:\src\learn>


Which, I believe,
is what Gunnar G was reporting for an *empty* container.


Sorry, I have no idea what you're talking about. He was reporting that
he drew no errors, and asking how that could be--a perfectly
reasonable question, IMO. I would probably have guessed, if shown his
code, without being told it compiles, that it should have drawn an
error. But it does not; so, as per Mr. Holmes, "When you have
eliminated the impossible..."
> cat self.cc

#include <iostream>

template <typename T>
struct myVector {
T a[1];
};

struct X{
int a,b,c;
myVector<X> pp;
};

int main(int argc, char* argv[]){
X a;
return 0;
}
> g++ -Wall -ansi -pedantic -o self self.cc

self.cc: In instantiation of `myVector<X>':
self.cc:10: instantiated from here
self.cc:5: error: `myVector<T>::a ' has incomplete type
self.cc:8: error: forward declaration of `struct X'
self.cc: In function `int main(int, char**)':
self.cc:14: warning: unused variable `X a'

is probably the case that Gunnar G was concerned about.


Why isn't the case he actually showed us the one he was most likely to
actually be concerned about? Why would he have taken the trouble to
obfuscate what he was /really/ wanting to know about, by conjuring up
such an example that does not draw an error?
-leor
Jul 22 '05 #8
> Which, I believe,
is what Gunnar G was reporting for an *empty* container.
> cat self.cc

#include <iostream>

template <typename T>
struct myVector {
T a[1];
};

struct X{
int a,b,c;
myVector<X> pp;
};

int main(int argc, char* argv[]){
X a;
return 0;
}
> g++ -Wall -ansi -pedantic -o self self.cc

self.cc: In instantiation of `myVector<X>':
self.cc:10: instantiated from here
self.cc:5: error: `myVector<T>::a ' has incomplete type
self.cc:8: error: forward declaration of `struct X'
self.cc: In function `int main(int, char**)':
self.cc:14: warning: unused variable `X a'

is probably the case that Gunnar G was concerned about.


So, you get an error here since you are declaring the array T a[1], and that
implies that something (the compiler?) would have to know the size of the
"T" in order to allocate memory? But with a STL-vector , it uses (perhaps
pointers) so it would not have to know the size, or the size is easy to
find, since it's just an sizeof(int)+siz eof(X-pointer)+overhe ad ?

Jul 22 '05 #9
Gunnar G wrote:
Which, I believe,
is what Gunnar G was reporting for an *empty* container.
> cat self.cc

#include <iostream>

template <typename T>
struct myVector {
T a[1];
};

struct X{
int a,b,c;
myVector<X> pp;
};

int main(int argc, char* argv[]){
X a;
return 0;
}
> g++ -Wall -ansi -pedantic -o self self.cc

self.cc: In instantiation of `myVector<X>':
self.cc:10: instantiated from here
self.cc:5: error: `myVector<T>::a ' has incomplete type
self.cc:8: error: forward declaration of `struct X'
self.cc: In function `int main(int, char**)':
self.cc:14: warning: unused variable `X a'

is probably the case that Gunnar G was concerned about.


So, you get an error here since you are declaring the array T a[1]
and that implies that something (the compiler?)
would have to know the size of the "T" in order to allocate memory?
But, with a STL-vector, it uses (perhaps pointers)
so it would not have to know the size, or the size is easy to find
since it's just an sizeof(int)+siz eof(X-pointer)+overhe ad?


I think that that's right. What I can't tell you is
whether the standard specifies that behavior for template class vectors.
What we need is a good language lawyer.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
17654
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
10
3521
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern struct foobar; ^
5
3307
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors. The four files are main.c, main.h, readLP.h, and readLP.c. In readLP.h
19
2644
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; } RtWidget;
16
3846
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
4356
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read that there is no difference between giving this function a
7
2262
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
5069
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
4
9816
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
2788
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never done this before, I'm sure it's a rookie mistake but I cannot seem to find it. Can someone render some assistance please? struct Fpos { grib_handle *h;
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.