Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > AMD opteron 64

Reply
Thread Tools

AMD opteron 64

 
 
Ben Pfaff
Guest
Posts: n/a
 
      12-01-2003
Lionel Valero <> writes:

> 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) * 12; /* Don't do this! */

Instead, write it this way:

int *x = malloc (sizeof *x * 12;

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.

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
 
 
 
Lionel Valero
Guest
Posts: n/a
 
      12-01-2003
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=-

 
Reply With Quote
 
 
 
 
Randy Howard
Guest
Posts: n/a
 
      12-01-2003
In article <nELyb.74495$>,
says...
> 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.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet:
 
Reply With Quote
 
SenderX
Guest
Posts: n/a
 
      12-01-2003
> ka = (int *) malloc(nka * sizeof(int));

No cast.

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

--
The designer of the experimental, SMP and HyperThread friendly, AppCore
library.

http://AppCore.home.comcast.net


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      12-01-2003
On Mon, 01 Dec 2003 18:21:07 GMT, Lionel Valero <>
wrote:

>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')
 
Reply With Quote
 
Lionel Valero
Guest
Posts: n/a
 
      12-01-2003
I forgto the headers :
#include <stdio.h>
#include <sys/types.h>
#include <time.h>


Lionel Valero wrote:

> 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=-
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      12-01-2003
In article <6dMyb.74810$>,
says...
> 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.

What is necessary: <stdlib.h>, as has been pointed out already in this
thread.

>
>
> Lionel Valero wrote:
>
> > 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.

>
>


--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet:
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      12-01-2003
Lionel Valero wrote:

> 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)!
<ERROR>: 2147483647 int words required

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

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      12-01-2003
Ben Pfaff wrote:

> 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) * 12; /* Don't do this! */
>
> Instead, write it this way:
>
> int *p = malloc (sizeof *p * 12;
>
> 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));

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



 
Reply With Quote
 
The Real OS/2 Guy
Guest
Posts: n/a
 
      12-01-2003
On Mon, 1 Dec 2003 19:00:18 UTC, Lionel Valero
<> 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.

--
Tschau/Bye
Herbert

To buy eComStation 1.1 in germany visit http://www.pc-rosenau.de

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AMD Opteron 144 Gadge Addict Computer Information 0 10-11-2005 12:43 PM
32-bit JDK 1.4.2 on Solaris 10 x64 AMD Opteron js Java 0 09-22-2005 10:57 PM
MAX JVM on AMD Opteron / Linux system in 32bit mode Dan Java 2 07-28-2004 01:30 PM
Amd Opteron 64 bit Ged Computer Support 6 10-10-2003 08:12 PM
your 2 cent on amd-opteron 64 eck eck Computer Support 3 06-25-2003 03:01 PM



Advertisments