C++ class variables are not initialized

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

C++ class variables are not initialized

跳至解决方案
1,988 次查看
__invited__hari
Contributor V

Hi,

I am facing weird problem with C++.

With the below example, the print message supposed to be 'Value=22'. But I am seeing it as 'Value=1".

May I know the reason for it please?

FYI, I am using S32DS for ARM C++ Application.

Thanks,

Hari

#include "S32K148.h" /* include peripheral declarations S32K148 */
#include <stdio.h>

class MyClass
{
public:
   int a=10;
   MyClass()
   {
      a += 11;
   }
};

MyClass myClass;

void func1(void)
{
   int x = ++myClass.a;
   printf("Value=%d",x);
}

int main()
{
   func1();

   while(1);
   return 0;
}

0 项奖励
回复
1 解答
1,731 次查看
jiri_kral
NXP Employee
NXP Employee

Hi, 

it looks like that there is bug in startup file - thanks for report. You can fix it by adding 

bl _start 

instead

bl main 

in the startupS32K148.S file:

pastedImage_1.png

Hope it helps. 

Jiri 

在原帖中查看解决方案

2 回复数
1,731 次查看
__invited__hari
Contributor V

Thank you Jiri.

That works. Appreciated your quick turnaround.

Best,

Hari

0 项奖励
回复
1,732 次查看
jiri_kral
NXP Employee
NXP Employee

Hi, 

it looks like that there is bug in startup file - thanks for report. You can fix it by adding 

bl _start 

instead

bl main 

in the startupS32K148.S file:

pastedImage_1.png

Hope it helps. 

Jiri