Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

Problems with constructor of static member class

Problems with constructor of static member class

2005-06-28 by soren_t_hansen

Hi all.

I suspect the GNUARM compiler to have a flaw, since the following code
doesn't work as intended:

(Header)
class A
{
    public:
        A(int x);
        int m_x;
        static A* get_a_Instance();

    private:
        static A m_a;
        static A m_b;

};
(Source)
A A::m_a(1);
A A::m_b(2);

A::A(int x)
{
    m_x = x;
}

A* A::get_a_Instance()
{
   return &m_a;
}

int main()
{
   A* temp_a = get_a_Instance();
   int x = temp_a->m_x;
}

The problem is that the constuctor doesn_t get called, causing m_x to
have a garbage value (or 0). Even if I make an object of type A first,
it doesn't work. 
Is it something I have to setup in my startup.s file?

Best Regards
Søren

Re: [lpc2000] Problems with constructor of static member class

2005-06-28 by thomas.kuschel@anton-paar.com

Hallo Søren,

When you declare a static data member within class, you are _not_ defining 
it. ( That is, your are not allocating strage for it)
Instead you must provide a global definition for it elsewhere, outside the 
class.
This is done by redeclaring the _static_ varialbe using the scope 
resolution operator to identify the class to which it belongs.
This causes storage for the varialbe to be allocated.
(Remember, a class definition is simply a logical construct that does not 
have physical reality - not so in your program)
Try not to use statics within classes - generally
also - if this program probably would work - it's not really good code.

Hope could help

Thomas Kuschel

================
Anton Paar GmbH
Research & Development
Anton-Paar-Str. 20
8054 Graz
Austria
Tel. +43 316 257-469

lpc2000@yahoogroups.com wrote on 28.06.2005 11:22:06:

> Hi all.
> 
> I suspect the GNUARM compiler to have a flaw, since the following code
> doesn't work as intended:
> 
> (Header)
> class A
> {
>     public:
>         A(int x);
>         int m_x;
>         static A* get_a_Instance();
> 
>     private:
>         static A m_a;
>         static A m_b;
> 
> };
> (Source)
> A A::m_a(1);
> A A::m_b(2);
> 
> A::A(int x)
> {
>     m_x = x;
> }
> 
> A* A::get_a_Instance()
> {
>    return &m_a;
> }
> 
> int main()
> {
>    A* temp_a = get_a_Instance();
>    int x = temp_a->m_x;
> }
> 
> The problem is that the constuctor doesn_t get called, causing m_x to
> have a garbage value (or 0). Even if I make an object of type A first,
> it doesn't work. 
> Is it something I have to setup in my startup.s file?
> 
> Best Regards
> Søren
> 
> 
> 
> 
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 
> 
> 


[Non-text portions of this message have been removed]

Re: Problems with constructor of static member class

2005-06-28 by soren_t_hansen

> This is done by redeclaring the _static_ varialbe using the scope 
> resolution operator to identify the class to which it belongs.
> This causes storage for the varialbe to be allocated.

I belive I already do this? In the 2 lines just below (Source)?

> Try not to use statics within classes - generally
> also - if this program probably would work - it's not really good code.

I know that the style isn't great, but it is the best way to approach
the problem we have at hand :o)

/Søren
Show quoted textHide quoted text
> > Hi all.
> > 
> > I suspect the GNUARM compiler to have a flaw, since the following code
> > doesn't work as intended:
> > 
> > (Header)
> > class A
> > {
> >     public:
> >         A(int x);
> >         int m_x;
> >         static A* get_a_Instance();
> > 
> >     private:
> >         static A m_a;
> >         static A m_b;
> > 
> > };
> > (Source)
> > A A::m_a(1);
> > A A::m_b(2);
> > 
> > A::A(int x)
> > {
> >     m_x = x;
> > }
> > 
> > A* A::get_a_Instance()
> > {
> >    return &m_a;
> > }
> > 
> > int main()
> > {
> >    A* temp_a = get_a_Instance();
> >    int x = temp_a->m_x;
> > }
> > 
> > The problem is that the constuctor doesn_t get called, causing m_x to
> > have a garbage value (or 0). Even if I make an object of type A first,
> > it doesn't work. 
> > Is it something I have to setup in my startup.s file?
> > 
> > Best Regards
> > Søren
> > 
> > 
> > 
> > 
> > 
> > 
> > Yahoo! Groups Links
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> 
> 
> [Non-text portions of this message have been removed]

Re: [lpc2000] Re: Problems with constructor of static member class

2005-06-28 by Jaromir

Hi

First :
instead static member , better is use singleton that call function with static object of your class.
Second:
standard startup.s file ( example from freertos or ... ) is only for C, so you nedd add extra code to call all constructors for global (static) obiects. Check WinARM site for example.

Jaromir 
Show quoted textHide quoted text
  ----- Original Message ----- 
  From: soren_t_hansen 
  To: lpc2000@yahoogroups.com 
  Sent: Tuesday, June 28, 2005 1:27 PM
  Subject: [lpc2000] Re: Problems with constructor of static member class



  > This is done by redeclaring the _static_ varialbe using the scope 
  > resolution operator to identify the class to which it belongs.
  > This causes storage for the varialbe to be allocated.

  I belive I already do this? In the 2 lines just below (Source)?

  > Try not to use statics within classes - generally
  > also - if this program probably would work - it's not really good code.

  I know that the style isn't great, but it is the best way to approach
  the problem we have at hand :o)

  /Søren

  > > Hi all.
  > > 
  > > I suspect the GNUARM compiler to have a flaw, since the following code
  > > doesn't work as intended:
  > > 
  > > (Header)
  > > class A
  > > {
  > >     public:
  > >         A(int x);
  > >         int m_x;
  > >         static A* get_a_Instance();
  > > 
  > >     private:
  > >         static A m_a;
  > >         static A m_b;
  > > 
  > > };
  > > (Source)
  > > A A::m_a(1);
  > > A A::m_b(2);
  > > 
  > > A::A(int x)
  > > {
  > >     m_x = x;
  > > }
  > > 
  > > A* A::get_a_Instance()
  > > {
  > >    return &m_a;
  > > }
  > > 
  > > int main()
  > > {
  > >    A* temp_a = get_a_Instance();
  > >    int x = temp_a->m_x;
  > > }
  > > 
  > > The problem is that the constuctor doesn_t get called, causing m_x to
  > > have a garbage value (or 0). Even if I make an object of type A first,
  > > it doesn't work. 
  > > Is it something I have to setup in my startup.s file?
  > > 
  > > Best Regards
  > > Søren
  > > 
  > > 
  > > 
  > > 
  > > 
  > > 
  > > Yahoo! Groups Links
  > > 
  > > 
  > > 
  > > 
  > > 
  > > 
  > > 
  > 
  > 
  > [Non-text portions of this message have been removed]




------------------------------------------------------------------------------
  YAHOO! GROUPS LINKS 

    a..  Visit your group "lpc2000" on the web.
      
    b..  To unsubscribe from this group, send an email to:
     lpc2000-unsubscribe@yahoogroups.com
      
    c..  Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 


------------------------------------------------------------------------------


_________________________________________________________________
List sprawdzony skanerem poczty mks_vir ( http://www.mks.com.pl )


[Non-text portions of this message have been removed]

Re: Problems with constructor of static member class

2005-06-28 by soren_t_hansen

--- In lpc2000@yahoogroups.com, "Jaromir" <jaromir.kajdaniak@z...> wrote:
> Hi
> 
> First :
> instead static member , better is use singleton that call function
with static object of your class.
> Second:
> standard startup.s file ( example from freertos or ... ) is only for 
> C, so you nedd add extra code to call all constructors for global 
> (static) obiects. Check WinARM site for example.


I have found and added the following code: 

		LDR 	r0, =__ctors_start__
		LDR 	r1, =__ctors_end__
ctor_loop:
		CMP 	r0, r1
		BEQ 	ctor_end
		LDR 	r2, [r0], #4
		STMFD 	sp!, {r0-r1}
		MOV 	lr, pc
		MOV 	pc, r2
		LDMFD 	sp!, {r0-r1}
		B 		ctor_loop
ctor_end:

But it doesn't do the trick yet. Is there something else I'm missing
out? I'm using the startup.s file from Keil, which is intended to be
used in C.

/Søren

Re: [lpc2000] Re: Problems with constructor of static member class

2005-06-29 by Jaromir

Check also linker script. Are you sure that Keil support C++ ? I use GCC...

Jaromir

  > First :
  > instead static member , better is use singleton that call function
  with static object of your class.
  > Second:
  > standard startup.s file ( example from freertos or ... ) is only for 
  > C, so you nedd add extra code to call all constructors for global 
  > (static) obiects. Check WinARM site for example.


  I have found and added the following code: 

              LDR       r0, =__ctors_start__
              LDR       r1, =__ctors_end__
  ctor_loop:
              CMP       r0, r1
              BEQ       ctor_end
              LDR       r2, [r0], #4
              STMFD       sp!, {r0-r1}
              MOV       lr, pc
              MOV       pc, r2
              LDMFD       sp!, {r0-r1}
              B             ctor_loop
  ctor_end:

  But it doesn't do the trick yet. Is there something else I'm missing
  out? I'm using the startup.s file from Keil, which is intended to be
  used in C.

  /Søren


_________________________________________________________________
List sprawdzony skanerem poczty mks_vir ( http://www.mks.com.pl )


[Non-text portions of this message have been removed]

Re: Problems with constructor of static member class

2005-06-29 by soren_t_hansen

--- In lpc2000@yahoogroups.com, "Jaromir" <jaromir.kajdaniak@z...> wrote:
> Check also linker script. Are you sure that Keil support C++ ? I use
GCC...
> 

Keil doesn't support C++, but their startup file has a lot of setup
(PLL, MAM) that I use. If i use crt0.s (which I guess you do?) this
stuff isn't there.

I've added the .ctors to the linker script too, but still can't make
it work.

Maybe you can send me a copy of your startup file and your linker
script? I'm growing hairless, because of this problem :o)

/Søren

Re: [lpc2000] Re: Problems with constructor of static member class

2005-06-29 by Jaromir

Maybe problem is, that you use non-default constructor. Static obiects IMHO should have constructors without arguments - startup code (that I use, and that I saw in previus posts) can only call default constructors.

Jaromir
Show quoted textHide quoted text
  ----- Original Message ----- 
  From: soren_t_hansen 
  To: lpc2000@yahoogroups.com 
  Sent: Wednesday, June 29, 2005 10:53 AM
  Subject: [lpc2000] Re: Problems with constructor of static member class


  --- In lpc2000@yahoogroups.com, "Jaromir" <jaromir.kajdaniak@z...> wrote:
  > Check also linker script. Are you sure that Keil support C++ ? I use
  GCC...
  > 

  Keil doesn't support C++, but their startup file has a lot of setup
  (PLL, MAM) that I use. If i use crt0.s (which I guess you do?) this
  stuff isn't there.

  I've added the .ctors to the linker script too, but still can't make
  it work.

  Maybe you can send me a copy of your startup file and your linker
  script? I'm growing hairless, because of this problem :o)

  /Søren


_________________________________________________________________
List sprawdzony skanerem poczty mks_vir ( http://www.mks.com.pl )


[Non-text portions of this message have been removed]

Re: Problems with constructor of static member class

2005-06-30 by soren_t_hansen

--- In lpc2000@yahoogroups.com, "Jaromir" <jaromir.kajdaniak@z...> wrote:
> Maybe problem is, that you use non-default constructor. Static
obiects IMHO should have constructors without arguments - startup code
(that I use, and that I saw in previus posts) can only call default
constructors.

I've come a little closer, solving the problem. I've now made a small
test project which is based on the startup and linker files from a
Winarm c++ project sample. In this test, the constructors gets called
nicely with and without parameters- both for static and nonstatic objects.

Therefore my suspicion in on my makefile and the options I use in the
compilation and linking. That's what I gonna spend my day on today :o)

Thanks for the help so far - I'll come back (I hope) with the solution

Best Regards
Søren

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.