We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,307 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Compilers: Are variables stored i RAM when no calculations are made?

I hope I'm posting in the right forum :)

I'm primarily concerned with C compilers and sun's JDK, but general info is more than welcome (Documentation for self-education as well).

Well, if i wrote a program like

int i = 5
printf("i is $d", i);

Would i=5 be stored separately in memory, or would it exactly the same the following?

printf("i is 5");

(perhaps ("i is $d", 5) is more accurate)

If not, one might balance readablity/maintainability against performance (small improvement perhaps, but I'm a sucker for learning these things, if not using them :) )

3
Contributors
5
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
Bladtman242
Junior Poster
185 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
Skill Endorsements: 0

Sincerely i didn't understand your code,im new to programming.
Usually, even if not used, the variables are stored in memory.

Karlwakim
Junior Poster in Training
94 posts since Dec 2011
Reputation Points: 27
Solved Threads: 2
Skill Endorsements: 0

I hope I'm posting in the right forum :)

I'm primarily concerned with C compilers and sun's JDK, but general info is more than welcome (Documentation for self-education as well).

Well, if i wrote a program like

int i = 5
printf("i is $d", i);

Would i=5 be stored separately in memory, or would it exactly the same the following?

printf("i is 5");

(perhaps ("i is $d", 5) is more accurate)

If not, one might balance readablity/maintainability against performance (small improvement perhaps, but I'm a sucker for learning these things, if not using them :) )

An optimizing compiler would likely choose your final option, assuming there's nothing in the code that would require i to exist as an actual entity. There are a huge number of factors involved when it comes to optimization though, so without a full example it's hard to tell you what the possible outcomes are.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55

There are a huge number of factors involved when it comes to optimization though, so without a full example it's hard to tell you what the possible outcomes are.

Okay, I'm quite happy just having learned that "optimizing compilers" probably will optimize this.
So, if I may lean on the boundaries of between the computer science and C forum, I'm guessing with gcc there will be no optimization, unless called with -O or similar parameters? :)

EDIT: Karlwakim: perhaps I should have made it more pseudo code-ish the code is just this

Variable = 5
print-to-std-output(Variable is *VALUE_OF_Variable*);

which the compiler might see is the same as print-to-std-output(Variable is 5); As no operations are done to the variable. It is only kept there for readability/maintainability :)

Bladtman242
Junior Poster
185 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
Skill Endorsements: 0

I'm guessing with gcc there will be no optimization, unless called with -O or similar parameters?

It's best not to guess. GCC is very aggressive when it comes to optimization, so a test using assembly output would be the better approach.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55

I had no idea gcc could do that, that is awesome! :)
Not that assembly is my strongest, but I guess it's an opportunity to learn eh?
Thanks a bunch, I'll be looking into this :)

Bladtman242
Junior Poster
185 posts since Dec 2008
Reputation Points: 18
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Narue and Karlwakim

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0812 seconds using 2.67MB