473,698 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What will happen to "1 << (-1)" in G++?

Left shift by negative numbers, will I get 1/2?

Thanks.

Sep 8 '06
16 2021
Clark S. Cox III wrote:
'give it to G++ and see'."

Except that G++ could do it differently each and every time you run the
program.
Yes, it could. And if you try it a few times, you'll discover if it
does.

I do not understand this hostility towards discovering how your
compiler handles one particular kind of undefined behavior.

Sep 9 '06 #11
Robert J. Hansen wrote:
Clark S. Cox III wrote:
>>'give it to G++ and see'."
Except that G++ could do it differently each and every time you run the
program.

Yes, it could. And if you try it a few times, you'll discover if it
does.
You *still* won't know for sure. It could be the same 20 times in a row,
and then be different on the 21st.

I have personally seen all of the following:
- Undefined behavior that works one way on the development machine, the
production build machine, and several test machines; only to work in a
completely different way on a customer's machine.

- Undefined behavior that works one way with one build of the compiler,
and a different way with the next build.

- Undefined behavior that works one way with one optimization level, and
a different way with another.
I do not understand this hostility towards discovering how your
compiler handles one particular kind of undefined behavior.
Because finding that out provides you with a false sense of security,
and encourages people to write code that makes unwarranted assumptions.

--
Clark S. Cox III
cl*******@gmail .com
Sep 9 '06 #12
You *still* won't know for sure. It could be the same 20 times in a row,
and then be different on the 21st.
Then you know it's not "each and every".
I have personally seen all of the following:
So have I.
Because finding that out provides you with a false sense of security,
and encourages people to write code that makes unwarranted assumptions.
It only encourages a false sense of security if you're dumb enough to
feel secure in undefined behavior. On the other hand, knowing that
"every time I've done this particular bit of undefined behavior in the
past, the results have been like this" is an incredible help in
debugging when you see that exact same pattern of behavior. It's not a
deterministic help, no, but it's an effective heuristic.

If you let the fear of a false sense of security prevent you from
learning how your compiler handles undefined behavior, you have
needlessly and pointlessly crippled yourself.

Sep 9 '06 #13
Robert J. Hansen wrote:
Clark S. Cox III wrote:
'give it to G++ and see'."
Except that G++ could do it differently each and every time you run the
program.

Yes, it could. And if you try it a few times, you'll discover if it
does.
If you try something on your compiler, you might also find that it
works the same way every time and appears to produce the expected
behavior.

If you know that the behavior is undefined, and you still require this
type of experimentation to convince yourself of something, it suggests
that you don't understand what it means for behavior to be undefined.

Sep 10 '06 #14
If you know that the behavior is undefined, and you still require this
type of experimentation to convince yourself of something, it suggests
that you don't understand what it means for behavior to be undefined.
There's nothing of which I need convincing with respect to undefined
behavior. Anyone who relies on undefined behavior is living in sin.

However, it's a simple fact that failures happen. Failures happen
_lots_. They happen when you write code and they happen when other
people write code which is then dumped in your lap. Understanding how
your compiler acts when pushed out of spec is valuable knowledge in
debugging. Understanding what the symptoms are (or "may be", since
undefined behavior is not required to be reproducible) is a good way to
equip yourself for spotting problems in the future.

As an example, while I would certainly never want to work with a
programmer who deliberately wrote off the end of arrays, I would
certainly never want to work with a programmer who had no idea what
problems a buffer overflow could create, or the symptoms of those
problems.

Sep 10 '06 #15
Robert J. Hansen wrote:
Why not try it for yourself? The great virtue of computer science is
that in many ways it's an experimental discipline, not a purely
theoretical one.
this is Really Bad Advice.

It is really _good_ advice, given that he specifically asked a question
tied to a specific implementation. If he wants to know what happens in
G++ when this is done, the best way is to try it for yourself. It's
not as if the compiler costs a lot of money to download.
this is comp.lang.c++. They don't discuss particular implementations .
Even if the question is confined to G++, my points (which you snipped)
about different versions of the compiler or optimisation levels still
apply. Undefined behaviour is, by definition, undefined.
--
Nick Keighley

Sep 10 '06 #16

Robert J. Hansen wrote:
You *still* won't know for sure. It could be the same 20 times in a row,
and then be different on the 21st.

Then you know it's not "each and every".
You don't know that if you only try it once or twice.
Because finding that out provides you with a false sense of security,
and encourages people to write code that makes unwarranted assumptions.

It only encourages a false sense of security if you're dumb enough to
feel secure in undefined behavior.
Or if you don't realise that the behaviour is undefined.
On the other hand, knowing that
"every time I've done this particular bit of undefined behavior in the
past, the results have been like this" is an incredible help in
debugging when you see that exact same pattern of behavior. It's not a
deterministic help, no, but it's an effective heuristic.
Absolutely true, if, and only if, you know beforehand that the
behaviour is undefined.

Knowing that "every time I've done this particular bit of code, the
results have been like this" is at best incredibly unhelpful and at
worst dangerous in debugging when you see a different pattern of
behaviour if you aren't pre-armed with the knowledge that "this
particular bit of code" has undefined behaviour.

To me, and I suspect to other respondants in this thread, it looked
like the OP was at risk of relying on some undefined behaviour because
he did not realise the behaviour of his code was undefined.

Gavin Deane

Sep 10 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2937
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
2
1203
by: Xiangliang Meng | last post by:
Hi, all. What will happen if a virtual function is declared to be virtual again in a derived class? Any harm? Best Regards, Xiangliang Meng
4
1353
by: Anil | last post by:
Question related to ASP.Net web application with .Net Framework 1.1. I am using ADO.Net and ODBC namespace. When the user clicks the process button I am doing database INSERT/UPDATRE.. activity and it will take 30 seconds to complete the database activity and then the user will be redirected to different screen.
67
3777
by: neilcancer | last post by:
i come from china,and i'm sorry that my english is very poor. now i'm studing data structure and i met some problem about c language. could you tell me what will happen after i use free()? i mean once i use free() on a pointer,what will the pointer points to ? for example: #include<stdio.h>
17
6795
by: Ravi | last post by:
void main() { main(); } int main() { main(); }
10
1714
by: Shraddha | last post by:
int *s=(int *)2000; int *p=(int *)1000; printf("%d",p-s);
3
1714
by: vimalankvk80 | last post by:
what will happen, if we forget to return ostream referance ? class A { Private: int _a; int _b;
11
1501
by: active | last post by:
If I install .NET Framework 3.0 what will happen to my VS2005 experience? Will it automatically use 3.0? Will I find new features available? Will the VS doc be updated? Thanks fir any info
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4361
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.