473,414 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Common Problems that Compilers Don't Catch

I am starting this thread after having run into two separate
programming problems,
where the compiler offered no help. The compiler did not even warn.
The programs
compiled fine. And the programs even appeared to work.

The first case was passing directly a variable of type char **.

Here's an example:

prototype: void func(char **);

WRONG

char **var;

func(var);

CORRECT

char *var;

func(&var);

The problem with the above is that the pointer is never initialized.

The second problem I ran into was typecasting an integer to a pointer.

For example:

int var;

foo((void *)var);

The above causes a problem since a pointer and an integer are two
really different types of variables. The problem is that function
required a pointer to be passed, but I needed to work with an integer
in the function I was calling.

Here's the correct way to pass the variable:

foo(&var);

to access the integer

foo(*(int *)var);

Please add additional problems, where the compiler will just compile
without warning or add some clarification to the above examples and
explanations. I prefer to write good code versus code that just works.

Aug 9 '06 #1
9 1673
bw*****@yahoo.com wrote:
I am starting this thread after having run into two separate
programming problems,
where the compiler offered no help. The compiler did not even warn.
The programs
compiled fine. And the programs even appeared to work.

The first case was passing directly a variable of type char **.

Here's an example:

prototype: void func(char **);

WRONG

char **var;

func(var);
Lint would pick this up - use it!
The second problem I ran into was typecasting an integer to a pointer.

For example:

int var;

foo((void *)var);
You used a cast, which tells the compiler you know what you are doing
and suppresses any warnings. <OTThis one reason why C++ added specific
casts </OT>

--
Ian Collins.
Aug 9 '06 #2
bw*****@yahoo.com posted:
I am starting this thread after having run into two separate programming
problems, where the compiler offered no help. The compiler did not even
warn. The programs compiled fine. And the programs even appeared to
work.

Computers are our slaves. Your code told the computer what to do, so it did
it.

The first case was passing directly a variable of type char **.

Here's an example:

prototype: void func(char **);

WRONG

char **var;

func(var);

I presume you would like a warning such as:

usage of uninitialised variable
Become more familiar with pointer variables and you won't be making such
mistakes.

The second problem I ran into was typecasting an integer to a pointer.

For example:

int var;

foo((void *)var);

The above causes a problem since a pointer and an integer are two
really different types of variables.

So why perform the cast?

The problem is that function required a pointer to be passed, but I
needed to work with an integer in the function I was calling.

What, something like this:

void Func(void *p)
{
int i = (int)p;

DoSomething(i);
}

int main()
{
int i = 5;

Func( (void*)i );
}

That's a horrid design (and isn't guaranteed to work). If anything, you
should use the "void*" to store the ADDRESS of the integer, rather than the
integer itself:

void Func(void *p)
{
int i = *(int*)p;

DoSomething(i);
}

int main()
{
int i = 5;

Func(&i);
}

Please add additional problems, where the compiler will just compile
without warning or add some clarification to the above examples and
explanations. I prefer to write good code versus code that just works.

All I can say is that you need to practise C programming more.

--

Frederick Gotham
Aug 9 '06 #3
On Wed, 9 Aug 2006 13:28:17 UTC, Frederick Gotham <fg*******@SPAM.com>
wrote:
bw*****@yahoo.com posted:
I am starting this thread after having run into two separate programming
problems, where the compiler offered no help. The compiler did not even
warn. The programs compiled fine. And the programs even appeared to
work.


Computers are our slaves. Your code told the computer what to do, so it did
it.

The first case was passing directly a variable of type char **.

Here's an example:

prototype: void func(char **);

WRONG

char **var;

func(var);


I presume you would like a warning such as:

usage of uninitialised variable
Become more familiar with pointer variables and you won't be making such
mistakes.

The second problem I ran into was typecasting an integer to a pointer.

For example:

int var;

foo((void *)var);

The above causes a problem since a pointer and an integer are two
really different types of variables.


So why perform the cast?

The problem is that function required a pointer to be passed, but I
needed to work with an integer in the function I was calling.


What, something like this:

