this piece of code assigns an int pointer(evident ) to a char,
and when i try to access the ascii value of the char through the
integer pointer(p) ,
what i get is a junk value or not i don't know !
'cause it shows consistent values for diff. chars , for eg.
c = 'a' ans=>-9119
c = 'b' ans=>-9118
c = 'c' ans=>-9117
... and so on
#include <stdio.h>
void main()
{
char c = 'a';
int * p;
p = &c; /*suspicious pointer conversion WARNING !*/
clrscr();
pf("\n%d", *p);
getch();
}
. . . . whats happening ?
Gautam
Nov 14 '05
23 1946
On 2004-03-04, Old Wolf <ol*****@inspir e.net.nz> wrote: I wrote: I generally ignore this, since it may be valid in a freestanding environment.
I generally ignore freestanding environments, as you could raise the objection "it may be valid in a f.e." to just about anything posted on c.l.c. FWIW, void main() may be valid in a hosted environment too.
Yikes. I didn't really mean ignore (my original response to the OP
did not ignore it). I meant I generally discount it as a source of
erroneous behavior.
-- James
In <8M************ ********@comcas t.com> James Hu <jx*@despammed. com> writes: On 2004-03-04, Dan Pop <Da*****@cern.c h> wrote: In <Uo************ ********@comcas t.com> James Hu <jx*@despammed. com> writes:
On 2004-03-04, James Hu <jx*@despammed. com> wrote: >>>[snip]>>> I don't believe a constraint violation results in undefined behavior, >>> but IANACLL (xref: 6.5.16.1p1 and 4p1).
I should qualify that with "c.v. doesn't necessarily result in u'd.b.". [snip] Like I said, IANACLL, but my understanding was that the behavior of a constraint violation is unspecified behavior, not undefined behavior.
Since I have only be looking at the assignment operator, this statement should be restricted to the assignment in question:
char c = 'a'; int *p = &c; The C standard doesn't classify constraint violations in several categories. ALL of them have the same effect: a diagnostic is required after which there are no more requirements from the implementation. An implementation aborting the translation process after issuing a required diagnostic would be perfectly conforming.
I think this is true of C89. But, C99 seems to have changed things.
Nope, C99 hasn't changed anything here. ALL constraint violations are
treated identically by C99, as well.
For one thing:
xref 4p2: If a "shall" or "shall not" requirement that appears outside of a constraint is violated, the behavior is undefined.
And this sentence is repeated in the Appendix the summarizes undefined behaviors. If a constraint violation implied undefined behavior, I believe the standard would not have bothered to state this exception.
Please engage your brain. Undefined behaviour as such does NOT require
a diagnostic. Hence the distinction: violating a shall inside a
constraint requires a diagnostic, violating a shall outside a constraint
doesn't.
The standard stil doesn't provide any clue about the behaviour of a
program violating a constraint, unless you can prove otherwise.
As to the particular assignment, there is a curious example in the standard:
xref 6.5.16.1p6: EXAMPLE3: Consider the fragment:
const char **cpp; char *p; const char c = 'A';
cpp = &p; // constraint violation *cpp = &c; // valid *p = 0; // valid
The first assignment is unsafe because it would allow the following valid code to attempt to change the value of the const object c.
It seems the standard is implying the statement with the constraint violation still has valid semantics.
That would be the case if the text said "because it allows the following".
As it is, the standard explains the semantics if the statement in question
were allowed by the language.
The standard explains why the statement is not actually allowed in
correct C programs. A compiler is not required to translate such code,
which wouldn't be the case if the code had valid semantics.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <84************ **************@ posting.google. com> ol*****@inspire .net.nz (Old Wolf) writes: >> > char c = 'a'; >> > int * p; >> > >> > p = &c; /*suspicious pointer conversion WARNING !*/
>Actually, undefined behaviour is introduced by "void main()", >and also by the line "p = &c;"
Nope, this is a constraint violation and requires a diagnostic. Mere undefined behaviour doesn't.
This sentence seems to imply that constraint violations are all UB (otherwise, I fail to understand your use of 'mere')
AFTER the reqired diagnostic is produced. Big difference! >(for example, it could produce >a hardware exception on a device which requires int pointers >to be correctly aligned). It is UB to point a pointer to an
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^ >object of an incompatible type. ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^ Only in certain cases, when the incompatible type has looser alignment requirements. Character pointers (of all three types) can be pointed to any object. Pointers to unsigned char can even be safely dereferenced after that. A pointer to int can be safely made to point to a struct whose first member has type int. It can be even dereferenced, if the struct member has already been initialised.
I agree totally, but the case in point (pointer to int being made to point to a char) is not in your list of acceptable pointings.
I was addressing your sweeping generalisation underlined above,
that has precious little to do with the case in point. However, even the
case in point is perfectly OK on implementations where all types have
identical alignment requirements. That is, assuming that the required
cast is present.
Anyway, here is the chapter and verse:
7 A pointer to an object or incomplete type may be converted to
a pointer to a different object or incomplete type. If the
resulting pointer is not correctly aligned for the pointed-to
type, the behavior is undefined.
Doesn't quite match your statement, does it?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
On 2004-03-05, Dan Pop <Da*****@cern.c h> wrote: In <8M************ ********@comcas t.com> James Hu <jx*@despammed. com> writes:
[snip]For one thing:
xref 4p2: If a "shall" or "shall not" requirement that appears outside of a constraint is violated, the behavior is undefined.
And this sentence is repeated in the Appendix the summarizes undefined behaviors. If a constraint violation implied undefined behavior, I believe the standard would not have bothered to state this exception. Please engage your brain.
I have given you the benefit of the doubt. But even if I had not,
I would still give you the courtesy of keeping the sentiment to
myself.
Undefined behaviour as such does NOT require a diagnostic. Hence the distinction: violating a shall inside a constraint requires a diagnostic, violating a shall outside a constraint doesn't.
There is no need to make that distinction with the wording that I
quoted. 5.1.1.3 already makes it clear a constraint violation
requires a diagnostic, even in the cases it is explicitly stated
to cause undefined behavior. The quote:
xref 5.1.1.3p1: A conforming implementation shall produce at least
one diagnostic message ... if a preprocessing translation unit or
translation unit contains a violation of any syntax rule or constraint,
even if the behavior is also explicitly specified as undefined or
implementation-defined.
So, if the sentence I cited in 4p2 just drops "that appears outside
of a constraint", a diagnostic would still be required for a constraint
violation. Instead, the standard clearly draws a line, as if to say
constraint violations do not necessarily imply undefined behavior.
The standard stil doesn't provide any clue about the behaviour of a program violating a constraint, unless you can prove otherwise.
I have changed my mind. It is not unspecified behavior. My deduction
is now that the standard is realizing that a program with a constraint
violation may not exhibit any behavior whatsoever. Only if the
constraint violation also violates syntax or violates semantics does the
behavior of the program become undefined.
The example in 5.1.1.3 points to this:
xref 5.1.1.3p2: EXAMPLE An implementation shall issue a diagnostic
for the translation unit:
char i;
int i;
because in those cases where the wording in this International
Standard describes the behavior for a construct as being both
a constraint error and resulting in undefined behavior, the
constraint error shall be diagnosed.
The constraint error in this case is a violation of 6.7p3. But the
undefined behavior is the consequence of the defined semantics in
6.7.5p2. As to the particular assignment, there is a curious example in the standard:
xref 6.5.16.1p6: EXAMPLE3: Consider the fragment:
const char **cpp; char *p; const char c = 'A';
cpp = &p; // constraint violation *cpp = &c; // valid *p = 0; // valid
The first assignment is unsafe because it would allow the following valid code to attempt to change the value of the const object c.
It seems the standard is implying the statement with the constraint violation still has valid semantics.
That would be the case if the text said "because it allows the following". As it is, the standard explains the semantics if the statement in question were allowed by the language.
The standard explains why the statement is not actually allowed in correct C programs. A compiler is not required to translate such code, which wouldn't be the case if the code had valid semantics.
If the result of the constraint violation is undefined behavior, what
does it matter what the hypothetical semantics of it and following
statements would be? I don't think the standard would waste time
discussing hypothetical semantics, as such discussions should be
left in footnotes and rationales. The semantics are real, as if the
pointer conversion of the RHS was coerced via an implicit cast.
-- James This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rebecca Hoffmann |
last post by:
Hi,
I have a serious problem while compiling a small project (a part of
the Modular Flow Scheduling Middleware: ex1):
There are 3 linker errors, all from symbols that point to templates:
-- verbose build output --------------------------------------------
ex1.obj : error LNK2001: unresolved external symbol "public: virtual
|
by: Lorenzo J. Lucchini |
last post by:
My code contains this declaration:
: typedef union {
: word Word;
: struct {
: byte Low;
: byte High;
: } Bytes;
: } reg;
|
by: Andrej Prsa |
last post by:
Hi!
What happens to a globally defined pointer, e.g.
void *value;
that points to a particular type in a function:
int dummy_func (int a)
{
|
by: Tran Hong Quang |
last post by:
Hi,
I have code like this:
unsigned char *buf
*buf=1234567;
How I detect the the size of data buf is pointing to? (In this case is 7)
Thanks
|
by: william |
last post by:
My question is: Specific memory block where my pointer pointing to
changed strangely, seemingly that no statement changed it.
Here are two examples I got:
***********1*****************
I was about to read from a floppy image and build a tree for all the
directories and files.
My question is only about a small portion where I had debugging
problem, and I marked the place below at two places using
"<======================"(you can try to...
| |
by: jois.de.vivre |
last post by:
Hello, I was wondering if the following code was ok:
// ----------- start code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
|
by: Martin |
last post by:
When referring to the conforming declaration for main, Lint displays
Info 818: Pointer parameter 'argv' (line 3) could be declared as
pointing to const
Presumably it's saying that the definition could be:
int main(int argc, char *const *argv)
Why didn't C89 mandate that?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |