# Question of the day!!!

Discussion in 'C Programming' started by Anshum Kocher, Oct 1, 2011.

1. ### Anshum KocherGuest

Code A:
float a=5 ,b=10;
float c=a+(b-a)/a+(b-a)*(b-a);
//End of code A

Code B:
float a=5,b=10;
float d=b-a;
float c=a+d/a+d*d;

Question:

Which code segment A or B takes more memory and which ones take more
memory?

Anshum Kocher, Oct 1, 2011

2. ### WillemGuest

Anshum Kocher wrote:
) Code A:
) float a=5 ,b=10;
) float c=a+(b-a)/a+(b-a)*(b-a);
) //End of code A
)
) Code B:
) float a=5,b=10;
) float d=b-a;
) float c=a+d/a+d*d;
)
) Question:
)
) Which code segment A or B takes more memory and which ones take more
) memory?

Your question doesn't make sense. Even ignoring the poor wording.
In any case, any decent compiler will generate the same code for both,
assuming 'd' isn't used somewhere else.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Willem, Oct 1, 2011

3. ### James KuyperGuest

On 10/01/2011 09:04 AM, Anshum Kocher wrote:
> Code A:
> float a=5 ,b=10;
> float c=a+(b-a)/a+(b-a)*(b-a);
> //End of code A
>
> Code B:
> float a=5,b=10;
> float d=b-a;
> float c=a+d/a+d*d;
>
> Question:
>
> Which code segment A or B takes more memory and which ones take more
> memory?

That depends entirely upon the compiler and the context. Assuming that
the above is the only difference between two otherwise identical
programs, most decent compilers should generate essentially equivalent
code for both cases.
--
James Kuyper

James Kuyper, Oct 1, 2011
4. ### Nick KeighleyGuest

On Oct 1, 2:04 pm, Anshum Kocher <> wrote:
> Code A:
> float a=5 ,b=10;
> float c=a+(b-a)/a+(b-a)*(b-a);
> //End of code A
>
> Code B:
> float a=5,b=10;
> float d=b-a;
> float c=a+d/a+d*d;
>
> Question:
>
> Which code segment A or B takes more memory and which ones take more
> memory?

you do know you asked the same question twice? Why don't you just
measure it, if you care. I suspect the answer is "not much" or "none
at all". For the record I'd use the second version as I don't like
repeating myself. Oh and I'd prefer double to float.

Nick Keighley, Oct 2, 2011
5. ### Anshum KocherGuest

On Oct 2, 3:01 pm, Nick Keighley <>
wrote:
> On Oct 1, 2:04 pm, Anshum Kocher <> wrote:
>
> > Code A:
> > float a=5 ,b=10;
> > float c=a+(b-a)/a+(b-a)*(b-a);
> > //End of code A

>
> > Code B:
> > float a=5,b=10;
> > float d=b-a;
> > float c=a+d/a+d*d;

>
> > Question:

>
> > Which code segment A or B takes more memory and which ones take more
> > memory?

>
> you do know you asked the same question twice? Why don't you just
> measure it, if you care. I suspect the answer is "not much" or "none
> at all". For the record I'd use the second version as I don't like
> repeating myself. Oh and I'd prefer double to float.

It was more of a typing error....the actual question was
Which code segment A or B takes more memory and which ones take more
time?
and it was a question asked to me in a written examination...

Anshum Kocher, Oct 3, 2011
6. ### Eric SosmanGuest

On 10/3/2011 7:17 AM, Anshum Kocher wrote:
>[...]
> It was more of a typing error....the actual question was
> Which code segment A or B takes more memory and which ones take more
> time?
> and it was a question asked to me in a written examination...

If the written examination concerned the C language, the

"It depends on the implementation."
and
"It depends on the implementation."

If the examination concerned a specific implementation of C,
the correct answers would have been

"It depends on the context."
and
"It depends on the context."

If the examination concerned a specific implementation of C
in the context of a specific complete program, the correct answers
would have been

"I'll have to measure it."
and
"Who cares?"

Seriously.

--
Eric Sosman
d

Eric Sosman, Oct 3, 2011
7. ### Jorgen GrahnGuest

On Mon, 2011-10-03, Anshum Kocher wrote:
> On Oct 2, 3:01 pm, Nick Keighley <>
> wrote:
>> On Oct 1, 2:04 pm, Anshum Kocher <> wrote:
>>
>> > Code A:
>> > float a=5 ,b=10;
>> > float c=a+(b-a)/a+(b-a)*(b-a);
>> > //End of code A

>>
>> > Code B:
>> > float a=5,b=10;
>> > float d=b-a;
>> > float c=a+d/a+d*d;

>>
>> > Question:

>>
>> > Which code segment A or B takes more memory and which ones take more
>> > memory?

>>
>> you do know you asked the same question twice? Why don't you just
>> measure it, if you care. I suspect the answer is "not much" or "none
>> at all". For the record I'd use the second version as I don't like
>> repeating myself. Oh and I'd prefer double to float.

>
> It was more of a typing error....the actual question was
> Which code segment A or B takes more memory and which ones take more
> time?
> and it was a question asked to me in a written examination...

Then don't trust whoever wrote that question. Unless it was a trick
understand C (or programming in general).

Both A and B can be optimized away to three constants -- or less if
the rest of the code doesn't use all of them.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Jorgen Grahn, Oct 4, 2011
8. ### henry eshbaughGuest

On Oct 1, 9:04 am, Anshum Kocher <> wrote:
> Code A:
> float a=5 ,b=10;
> float c=a+(b-a)/a+(b-a)*(b-a);
> //End of code A
>
> Code B:
> float a=5,b=10;
> float d=b-a;
> float c=a+d/a+d*d;
>
> Question:
>
> Which code segment A or B takes more memory and which ones take more
> memory?

Independent of architecture and compiler, B takes up more space.

henry eshbaugh, Oct 5, 2011
9. ### Ben BacarisseGuest

henry eshbaugh <> writes:

> On Oct 1, 9:04Â am, Anshum Kocher <> wrote:
>> Code A:
>> float a=5 ,b=10;
>> float c=a+(b-a)/a+(b-a)*(b-a);
>> //End of code A
>>
>> Code B:
>> float a=5,b=10;
>> float d=b-a;
>> float c=a+d/a+d*d;
>>
>> Question:
>>
>> Which code segment A or B takes more memory and which ones take more
>> memory?

>
> Independent of architecture and compiler, B takes up more space.

Why do you say that? Part of my question is just that I know what
"takes up more space" means for code fragments like this.

--
Ben.

Ben Bacarisse, Oct 5, 2011
10. ### Keith ThompsonGuest

henry eshbaugh <> writes:
> On Oct 1, 9:04Â am, Anshum Kocher <> wrote:
>> Code A:
>> float a=5 ,b=10;
>> float c=a+(b-a)/a+(b-a)*(b-a);
>> //End of code A
>>
>> Code B:
>> float a=5,b=10;
>> float d=b-a;
>> float c=a+d/a+d*d;
>>
>> Question:
>>
>> Which code segment A or B takes more memory and which ones take more
>> memory?

>
> Independent of architecture and compiler, B takes up more space.

Evidence?

Yes, I know B defines more objects than A does; that won't necessarily
show up in the generated code.

And in fact "gcc -O3" generates *exactly* the same code for both
(with a small wrapper program that prints the value of c).

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Oct 5, 2011
11. ### NoobGuest

henry eshbaugh wrote:

> Independent of architecture and compiler, B takes up more space.

I count 48 characters for both code fragments.
Ergo, they take the same space.

Noob, Oct 5, 2011