void Func(void *p)
{
int i = (int)p;
enters UB. a pinter is not an int. So casing it to int will produce
something but seldom what you thinks it should produce.
DoSomething(i);
}

int main()
{
int i = 5;

Func( (void*)i );
}

That's a horrid design (and isn't guaranteed to work).
It is goiaranteed NOT to work.

If anything, you
should use the "void*" to store the ADDRESS of the integer, rather than the
integer itself:

void Func(void *p)
{
int i = *(int*)p;
DoSomething(i);
}

int main()
{
int i = 5;

Func(&i);
}

Please add additional problems, where the compiler will just compile
without warning or add some clarification to the above examples and
explanations. I prefer to write good code versus code that just works.


All I can say is that you need to practise C programming more.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 9 '06 #4

<bw*****@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
I am starting this thread after having run into two separate
programming problems,
where the compiler offered no help. The compiler did not even warn.
Probably. The idea is that it will let you do things "comparable" to
assembler. Have you tried the Salford compiler?
http://www.polyhedron.com/salford/pr...other/scc.html
I seem to remember it was designed to catch things like those below...
The programs
compiled fine. And the programs even appeared to work.

The first case was passing directly a variable of type char **.

Here's an example:

prototype: void func(char **);

WRONG

char **var;

func(var);

CORRECT

char *var;

func(&var);

The problem with the above is that the pointer is never initialized.

The second problem I ran into was typecasting an integer to a pointer.

For example:

int var;

foo((void *)var);

The above causes a problem since a pointer and an integer are two
really different types of variables. The problem is that function
required a pointer to be passed, but I needed to work with an integer
in the function I was calling.

Here's the correct way to pass the variable:

foo(&var);

to access the integer

foo(*(int *)var);

Please add additional problems, where the compiler will just compile
without warning or add some clarification to the above examples and
explanations. I prefer to write good code versus code that just works.

Aug 9 '06 #5
Herbert Rosenau posted:
>void Func(void *p)
{
int i = (int)p;

enters UB. a pinter is not an int. So casing it to int will produce
something but seldom what you thinks it should produce.

Not UB, but rather implementation-specific behaviour. It's possible that
however the following expression could result in a trap representation:

(int)p

>int main()
{
int i = 5;

Func( (void*)i );
}

That's a horrid design (and isn't guaranteed to work).

It is goiaranteed NOT to work.

No, it's NOT guaranteed not to work. The following works fine on my system:

#include <stdio.h>

void Func(void const *const p)
{
printf("%u", (unsigned)p);
}

int main(void)
{
unsigned i = 123;

Func((void const*)i);

return 0;
}

It's not guaranteed to work by the Standard, and thus is non-portable --
but nonetheless, there's no guarantee that it WON'T work.

--

Frederick Gotham
Aug 9 '06 #6
Frederick Gotham <fg*******@SPAM.comwrites:
bw*****@yahoo.com posted:
[...]
>Here's an example:

prototype: void func(char **);

WRONG

char **var;

func(var);


I presume you would like a warning such as:

usage of uninitialised variable
The standard doesn't require such a warning, but any decent compiler
should give you one if you invoke it with the right arguments. (In
some cases, just enabling warnings won't be enough; you may also have
to enable optimization to force the compiler to do the analysis
necessary to catch the error.)

Of course it's better not to make such an error in the first place.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 9 '06 #7
"Herbert Rosenau" <os****@pc-rosenau.dewrites:
On Wed, 9 Aug 2006 13:28:17 UTC, Frederick Gotham <fg*******@SPAM.com>
wrote:
>bw*****@yahoo.com posted:
I am starting this thread after having run into two separate programming
problems, where the compiler offered no help. The compiler did not even
warn. The programs compiled fine. And the programs even appeared to
work.


Computers are our slaves. Your code told the computer what to do, so it did
it.

The first case was passing directly a variable of type char **.

Here's an example:

prototype: void func(char **);

WRONG

char **var;

func(var);


I presume you would like a warning such as:

usage of uninitialised variable
Become more familiar with pointer variables and you won't be making such
mistakes.

The second problem I ran into was typecasting an integer to a pointer.

For example:

int var;

foo((void *)var);

The above causes a problem since a pointer and an integer are two
really different types of variables.


So why perform the cast?

The problem is that function required a pointer to be passed, but I
needed to work with an integer in the function I was calling.


What, something like this:

void Func(void *p)
{
int i = (int)p;

enters UB. a pinter is not an int. So casing it to int will produce
something but seldom what you thinks it should produce.
Casting a pointer to int does not *necessarily* invoke undefined
behavior. C99 6.3.2.3p6:

Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If the
result cannot be represented in the integer type, the behavior is
undefined. The result need not be in the range of values of any
integer type.

(The phrase "Except as previously specified" refers to null pointer
constants.) If your implementation provides intprt_t and/or
uintptr_t, you can convert a void* to either of those types without
risk of undefined behavior.

That's not to say that converting pointers to integers is a good idea.
If you're writing low-level code *and* you know exactly what you're
doing, it can be useful. If not, you should avoid it. Pointers are
pointers, and integers are integers.
> DoSomething(i);
}

int main()
{
int i = 5;

Func( (void*)i );
}

That's a horrid design (and isn't guaranteed to work).

It is goiaranteed NOT to work.
No, it's not guaranteed not to work. If the implementation allows int
and void* to be converted back and forth, it could do exactly what you
want it to do. But yes, it's most likely a horrid design, unless
there's some really good reason to do it that way.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 9 '06 #8

Keith Thompson wrote:
>
Casting a pointer to int does not *necessarily* invoke undefined
behavior. C99 6.3.2.3p6:

Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If the
result cannot be represented in the integer type, the behavior is
undefined. The result need not be in the range of values of any
integer type.

(The phrase "Except as previously specified" refers to null pointer
constants.)
Also converting a pointer to _Bool; always defined.

Aug 10 '06 #9
On 2006-08-09, bw*****@yahoo.com <bw*****@yahoo.comwrote:
I am starting this thread after having run into two separate
programming problems,
where the compiler offered no help. The compiler did not even warn.
The programs
compiled fine. And the programs even appeared to work.

The first case was passing directly a variable of type char **.

WRONG
char **var;
func(var);

CORRECT
char *var;
func(&var);

The problem with the above is that the pointer is never initialized.
My compiler warns about that, informing me that "var may be used
uninitialized".
The second problem I ran into was typecasting an integer to a pointer.

For example:

int var;

foo((void *)var);

The above causes a problem since a pointer and an integer are two
really different types of variables. The problem is that function
required a pointer to be passed, but I needed to work with an integer
in the function I was calling.
The point of casting is to shut the compiler up. (Actually, there are
other, much more correct, uses, but you've used it here as a silencer.)

When investigating a murder, you can't complain about faulty guns that
don't make loud bangs when silenced.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 12 '06 #10

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

Similar topics

8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
5
by: Steven T. Hatton | last post by:
I often encounter statements in documentation or comments in source code that says things along the lines of "not all compilers support X, so we did not use it." X might be namespaces,...
22
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a...
5
by: nospam_news | last post by:
When language changes make old code uncompilable, that's not what is called protection of investment. New compilers (g++ 3.2.3) reject classes where methods throw the class they belong to. gcc...
0
by: JosAH | last post by:
Greetings, this week's compiler article is all about bookkeeping; boring, I admit it, but we need it for our Tokenizer and Parser(s). Two weeks ago I showed the Tokenizer class code. It uses a...
0
by: JosAH | last post by:
Greetings, welcome back at the sequel of the parsers article chapter. This part is dedicated to the ExpressionParser, the largest parser class for our little language. This class parses a...
19
by: Ant | last post by:
Hi all, I have a question on PyParsing. I am trying to create a parser for a hierarchical todo list format, but have hit a stumbling block. I have parsers for the header of the list (title and...
2
by: Luke | last post by:
I am trying to build a simple PHP Extension which simply returns a string object. I have followed the tutorial found on http://devzone.zend.com/node/view/id/1021 up until the deployment stage....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.