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

    Join Date
    Nov 2013
    Rep Power

    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

    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);
    This is the struct that is being passed:

    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.

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

    Join Date
    Dec 2003
    Rep Power
    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
    Rep Power
    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:
            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
    Rep Power
    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
    Woodland Hills, Los Angeles County, California, USA
    Rep Power
    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