473,549 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why compiler not generating any warning ?

Consider the following piece of code:

struct junk {
int i_val;
int i_val1;
char c_val;
};

int main(void)
{
struct junk str_junk;
int * i_ptr;
struct junk * pstr_junk;
pstr_junk = &str_junk;
i_ptr = (int *)pstr_junk + 1; /* shouldn't the compiler
give warning here ? */
}

On compiling this program: cc -c99 -check -portable test.c
I don't get any warning.
However, the statement
i_ptr = (int *)pstr_junk + 1; seems to be incorrect to me.

Here, pstr_junk is a pointer to structure and it is being typecasted to
"int *" which may cause undefined behaviour.
But on compilation I don't get any warning message.
So, what is the way to cacth these type of bugs ? Is there
any other tool that may find out all undefined, unportable
behaviour ?
I tried *lint* as well but it also didn't warned for this.

Only, the typecasting to "char *" should be allowed which is
*guaranteed* to point to the lowest addressed byte of the structure.

Nov 14 '05 #1
29 2484
ju**********@ya hoo.co.in wrote:
But on compilation I don't get any warning message.
So, what is the way to cacth these type of bugs ?


Learn more C.

--
pete
Nov 14 '05 #2
ju**********@ya hoo.co.in wrote:
Consider the following piece of code: struct junk {
int i_val;
int i_val1;
char c_val;
}; int main(void)
{
struct junk str_junk;
int * i_ptr;
struct junk * pstr_junk;
pstr_junk = &str_junk;
i_ptr = (int *)pstr_junk + 1; /* shouldn't the compiler
give warning here ? */
} On compiling this program: cc -c99 -check -portable test.c
I don't get any warning.
However, the statement
i_ptr = (int *)pstr_junk + 1; seems to be incorrect to me. Here, pstr_junk is a pointer to structure and it is being typecasted to
"int *" which may cause undefined behaviour.
You will also get undefined behaviour when you write past the end of
an array, if you use a pointer to already deallocated memory etc. ect.
but the compiler won't warn about that.
But on compilation I don't get any warning message.
Well, you tell the compiler "convert this pointer to a pointer of
a different type". So the compiler does, and since you explicitely
told it to do that why should it doubt the wisdom of your commands?
If you cast you basically say "I know what I am doing, don't tell
me I shouldn't, if I would like you to warn me I wouldn't use that
[expletive censored] cast". And afterwards you tell it "assign to
a pointer from a pointer of the same type" which is 100% legal and
not dangerous at all.
So, what is the way to cacth these type of bugs ? Is there
any other tool that may find out all undefined, unportable
behaviour ?
The best tool is probably between your ears;-) View all casts as
being suspicious until you're satisifed that they are ok. Finding
all instances of undefined behaviour would be impossible, it might
only happen at runtime - the compiler can't typically know in ad-
vance if e.g. in an access of an array element the index is going
to be within the bounds of the array. And undefined behaviour is
not something evil in itself, it's just something the C standard
does not define. And in most programs you actually rely on unde-
fined behaviour to work out fine, e.g. when you call a function
in a third-party library - while a strictly conforming program may
use only those features of the language and library specified in
the C standard you very often need more than just that.
Only, the typecasting to "char *" should be allowed which is
*guaranteed* to point to the lowest addressed byte of the structure.


In this case maybe yes. But there can be situations where you
know what's going to happen on the platform you're working on
(because it's well-documented) and where you have to explicitely
invoke undefined behaviour in order to get things done.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #3
pete wrote:
ju**********@ya hoo.co.in wrote:

But on compilation I don't get any warning message.
So, what is the way to cacth these type of bugs ?

Learn more C.

What is the point of this reply?

He is exactly trying to learn more C and asks in this
discussion group a question.

Of course *you* never asked any questions when you were learning C.

Why being rude to newcomers?

If you do not feel like answering or you think the question is silly
just do not reply.

jakob
Nov 14 '05 #4
jacob navia wrote:

pete wrote:
ju**********@ya hoo.co.in wrote:

But on compilation I don't get any warning message.
So, what is the way to cacth these type of bugs ?

Learn more C.

What is the point of this reply?


The only way that you catch undefined behavior
that your compiler warnings miss, is by knowing enough C.
That's pretty much the whole point of reading this newsgroup.

--
pete
Nov 14 '05 #5

<ju**********@y ahoo.co.in> wrote in message
On compiling this program: cc -c99 -check -portable test.c
I don't get any warning.
However, the statement
i_ptr = (int *)pstr_junk + 1; seems to be incorrect to me.

Here, pstr_junk is a pointer to structure and it is being typecasted to
"int *" which may cause undefined behaviour.
But on compilation I don't get any warning message.


From the switches used, I'll guess you're using the HP C compiler
on Tru64. If that's the case, you can get the compiler to
emit a diagnostic for this case.

Using -check enables most messages, but not all. To enable
all messages use the -msg_enable all option. To get the
specific message for this case, use -msg_enable ansialiascast.

Ed Vogel
HP/Compaq/DEC C/C++ Engineering
Nov 14 '05 #6
On Fri, 03 Jun 2005 04:03:17 -0700, junky_fellow wrote:
Consider the following piece of code:

struct junk {
int i_val;
int i_val1;
char c_val;
};

int main(void)
{
struct junk str_junk;
int * i_ptr;
struct junk * pstr_junk;
pstr_junk = &str_junk;
i_ptr = (int *)pstr_junk + 1; /* shouldn't the compiler
give warning here ? */
}

On compiling this program: cc -c99 -check -portable test.c I don't get
any warning.
However, the statement
i_ptr = (int *)pstr_junk + 1; seems to be incorrect to me.

Here, pstr_junk is a pointer to structure and it is being typecasted to
"int *" which may cause undefined behaviour.
You can safely convert a pointer to a structure to a pointer to the first
element of the structure, no undefined behavior. What is dangerous is the
pointer arithmetic you perform after the conversion. You might expect
i_ptr to point to the second member in the structure but since there may
be padding between the two integers, dereferencing the pointer would
invoke UB.
But on compilation I don't get any warning message.
I don't see anything that requires such a message.
So, what is the way to cacth these type of bugs ? Is there any other
tool that may find out all undefined, unportable behaviour ?
Nothing to add to pete and Jens responses.
I tried *lint* as well but it also didn't warned for this.
Okay.
Only, the typecasting to "char *" should be allowed which is
*guaranteed* to point to the lowest addressed byte of the structure.


Nope. In addition to this and the case I described above, a pointer to a
structure may safely be converted to a pointer to any other structure and
back again.

Robert Gamble
Nov 14 '05 #7
> struct junk str_junk;
int * i_ptr;
struct junk * pstr_junk;
pstr_junk = &str_junk;
i_ptr = (int *)pstr_junk + 1; /* shouldn't the compiler
give warning here ? */


Why? You casted pstr_junk to an integer pointer. Since you are doing
an explicit cast, the compiler assumes that you know what you are doing.
Then you told the integer pointer to advance one, which is a perfectly
valid thing for an integer pointer to do (though, because it's from the
cast, the results are undefined, but since you did an explicit cast, the
compiler won't complain). Then you assigned your new integer pointer to
i_ptr, which is of type int *.

What did you expect the compiler to complain about?

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #8
On 3 Jun 2005 04:03:17 -0700, in comp.lang.c ,
ju**********@ya hoo.co.in wrote:
However, the statement
i_ptr = (int *)pstr_junk + 1; seems to be incorrect to me.
it /is/ incorrect. But you lied to the compiler, by putting in the
cast. This says "shut up, I know what I'm doing" to the compiler.
Hence no warning.
So, what is the way to cacth these type of bugs ?
Avoid casts, which are almost never needed in C, unless you're trying
to do naughty things. Or programme better...
I tried *lint* as well but it also didn't warned for this.
It can't since you already lied and told us that you knew what you
were doing....
Only, the typecasting to "char *" should be allowed which is
*guaranteed* to point to the lowest addressed byte of the structure.


You need to understand the difference between a conversion and a cast.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #9
On Fri, 03 Jun 2005 12:42:44 GMT, in comp.lang.c , pete
<pf*****@mindsp ring.com> wrote:
jacob navia wrote:

pete wrote:
> ju**********@ya hoo.co.in wrote:
>
>
>>But on compilation I don't get any warning message.
>>So, what is the way to cacth these type of bugs ?
>
>
> Learn more C.
>

What is the point of this reply?


The only way that you catch undefined behavior
that your compiler warnings miss, is by knowing enough C.
That's pretty much the whole point of reading this newsgroup.


To be fair though, you could have explained *why* the compiler didn't
grumble, and why using casts is a bad idea.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #10

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

Similar topics

5
11215
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler generates this warning ... "warning C4311: 'type cast' : pointer truncation from 'void *' to 'DWORD'" The warning is generated as the pointer is...
29
1968
by: Daniel Rudy | last post by:
Hello, Consider the following code fragment: fileptr = fopen(param->filename, "r"); if (fileptr == NULL) { error("digest.c: digest_file: Open File ", errno); return(-2); }
8
1996
by: Charles Sullivan | last post by:
I have a program written in C under Linux (gcc) which a user has ported to run under AT&T SysV R4. He sent me a copy of his makelog which displays a large number of compiler warnings similar to this: warning: semantics of ">>" change in ANSI C; use explicit cast The statement to which this applies is: xuc = ((uc & 0xF0 ) >4);
49
2098
by: valentin tihomirov | last post by:
fDeleted = false; uint jobId; foreach (Struct struct in structures) { if (struct.type == JOB) { jobId = struct.id; if (struct.dataType == STATUS) fDeteted = (struct.data & STATUS_DELETED) != 0; }
7
8125
by: Nathan Sokalski | last post by:
I am an ASP.NET developer, and Visual Studio 2005 seems to have stopped declaring the controls that I add in the *.designer.vb files, therefore forcing me to manually add them before I can use them in code that I write in the *.aspx.vb and *.ascx.vb files. Why is it no longer automatically declaring the controls? Is there a way to manually...
1
2186
by: Nathan Sokalski | last post by:
Visual Studio 2005 recently stopped generating the *.designer.vb files for my *.aspx and *.ascx files. I am using Service Pack 1, and do not believe I did anything differently than normal prior to this happening. Is there anything that could have caused this? Does anyone know of a way to fix it? Thanks. -- Nathan Sokalski...
5
3335
by: ganesh.kundapur | last post by:
Hi, I want to know how to compile the C source file without using the compiler directly. I mean to say by using compiler componets such as ( cpp, cc, as, ld ). I tried to compile the fallowing code as fallows file name: hello.c # include <stdio.h> int main ( void )
29
1912
by: Bob | last post by:
Hi, I have been trying to use some inventive alternative idioms for infinite loops in my code, rather than the same old for(;;) and while(1) - this could be a nice amusing Easter-egg for any future maintenance programmers! One of my ideas was this: unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
0
7532
marktang
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...
0
7461
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...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7823
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...
0
6055
agi2029
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...
1
5381
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...
0
5101
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...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.