Jump to content

Initialisierung Globaler Variablen


Rio
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...