Velocity Reviews > Test you C skills !!!!!!!

# Test you C skills !!!!!!!

aarklon@gmail.com
Guest
Posts: n/a

 11-14-2007
Hi all,

I was going through the book Test your C skills by "Test your C
skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6

I have got the following doubts ,questions...???

1)On shifting, the bits from the left are rotated and brought to the
right and accomodated where there is empty space on the right.

the reader is asked to tell if this statement is true or not???

The answer is given as false, when i discussed with my friends they
told me that correct answer is implementation dependent.

well what is the correct answer...???

2) To free() we only pass the pointer to the block of memory which we
want to deallocate.Then how does free() know how many bytes it should
deallocate()...???

the answer is given as follows:-

In most implementations of malloc() the number of bytes allocated
is stored adjacent to the allocated block.Hence it is simple for
free() to know how many bytes to deallocate().

To what extent this answer is true...???

Richard Heathfield
Guest
Posts: n/a

 11-14-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) said:

> Hi all,
>
> I was going through the book Test your C skills by "Test your C
> skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6
>
> I have got the following doubts ,questions...???
>
> 1)On shifting, the bits from the left are rotated and brought to the
> right and accomodated where there is empty space on the right.
>
> the reader is asked to tell if this statement is true or not???
>
> The answer is given as false, when i discussed with my friends they
> told me that correct answer is implementation dependent.
>
> well what is the correct answer...???

It's false for one and a half reasons. Firstly, no rotation occurs.
Secondly, even if it did, the statement as given would apply only to left
shifts.

> 2) To free() we only pass the pointer to the block of memory which we
> want to deallocate.Then how does free() know how many bytes it should
> deallocate()...???
>
> the answer is given as follows:-
>
> In most implementations of malloc() the number of bytes allocated
> is stored adjacent to the allocated block.Hence it is simple for
> free() to know how many bytes to deallocate().
>
> To what extent this answer is true...???

The Standard doesn't say. It is entirely up to the implementation, and it
is certainly true that some implementations do this in the described
manner.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

vippstar@gmail.com
Guest
Posts: n/a

 11-14-2007
On Nov 14, 4:53 pm, (E-Mail Removed) wrote:
> 2) To free() we only pass the pointer to the block of memory which we
> want to deallocate.Then how does free() know how many bytes it should
> deallocate()...???

To free() you can pass the return value of malloc(), realloc() and
calloc().
May that be a unique pointer, a valid pointer or NULL.

Philip Potter
Guest
Posts: n/a

 11-14-2007
(E-Mail Removed) wrote:
> Hi all,
>
> I was going through the book Test your C skills by "Test your C
> skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6
>
> I have got the following doubts ,questions...???
>
> 1)On shifting, the bits from the left are rotated and brought to the
> right and accomodated where there is empty space on the right.
>
> the reader is asked to tell if this statement is true or not???
>
> The answer is given as false, when i discussed with my friends they
> told me that correct answer is implementation dependent.
>
> well what is the correct answer...???

The book is correct, your friends are not.

n1256 6.5.7p4 says:
"The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
bits are filled with zeros."
p5 says:
"The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has
an unsigned type or if E1 has a signed type and a nonnegative value, the
value of the result is the integral part of the quotient of E1 / 2^{E2}.
If E1 has a signed type and a negative value, the resulting value is
implementation-defined."

(Where x^{y} is x to the power of y)

In theory, the implementation could define right-shifting of negative
numbers to be a rotate-right operation instead. But it couldn't do the
same of positive numbers, so the principle of least surprise demands
that an implementation shift in zeroes or sign-extend (logical or
arithmetic right shift respectively).

This leads me to an interesting question: Is right-shifting defined at
all on plain char?

--
Philip Potter pgp <at> doc.ic.ac.uk

Mark Bluemel
Guest
Posts: n/a

 11-14-2007
(E-Mail Removed) wrote:
> Hi all,
>
> I was going through the book Test your C skills by "Test your C
> skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6
>
> I have got the following doubts ,questions...???
>
> 1)On shifting, the bits from the left are rotated and brought to the
> right and accomodated where there is empty space on the right.
>
> the reader is asked to tell if this statement is true or not???
>
> The answer is given as false, when i discussed with my friends they
> told me that correct answer is implementation dependent.

> well what is the correct answer...???

Did you look to find out what the standard says?
Or a standard reference text?
If not, why not? Do you trust uninformed chatter with your friends
more than the standard or a good text book?

