Thread: Compiler gives warnings I don't understand

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    36

    Compiler gives warnings I don't understand

    My current problem is about the compiler giving warnings that don't make sense to me, as in I don't understand why they're happening.

    "initialization makes pointer from integer without a cast"

    The variable in question is an unsigned char*. It is initialised by a function which returns an unsigned char*. I honsetly can't find the problem there. I'm returning a pointer, not an integer. I made casted the return value as (unsigned char*) so I could shut it up, but now that I'm getting unexplained segmentation faults out of nowhere I think it's time to find out why it's happening...I have multiple functions which are doing this.

    EDIT: Just realised I posted in the windows forum :/...Could someone please move it to windows forum since there's no delete?
    Last edited by Gerread; 11-24-2007 at 02:52 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what is the code?

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    36
    Declared:
    Code:
    unsigned char* mem = function();
    Proto:
    Code:
    unsigned char* function(void);
    Relevant function part:
    Code:
    //memory to be returned
    	unsigned char* retmem = malloc(size);
    
    ...
    
    	//return memory
    	return retmem;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's probably complaining about the malloc call.
    Did you include stdlib.h ?

    Because without it, malloc is implicitly declared as returning an int.
    Hence the "makes pointer from integer" part of the message.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    36
    That stopped it from complaining. Thanks for the insight. I suppose that means wtih including stdlib, I can just cast it as whatever. It didn't fix the crashing (ofcourse) but it's one less thing to suspect. I guess I'll have to rewrite and pray I don't do whatever I did to screw it up.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I can just cast it as whatever.
    NO!
    casting malloc would merely mean that you no longer get the warning at all. It's not the same as fixing the real problem (not including the header file).

    Also, are you using C or C++ (don't be vague and say C/C++).
    If it's C++, then you really should be using new/delete and not malloc/free.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    36
    I am using C here. I'll take your advice and remove the casts in favour of stdlib.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Compiler Warnings
    By Elysia in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2007, 12:44 PM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Some Error messages I don't understand...
    By Shogun in forum C Programming
    Replies: 3
    Last Post: 08-05-2004, 03:35 PM
  5. compiler warnings
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 12:06 PM