public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* A problem about g++ 4.8.5
@ 2024-03-28  9:34 shaoben zhu
  2024-03-28 10:51 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: shaoben zhu @ 2024-03-28  9:34 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 469 bytes --]

I compile my program using g++ 4.8.5, I find that when my program exits, it
first deconstructs the static member variables of class A, and then
deconstructs a global object of class A. This caused an error in my program.
Could you tell me how can I avoid this problem?Upgrade compiler
version?Modify my code?

my code like this:
class A{
static int var;
~A();                       //A  Destructor depended var
};

int A::var;
A       obj;

var deconstructs before obj

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: A problem about g++ 4.8.5
  2024-03-28  9:34 A problem about g++ 4.8.5 shaoben zhu
@ 2024-03-28 10:51 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2024-03-28 10:51 UTC (permalink / raw)
  To: shaoben zhu; +Cc: gcc

Hello,
This mailing list is for discussion the development of GCC itself.
Please use the gcc-help mailing list for help questions. Please send
any replies to that list instead of this one, thanks.

On Thu, 28 Mar 2024 at 09:35, shaoben zhu via Gcc <gcc@gcc.gnu.org> wrote:
>
> I compile my program using g++ 4.8.5, I find that when my program exits, it

That version is ancient and has not been supported by the GCC project
for many years. You might be able to get support for it from a vendor
who provided you with that version.

> first deconstructs the static member variables of class A, and then
> deconstructs a global object of class A. This caused an error in my program.
> Could you tell me how can I avoid this problem?Upgrade compiler
> version?Modify my code?
>
> my code like this:
> class A{
> static int var;
> ~A();                       //A  Destructor depended var
> };
>
> int A::var;
> A       obj;
>
> var deconstructs before obj

That's not what I see using g++ 4.8.5 on CentOS 6. With the following code:

struct M { ~M() { __builtin_puts("~M"); } };
struct A{
static M var;
~A() { __builtin_puts("~A"); }
};

M A::var;
A       obj;

int main()
{
}

I get this output:

~A
~M

N.B. I had to change your int to a class type, because int has no
destructor anyway, so doesn't get destroyed. So your example does not
demonstrate the problem you're describing.

If the definitions of A::var and obj are in the same source file then
the destructors should be in the reverse order of their definitions.
If they are in separate source files, then their initialization order
is unspecified, and so the destruction order is also unspecified. If
you define them in the same file, it should work correctly.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-28 10:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-28  9:34 A problem about g++ 4.8.5 shaoben zhu
2024-03-28 10:51 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).