2

I am testing example code as following, why I got warning during compile by gcc 5.6 on linux ubuntu 16-4 ?

~/c$ gcc malloc.c 
malloc.c: In function ‘main’:
malloc.c:17:14: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     if(number= malloc(50*sizeof(int) )== NULL)

This is my code :

#include "stdlib.h"
#include "stdio.h"

int main()
{
    char* str;
    int * number;
    if((str= (char *)malloc(100) )== NULL)
    {
        printf("malloc fail \n");
        exit(1);
    }

    printf ("sting was allocaed \n");
    if(number= malloc(50*sizeof(int) )== NULL)
    {
        printf("malloc fail \n");
        exit(1);
    }

    printf ("int was allocaed \n");
    return 0;
}
mch
  • 9,424
  • 2
  • 28
  • 42
camto
  • 21
  • 4
  • 4
    check precedence: `if((number= malloc(50*sizeof(int)) )== NULL)` is probably what you want – Jean-François Fabre Oct 09 '17 at 13:17
  • I recommend you check e.g. [this operator precedence table](http://en.cppreference.com/w/c/language/operator_precedence). You do it correctly for the first allocation, but wrong for the second, how come? – Some programmer dude Oct 09 '17 at 13:18
  • Thanks Jean, After add () it works but still // if((number= (int*) malloc(50*sizeof(int) ))== NULL) if((number= malloc(50*sizeof(int) ))== NULL) { just I don't quite understand why both line above works, because the 2nd line does not have cost as well ? – camto Oct 09 '17 at 13:24
  • Assuming you mean "cast" when you say "cost", please read [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Some programmer dude Oct 09 '17 at 13:27
  • "sting was allocaed". Will he sing as well ? :) – Jean-François Fabre Oct 09 '17 at 13:48
  • the code would be a lot easier to read if you separated out the assignment and check to see if it anything was assigned into multiple statements. – Chris Turner Oct 09 '17 at 14:49

1 Answers1

3

here

number= malloc(50*sizeof(int) )== NULL 

you're assigning to number the result of the comparison from the return of malloc and NULL because == has higher precedence than =.

Fortunately, the compiler catches that because number is a pointer.

You need to do:

(number = malloc(50*sizeof(int)) )== NULL

Note: when you have a doubt, insert some parentheses. It doesn't cost a dime.

Also, you were lucky that the compiler caught this with default warning level. In the future, always compile with all warnings enabled -Wall and maybe add -Wextra -pedantic.

Note that your first allocation was (almost) okay:

if((str= (char *)malloc(100) )== NULL)

except that [you shouldn't cast the output of malloc in C][1] so:

if((str= malloc(100) )== NULL)

is even better (yes, no need to multiply by sizeof(char) which is always 1)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • `if((str= malloc(sizeof *str * 100)) == NULL)` is an alternative to insure the correct size allocation - even if `str` later changes pointer type. – chux - Reinstate Monica Oct 09 '17 at 13:33
  • 1
    Thanks a lot ! Now I understand better that : In C, you don't need to cast the return value of malloc. The pointer to void returned by malloc is automagically converted to the correct type. However, if you want your code to compile with a C++ compiler, a cast is needed. A preferred alternative among the community is to use the following – camto Oct 09 '17 at 13:41
  • gcc -Wall -Wextra -pedantic malloc.c malloc.c: In function ‘main’: malloc.c:19:14: warning: assignment makes pointer from integer without a cast [-Wint-conversion] if(number= malloc(50*sizeof(int) )== NULL) ^ malloc.c:19:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses] if(number= malloc(50*sizeof(int) )== NULL) ^ – camto Oct 09 '17 at 14:17