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 :) )

Recommended Answers

All 5 Replies

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

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.

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 :)

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.

commented: This is great :) +3

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.