> 2) To free() we only pass the pointer to the block of memory which we
> want to deallocate.Then how does free() know how many bytes it should
> deallocate()...???

> the answer is given as follows:-
>
> In most implementations of malloc() the number of bytes allocated
> is stored adjacent to the allocated block.Hence it is simple for
> free() to know how many bytes to deallocate().
>
> To what extent this answer is true...???

All that is required is that free knows how many bytes to deallocate,
the way it knows that is up to the implementer. They may use the
strategy you describe, but there are others. A C programmer has no
need, generally, to know what strategy is used.

Ben Bacarisse
Guest
Posts: n/a

 11-14-2007
Philip Potter <(E-Mail Removed)> writes:

> This leads me to an interesting question: Is right-shifting defined at
> all on plain char?

The operands are promoted so in one sense the answer is no (because
the shift happens on the promoted value) and in another sense yes
because the meaning will depend on what char promotes to.

--
Ben.

Ben Pfaff
Guest
Posts: n/a

 11-14-2007
(E-Mail Removed) writes:

> On Nov 14, 4:53 pm, (E-Mail Removed) wrote:
>> 2) To free() we only pass the pointer to the block of memory which we
>> want to deallocate.Then how does free() know how many bytes it should
>> deallocate()...???

>
> To free() you can pass the return value of malloc(), realloc() and
> calloc().
> May that be a unique pointer, a valid pointer or NULL.

This is all true, but it's hard to see how it bears on the OP's
question.
--
Ben Pfaff
http://benpfaff.org

pete
Guest
Posts: n/a

 11-14-2007
(E-Mail Removed) wrote:
>
> Hi all,
>
> I was going through the book Test your C skills by "Test your C
> skills" by Yashavant p. Kanetkar,ISBN 81-7029-801-6
>
> I have got the following doubts ,questions...???
>
> 1)On shifting, the bits from the left are rotated and brought to the
> right and accomodated where there is empty space on the right.
>
> the reader is asked to tell if this statement is true or not???
>
> The answer is given as false, when i discussed with my friends they
> told me that correct answer is implementation dependent.
>
> well what is the correct answer...???

> 2) To free() we only pass the pointer to the block of memory which we
> want to deallocate.Then how does free() know how many bytes it should
> deallocate()...???
>
> the answer is given as follows:-
>
> In most implementations of malloc() the number of bytes allocated
> is stored adjacent to the allocated block.Hence it is simple for
> free() to know how many bytes to deallocate().
>
> To what extent this answer is true...???

I don't care.
Either you know what your target platform is, or you don't.

If you don't,
then it doesn't make sense to write code as though you do.

--
pete

Old Wolf
Guest
Posts: n/a

 11-14-2007
On Nov 15, 4:26 am, Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) said:
>
> > 1)On shifting, the bits from the left are rotated and brought to the
> > right and accomodated where there is empty space on the right.

>
> > the reader is asked to tell if this statement is true or not???

>
> It's false for one and a half reasons. Firstly, no rotation occurs.
> Secondly, even if it did, the statement as given would apply only to left
> shifts.

I assume he is talking about left shifts. A left shift
could be implemented as a rotation (shifting a 0 off the
left requires a 0 to enter at the right; shifting a 1
off the left causes undefined behaviour).

Jack Klein
Guest
Posts: n/a

 11-15-2007
On 14 Nov 2007 14:02:28 -0800, Old Wolf <(E-Mail Removed)> wrote
in comp.lang.c:

> On Nov 15, 4:26 am, Richard Heathfield <(E-Mail Removed)> wrote:
> > (E-Mail Removed) said:
> >
> > > 1)On shifting, the bits from the left are rotated and brought to the
> > > right and accomodated where there is empty space on the right.

> >
> > > the reader is asked to tell if this statement is true or not???

> >
> > It's false for one and a half reasons. Firstly, no rotation occurs.
> > Secondly, even if it did, the statement as given would apply only to left
> > shifts.

>
> I assume he is talking about left shifts. A left shift
> could be implemented as a rotation (shifting a 0 off the
> left requires a 0 to enter at the right; shifting a 1
> off the left causes undefined behaviour).

Not on unsigned types, it couldn't.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pmp C Programming 7 10-28-2009 09:23 PM pmp C++ 1 10-24-2008 06:22 PM Sharath C Programming 24 04-23-2007 09:19 PM =?Utf-8?B?S2Fnc0NvZ3M=?= MCSD 2 09-16-2005 06:11 PM Computer Support 2 07-02-2003 06:02 PM