#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Line 2287: warning: improper pointer/integer combi


    Hi Guys

    I have a memory allocation error within some legacy code. the compiler is throwing up the following error

    UX:i386acomp: WARNING: "f_qpol.c", line 2287: warning: improper pointer/integer combination: op "="
    for the code below

    Code:
    void ProcessSectEndmt( fh)
     FH *fh;
    {
            unsigned short iRelSects[ DRIVERS];
            char cSections[ 170];
    
            cSections[ 0] = NULL;
    
            GetPolSectRef( fh, iRelSects);
            GetPolBreakdown( fh, ENDORSEMENT___RESULT, FALSE, cSections);
    
    LINE 2287:Sections.PolSects = malloc( strlen( cSections) + 1);
    
            if( Sections.PolSects == NULL)
            {
                    PolClose( fh, SCH_CLOSE);
                    err( _ET_FATAL, _ER_NOMEM, NULL);
            }
            strcpy( Sections.PolSects, cSections);
    
            return;
    }
    This is the struct that is being passed:

    Code:
    struct sect {
            char            *PolRefNo;
            char            NumStyle;
            char            *PolSects;
            unsigned long   Sects[ PREMIUMS];
            unsigned short  XsSects[ MAX_SECTS];
    };
    Can anyone suggest what might be causing this?
    If you need anymore information just let me know.

    Thanks
    Jim
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Yup, judging by the function signature, that is really some legacy code.

    The error implies you are compiling C code with a C++ compiler.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    As I had advised, you should identify the legacy code as being K&R and that you are compiling it with a legacy K&R compiler. That would eliminate any confusion on our part concerning the compiler and its capabilities.

    I have to admit I'm a bit hazy on when it's necessary to cast the return value of malloc -- ie, I can never remember which versions of C/C++ require it and which don't. I am even less certain how it was in K&R times, since I started with ANSI C and never had to deal with K&R except to convert old code to ANSI.

    Try this:
    Code:
            Sections.PolSects = (char*)malloc( strlen( cSections) + 1);
    That should eliminate the warning. However, that could also mask a problem if K&R doesn't require such casting. Are there other invocations of malloc in the project and how are those handled?

    Also, others reading this thread should comment if my advice is not correct.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    Some rules of malloc.

    If this were compiled with a C++ compiler, then there are only two possible error messages (without a cast).
    1. foo.cpp:5:22: error: ‘malloc’ was not declared in this scope
    2. foo.cpp:6:22: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
    - You can't try to call a function without a prototype.
    - C++ does not support void* to T* conversions without an explicit cast.

    If this were compiled with a C compiler, then things are different.
    The first is that calling a function without a prototype results in an implicit declaration (and a subsequent error message).
    Specifically, the compiler will assume you meant
    int malloc();
    And then immediately complain about int to pointer conversions.
    Now if pointers and ints have different sizes (or are returned in different registers), then ignoring this warning will royally screw things up. Casting won't help, as you need to make sure that the compiler knows that malloc returns a pointer.

    For ancient pre-ANSI compilers (before void* was invented), malloc typically returned a char*. In some really old code, a cast would have been necessary. But in this case, the pointer is a char* anyway, so any flavour of malloc (so long as it was declared) would have been sufficient.

    For my money, you failed to include stdlib.h, and casting the result to make the compiler STFU is the wrong way to go. Include the proper header file and move on.
    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
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Smells like the function prototype for malloc() is missing. #include <stdlib.h> ought to fix it :).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo