Hello,
I have a test program that is compiled fine on a 32 bits redhat linux using gcc :
*************** ********
main (argc, argv)
int argc;
char *argv[];
{
int *ka;
int nka;
/* allocation dynamique entiere */
ka = (int *) malloc(nka * sizeof(int));
if (!ka) {
printf ("<ERROR> : Out of heap space (malloc) !\n");
printf ("<ERROR> : %d int words required\n", nka);
exit (-1);
}
}
*************** ********
But under linux suse AMD opteron 64, i get this message from the compiler :
warning: cast to pointer from integer of different size
Any explanation ?
Regards,
Lionel.
--
-=O=------------------------------------------=O=-
Lionel Valéro
Analyste Informatique Département Génie Chimique
École Polytechnique de Montréal
C.P. 6079, succ. centre-ville
Montréal (Québec) H3C 3A7
Tel: (514) 340 - 4711 # 4805 / C552
Fax: (514) 340 - 4159
-=O=------------------------------------------=O=-
Nov 13 '05
54 3593
Eric Sosman wrote: A cryptic and possibly misleading diagnostic is more helpful than no diagnostic at all. In fact, this entire thread stems from an incident in which the unnecessary cast hid the diagnostic that the compiler would otherwise have been required to produce! The O.P.'s mistake in omitting [the declaration of malloc] went unnoticed exactly as we've all said would happen.
You are confused. No diagnostic was hidden.
You need to reread Lionel Valéro's original post.
The diagnostic message was,
"warning: cast to pointer from integer of different size".
It says nothing about the fact that malloc was not declared.
It certainly doesn't tell you that stdlib.h was not included.
My GNU C compiler
gcc --version
gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
issues a more informative diagnostic message:
malloc.c:8: warning: implicit declaration of function `malloc'
The solution to this problem is to get a better C compiler
and *not* to cobble your code to accommodate inferior C compilers.
What benefit does *inserting* the cast provide?
It allows you to compile your code with a C++ compiler.
This is important because it allows your C code to survive
even if the C language itself doesn't survive.
Keith Thompson wrote:
As I mentioned in this thread, I posted an example myself (for gcc under IA-64 Linux) some time ago. Since you seem disinclined to search for it yourself, here's the Google Groups URL:
http://groups.google.com/gr*********...***@cts.com%3E
I posted it about 6 weeks ago in the "why still use C?" thread. You may have missed it the first time around; I see that you didn't participate in the thread after that.
And of course the article that started this thread was yet another perfectly valid example of the problem you keep telling us nobody cares about.
I just happen to have access to an IA-64 Linux machine:
expand main.c
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("sizeof( int) = %d\n", (int)sizeof(int ));
printf("sizeof( void*) = %d\n", (int)sizeof(voi d*));
int* p = (int*)malloc(si zeof(int));
printf("p = [%p]\n", (void*)p);
fflush(stdout);
*p = 42;
printf("*p = %d\n", *p);
return 0;
}
ecc -Wall -std=c99 -o main main.c
main.c(8): warning #266: function declared implicitly
int* p = (int*)malloc(si zeof(int));
^
main.c(8): remark #967: conversion from "int" to "int *";
sizes do not match
int* p = (int*)malloc(si zeof(int));
^
A good C compiler will warn you about this problem
whether or not you use a cast.
"E. Robert Tisdale" wrote: Eric Sosman wrote: A cryptic and possibly misleading diagnostic is more helpful than no diagnostic at all. In fact, this entire thread stems from an incident in which the unnecessary cast hid the diagnostic that the compiler would otherwise have been required to produce! The O.P.'s mistake in omitting [the declaration of malloc] went unnoticed exactly as we've all said would happen. You are confused. No diagnostic was hidden. You need to reread Lionel Valéro's original post. The diagnostic message was, "warning: cast to pointer from integer of different size".
No, the confusion is elsewhere. The O.P. asked why his
program compiled without complaint in one environment and
elicited a diagnostic on another. Without the cast, the
program would have produced diagnostics on *all* C compilers.
Thus, the cast hid the required diagnostic, and it was pure
luck that an "optional" or "extraneous " diagnostic from another
compiler drew his attention to the error.
It says nothing about the fact that malloc was not declared. It certainly doesn't tell you that stdlib.h was not included.
Nor does it tell you how to feed the hungry, heal the sick,
and end war. And when the diagnostic is suppressed altogether,
as in the O.P.'s original environment, it doesn't even tell
you to come in out of the rain.
But if the diagnostic had been permitted to appear, it
would at least have alerted the O.P. to the existence of a
problem. The description of the problem might not have been
all that could be desired, but knowing that a problem exists
is at least half the battle in finding the cure.
My GNU C compiler > gcc --version gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7) issues a more informative diagnostic message: malloc.c:8: warning: implicit declaration of function `malloc'
That's nice. Seriously, it *is* nice. So what?
The solution to this problem is to get a better C compiler and *not* to cobble your code to accommodate inferior C compilers.
The "inferior compilers" in the O.P.'s case were (ahem)
gcc and gcc. And I fail to see how leaving out an unnecessary
and potentially harmful cast constitutes "cobbling" the code. What benefit does *inserting* the cast provide? It allows you to compile your code with a C++ compiler. This is important because it allows your C code to survive even if the C language itself doesn't survive.
I once saw a cute little "Hello, world!" program that
ran correctly in three different languages: C, Fortran, and sh,
if I recall aright. It was an amusing stunt, but hardly a useful
way to write programs ...
I put it to you that writing programs that are somehow
simultaneously both C and C++ is an exercise of similar
[f]utility. Yes, it's possible to write such code, if one
is willing to wear a tight enough strait jacket. But the
result will be derided by both camps: Neither the C people
nor the C++ people will deem it good code. In the particular
case of malloc(), the C++ folks will say it's poor practice
to use it in the first place; the `new' operator needs no
cast. If you want to write C++ go right ahead and do so --
but don't pretend that writing bad C code so it can masquerade
as bad C++ code is a good idea.
Personally, I use `new' as an identifier whenever I can
find a reasonable excuse to do so.
-- Er*********@sun .com
Ben Pfaff wrote: Eric Sosman <Er*********@su n.com> writes:
"E. Robert Tisdale" wrote: 2. the code will probably execute properly even if malloc has not been declared properly.
False, as Dan's example demonstrated. False, false, false. What part of "false" are you having trouble with?
Why are you arguing with a troll?
Because he writes with an authoritative manner, and people
might therefore believe he's an authority. I have observed
that he oscillates between nonsense on some threads and good
advice on others; the latter tends to build credibility for
the former. (And makes it not entirely proper to dismiss
him as purely a troll; sometimes he *does* make sense.)
Somebody's got to contradict him when he spouts foolishness.
I'm not volunteering to be Permanent Latrine Orderly, so it's
kind of a coin-toss whether I'll join in on a thread or just
leave his flagellation to others -- there never seems to be a
shortage of people willing to point out misteaks, and in the
case of ERT I think that's a good thing.
I just wish I knew whether he does it out of forgivable
ignorance or out of malice.
-- Er*********@sun .com
Eric Sosman wrote: If you want to write C++ go right ahead and do so -- but don't pretend that writing bad C code so it can masquerade as bad C++ code is a good idea.
Amen...
Personally, I use `new' as an identifier whenever I can find a reasonable excuse to do so.
Yes, and this is not through anti-C++ bloodymindednes s. All right - it's not
/just/ through anti-C++ bloodymindednes s. There's a much more important
reason, too: it's a heads-up to anyone compiling the code with a C++
compiler; a message from the original author. When translated, the message
reads "I wrote this in C. I have assumed C rules throughout. C++ is
different from C. Proof: the diagnostic you are now reading! So either
rewrite this from the ground up in C++, or leave it alone".
Not bad for three letters. :-)
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Eric Sosman wrote: The "inferior compilers" in the O.P.'s case were (ahem) gcc and gcc.
No. Lionel Valéro simply forgot to use
the appropriate options (-Wall, etc.)
I put it to you that writing programs that are somehow simultaneously both C and C++ is an exercise of [f]utility. Yes, it's possible to write such code if one is willing to wear a tight enough strait jacket. But the result will be derided by both camps: Neither the C people nor the C++ people will deem it good code. In the particular case of malloc(), the C++ folks will say it's poor practice to use it in the first place; the `new' operator needs no cast. If you want to write C++ go right ahead and do so -- but don't pretend that writing bad C code so it can masquerade as bad C++ code is a good idea.
If I find a great open source function library implemented in C,
a I obliged to re-implement in C++ so that "C++ folks"
will "deem it good code"? I don't think so.
I just compile it with my C++ compiler
and link it into my C++ programs.
Personally, I use `new' as an identifier whenever I can find a reasonable excuse to do so.
Just to frustrate C++ programmers?
Is your code really more valuable
if C++ programmers can't use it?
Does your employer/client know that you do this?
E. Robert Tisdale wrote: Eric Sosman wrote:
If you want to write C++ go right ahead and do so -- but don't pretend that writing bad C code so it can masquerade as bad C++ code is a good idea. If I find a great open source function library implemented in C, a I obliged to re-implement in C++ so that "C++ folks" will "deem it good code"? I don't think so.
Right. You can just link it in, and everyone's happy.
I just compile it with my C++ compiler and link it into my C++ programs.
Right. Personally, I use `new' as an identifier whenever I can find a reasonable excuse to do so.
Just to frustrate C++ programmers?
No, not just for that. Partly to warn them "this is C code - compile as C,
then link".
Is your code really more valuable if C++ programmers can't use it?
But they can, as you yourself just pointed out!
Does your employer/client know that you do this?
I'm sure Eric Sosman's employer knows perfectly well that Eric is a
programmer to be trusted.
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
The Real OS/2 Guy wrote: On Mon, 1 Dec 2003 19:00:18 UTC, Lionel Valero <li***********@ polymtl.ca> wrote:
I forgto the headers : #include <stdio.h> #include <sys/types.h> #include <time.h>
You forgot to include stdlib.h again
Lionel Valero wrote:
ka = (int *) malloc(nka * sizeof(int));
And this cast hides the error that you forgot to include the header in redhat linux. Casting the result from a function that returns void* is always an error.
Well, I wouldn't go that far. Consider replacing the first occurrence
of 'a' with 'A" in a block of 100 bytes, given that it exists.
*(char*)memchr( buffer, 'a', sizeof buffer) = 'A';
seems like a reasonable solution.
In <3F************ **@jpl.nasa.gov > "E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes: Dan Pop wrote:
E. Robert Tisdale writes:
Dan Pop wrote:
E. Robert Tisdale writes:
>Barry Schwarz wrote: > >>On a machine where the method of returning a pointer >>differs from the method of returning an int >>(such as using a different register), > >Please tell us which machine does this.
The x86 in certain memory models where an int is 16 bits and a pointer is 32 bits.
Could you please code up an example and run it on the machine that you reference above so that we can see how it fails?
Yes, I can, but it shouldn't be necessary: only an idiot couldn't figure out how it fails.
H:\>type good.c #include <stdio.h> #include <stdlib.h>
int main(int argc, char* argv[]) { char *p = malloc(10); printf("%p\n", (void *)p); return 0; }
H:\>tcc -mh good.c Turbo C++ Version 1.01 Copyright (c) 1990 Borland International good.c: Turbo Link Version 3.01 Copyright (c) 1987, 1990 Borland International
Available memory 410832
H:\>good 0856:0004
H:\>type bad.c #include <stdio.h>
int main(int argc, char* argv[]) { char *p = (char *)malloc(10); printf("%p\n", (void *)p); return 0; }
H:\>tcc -mh bad.c Turbo C++ Version 1.01 Copyright (c) 1990 Borland International bad.c: Turbo Link Version 3.01 Copyright (c) 1987, 1990 Borland International
Available memory 418000
H:\>bad 0000:0004
The address printed by the bad program is right into the interrupt vector area of the processor, therefore it is clearly not the address returned by malloc. If the bad program actually attempted to write something into it, the whole system would get screwed.
Thanks Dan. I knew you could do it.
The only problem is that your example is for a C++ compiler (Turbo C++) which is obsolete, which never fully complied with the ANSI/ISO C89 standard much less the ANSI/ISO C99 standard and which was designed to emit code for Intel 80286 processors.
Wrong. The code is for a C compiler that does conform with the
ANSI/ISO C89 standard. The lack of a diagnostic for good.c is the
ultimate proof that the compiler was used as a C compiler and not as a
C++ compiler.
Nothing in your initial request outruled Intel 80286 processors, or
conforming C89 implementations , therefore my example is an adequate
response to your request.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Richard Heathfield wrote: E. Robert Tisdale wrote:
.... snip ... Does your employer/client know that you do this?
I'm sure Eric Sosman's employer knows perfectly well that Eric is a programmer to be trusted.
A more germane question is "Does ERTs employer know what he does
here?". If his job is pushing a broom it probably doesn't matter.
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jacob nikom |
last post by:
Hi,
We ran MySQL (4.0.15-standard, 64-bit, SuSe 8.0) benchmarks
on 2 CPU and 4 CPU Opteron machines. Mostly we tested insertion
times with many simultaneous connections (hundreds). To our
surprise we did not find significant differences.
Each of our servers (2 and 4 CPU machine) has one 1Gbit network
connection. I have feelings that the machines are the network
bounded, not the CPU bounded. Could it be true? How to verify it?
|
by: Plymouth Acclaim |
last post by:
Hi guys,
We have a problem with Dual AMD64 Opteron/MySQL 4.0.18/Mandrake 10
for a very high volume site. We are evaluating the performance on our
new server AMD64 and it seems it's slow compared to Dual Xeon/MySQL
4.0.15/RedHat8 and Dual Xeon/MySQL 4.0.18/Mandrake 10.
And it seems there are zombie threads. 570 threads in 1 hour and we
didn't even use JDBC connection pooling at all. These threads are
supposed to be gone within 60...
|
by: Rajrup Banerjee |
last post by:
Hi,
Does anybody know what are the changes to be done
to compile C++ code which uses stl from SGI STL library,
using g++ 3.2.3, on Linux running on AMD Opteron 64 bit?
Regards,
Rajrup.
|
by: louis_pastorik |
last post by:
Does anyone have any information about 64 bit python support for Xeon
and Opteron architectures on Windows platforms? If anyone has built the
python runtime on either of these platforms before I would be really
interested. Thanks.
|
by: Ericson Smith |
last post by:
Hi,
We're thinking of moving to a 64 bit platform for our database. Does
anyone have any stories of migration from 32 bit to 64 bits?
* How did it impact performance?
* Were there any gochas?
* What processors were used (any AMD Opteron's so far?)
Also what kind of performance increase could we expect if we moved from
a dual 32 bit to a quad 32 bit platform. (We are already using speedy
| |
by: J.Haan |
last post by:
Hi all,
at the company where I work, we're planning on installing DB2 on an AMD
Opteron with 32GB of memory.
Let's say we're running DB2 8.1 FP 7 in 64bit mode on SuSE Linux
Enterprise Server 9.0. Can we create bufferpools with a size over
4GB? Let's say 12GB?
Has anyone got any experience running such a configuration?
|
by: ashokrathi |
last post by:
I am trying to compile SQL procedures on Linux running on AMD/Opteron
64-bit machine. I have 'gcc', 'cc' and 'g++' compilers on my machine. I
have set DB2_SQLROUTINE_COMPILE_COMMAND with various options, such as,
db2set DB2_SQLROUTINE_COMPILE_COMMAND="gcc -fPIC -$HOME/sqllib/include
SQLROUTINE_FILENAME.c -shared -o SQLROUTINE_FILENAME -L$HOME/sqllib/lib
-ldb2"
I have tried with/without "-fPIC" option. Without "-fPIC", I get
following...
|
by: Rocael Hernandez |
last post by:
Hello all,
I was wondering what's best for PG, since we have to
decide what will be the new server for our DB, that
will serve one or more websites.
Our actual configuration is a dual xeon 2.8ghz with
4Gb ram + scsi disks, and works fine, though some
intensive insert/update processes has taken it to 100%
cpu use.
|
by: LLessieux |
last post by:
Hi,
I have been seeing a strange behaviour when doing some tests on a Dual -
Dual Core Opteron system using Windows XP (32bits with SP2).
In out application we have a wrapper around the thread pool to handle the
work on renderers and CPU intensive tasks. On the Dual - Dual Core Opteron,
the application is correctly starting 4 threads but it seems to be running
only 2 concurrently.
|
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: 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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: 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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |