Jump to content

Recommended Posts

Posted

Hi,

für meine Anwendung benötige ich globale Variablen, die mit einem bestimmten Wert initialisiert werden, und genau da liegt mein Problem:

wenn ich z.B. das global definiere:

unsigned char test = 2;

dann wird in meinem Programm die Variable "test" trotzdem mit 0 initialisiert. Versteh ich nicht.

Anhand meiner Tests wird anscheinend alles auf 0 initialisiert und ich muss per hand in der Init() Fkt. die daten nochmal initialisieren (das frisst natürlich speicher)...oder mach ich was falsch?

Übrigends wird mein Programm auch größer, sobald ich eine Initialiserung in der Deklaration vornehme...obwohl ja hier die Werte auf 0 bleiben..

Kann mir das jemand erklären und was wäre nun die günstigste Variante?

brauch man eigentlich dann werte, die mit 0 initialisiert werden sollen, nicht mehr explizit zu initialisieren, weil das automatisch geschieht?

PS: Ist eine Initialisierung von Variablen in der Deklaration nur in C++ und nicht in C möglich?

Posted

Hi Rio

this is normal behaviour, I think that TK recommended the use of an init function in another thread

I think that MIOS clears all variables on startup so that the initialisations don't have effect

not sure about this, otherwise it's an sdcc related issue

the other C compiler I have used (from Keil) initialises global variables as expected

I hope I did understand your question correctly,

it would be easier if you would post your questions in English...

Michaël

Posted

I hope I did understand your question correctly,

it would be easier if you would post your questions in English...

thx and sorry for german post... yes, seems that this is special at SDCC-Compiler.

But i most wonder about the wasted memory if i initialise the variables at deklaration too....and they are always 0.

So SDCC will init all variables at 0, i'm right?

So i'll only need to initialise variables in init function, if values are different from 0, correct?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...