AMD opteron 64

B

Ben Pfaff

Lionel Valero said:
main (argc, argv)
int argc;
char *argv[];
{
int *ka;
int nka;

/* allocation dynamique entiere */
ka = (int *) malloc(nka * sizeof(int));

I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

In fact, the second problem is your problem here. Fix it.

When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don't* write this:

int *x = malloc (sizeof (int) * 128); /* Don't do this! */

Instead, write it this way:

int *x = malloc (sizeof *x * 128);

There's a few reasons to do it this way:

* If you ever change the type that `x' points to, it's not
necessary to change the malloc() call as well.

This is more of a problem in a large program, but it's still
convenient in a small one.

* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
 
L

Lionel Valero

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=-
 
R

Randy Howard

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 ?

At a quick glance, you seem to have left out a proper header for malloc.
The compiler doesn't automagically know what "malloc" means without one.
Also, the cast is not required in C, and helps to hide this omission from
you, thereby making it harder to diagnose.
 
S

SenderX

ka = (int *) malloc(nka * sizeof(int));

No cast.

Also, sizeof( void* ) might not equal sizeof( int* ).
 
L

Lew Pitcher

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 ?

Yes. You neglected to
#include <stdlib.h>
which defines the malloc() function's return value.

Since you did not do this, the compiler assumed that malloc() returned the
default type (int), and saw that you forced a conversion from (int) to (int *)
in your code. The compiler issued the warning based on this.

The fix is to
a) include <stdlib.h> in your code, and
b) remove the unnecessary case of malloc()'s return value.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
 
L

Lionel Valero

I forgto the headers :
#include <stdio.h>
#include <sys/types.h>
#include <time.h>


Lionel said:
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=-
 
R

Randy Howard

I forgto the headers :
#include <stdio.h>
#include <sys/types.h>
#include <time.h>

<sys/types.h> does not seem to be necessary for what you have below
and you will find out that it isn't portable either.

<time.h> also does not seem to be necessary, but at least is standard.

as has been pointed out already in this said:
Lionel said:
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.
 
E

E. Robert Tisdale

Lionel said:
I have a test program
that is compiled fine on a 32 bits redhat linux using gcc :
***************
> cat malloc.c
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char* argv[]) {
int nka = atoi(argv[1]);

/* allocation dynamique entiere */
int* ka = (int*)malloc(nka*sizeof(int));
if (NULL == ka) {
fprintf(stderr, "<ERROR>: Out of free storage (malloc)!\n");
fprintf(stderr, "<ERROR>: %d int words required\n", nka);
exit (EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o malloc malloc.c
> ./malloc 13
> ./malloc 999999999999
<ERROR>: Out of free storage (malloc)!
said:
***********************

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?

You need to include stdlib.h which declares malloc.
 
E

E. Robert Tisdale

Ben said:
I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

But an ANSI/ISO C++ compiler will complain.
* Casting its return value can mask a failure to
#include <stdlib.h>, which leads to undefined behavior.

No! It may mask the fact that malloc was not declared
but a good C compiler will warn you about that.
* If you cast to the wrong type by accident,
odd failures can result.

Can you show us an example?
In fact, the second problem is your problem here. Fix it.

When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don't* write this:

int *p = malloc (sizeof (int) * 128); /* Don't do this! */

Instead, write it this way:

int *p = malloc (sizeof *p * 128);

There's a few reasons to do it this way:

* If you ever change the type that `p' points to, it's not
necessary to change the malloc() call as well.

A better solution would be:

typedef int T;
T* p = (T*)malloc(128*sizeof(T));
 
T

The Real OS/2 Guy

I forgto the headers :
#include <stdio.h>
#include <sys/types.h>
#include <time.h>

You forgot to include stdlib.h again
Lionel Valero wrote:

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.
 
B

Ben Pfaff

E. Robert Tisdale said:
But an ANSI/ISO C++ compiler will complain.

Fortunately, we are not discussing C++. malloc() should not
normally be used in C++.
No! It may mask the fact that malloc was not declared
but a good C compiler will warn you about that.

This is in fact the OP's problem, so I don't see how you can
discount it.
Can you show us an example?

Casting to (char) instead of (char *) is bound to be a problem.
A better solution would be:

typedef int T;
T* p = (T*)malloc(128*sizeof(T));

I don't see how that's a better solution at all.
 
M

Mike Wahler

E. Robert Tisdale said:
But an ANSI/ISO C++ compiler will complain.

So what? This is comp.lang.c.

No! It may mask the fact that malloc was not declared
but a good C compiler will warn you about that.

It might, but it's not required to. I'd rather rely upon
accurate knowledge than depend upon a convenience feature
of a tool, which might or might not exist.
Can you show us an example?

The OP's original message is a perfect example.
A better solution would be:

typedef int T;
T* p = (T*)malloc(128*sizeof(T));

Casting the return value from malloc() is never 'better'.
And your typdef only obfuscates things.


-Mike
 
M

Mark McIntyre

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));

Everyone else has explained your immediate problem.forgetting

HoweverI notice you are using nka to size your array. But you didn't
initialise it to any value. So it contains garbage, and may crash your
program.
 
J

Joona I Palaste

A COBOL compiler will complain even louder. So what?

ERT seems to have a fascination with compiling C code with a C++
compiler these days. Can you say "one-trick pony", ERT?
 
J

Jack Klein

Still playing the fool I see, Tisdale.
cat malloc.c
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char* argv[]) {
int nka = atoi(argv[1]);

What happens when you run it without command line arguments and
dereference the null pointer? Can you say "segfault"?
/* allocation dynamique entiere */
int* ka = (int*)malloc(nka*sizeof(int));

About one in 20 times you actually post something of value. Sadly,
this is not one of those times...

int *ka = malloc(nka * sizeof *ka);

Note the pleasing symmetry of the dual asterisks.
if (NULL == ka) {
fprintf(stderr, "<ERROR>: Out of free storage (malloc)!\n");
fprintf(stderr, "<ERROR>: %d int words required\n", nka);
exit (EXIT_FAILURE);
}
return EXIT_SUCCESS;
}

<ERROR>: Out of free storage (malloc)!
<ERROR>: 2147483647 int words required

Why didn't you show the results of running:

./malloc

???

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
C

CBFalconer

*** topposting fixed ***

Lionel said:
Lionel said:
I have a test program that is compiled fine on a 32 bits redhat linux
using gcc :
***********************
main (argc, argv)
int argc;
char *argv[];

Do not use old fashioned function declarations, they have been
obsolete for at least 15 years. main returns int, say so.

Do not cast malloc results. nka is uninitialized. Use sizeof
*ka.

-1 is not a portable value here. EXIT_FAILURE and

Increase the gcc warning levels. -W -Wall -ansi -pedantic -O1 are
the recommended minimum.
I forgto the headers :
#include <stdio.h>
#include <sys/types.h>

This is not a valid ISO C header. Omit it.
#include <time.h>

This is not used. omit it.

Please Do Not Toppost.
 
K

Keith Thompson

E. Robert Tisdale said:
But an ANSI/ISO C++ compiler will complain.

So don't use a C++ compiler on C source (or a Fortran compiler, or an
Intercal compiler). There's no good reason C source code should be
constrained to be valid C++.

<OT>
Well-written C++ code doesn't use malloc() anyway; it uses the "new"
operator.
</OT>
 
B

Barry Schwarz

But an ANSI/ISO C++ compiler will complain.


No! It may mask the fact that malloc was not declared
but a good C compiler will warn you about that.

On a machine where the method of returning a pointer differs from the
method of returning an int (such as using a different register), the
behavior is indeed undefined. malloc will return the pointer using
the correct procedure. The compiler will generate code to convert the
assumed returned int. Since malloc did not return an int, this code
will operate on residual data.


<<Remove the del for email>>
 
R

Richard Bos

Which proves once again that C++ is broken, since the cast is useless
and useless casts should be anathema.
ERT seems to have a fascination with compiling C code with a C++
compiler these days.

That's because ERT has, and AFAICT has always had, a bee in his bonnet
about C++ being "the new C". Not even Bjarne Stroustrup has that
delusion, but some people are impossible to disconfuse.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top