Re: Trouble with malloc().

From: Kevin Goodsell (usenet1.spamfree.fusion_at_neverbox.com)
Date: 11/23/03


Date: Sun, 23 Nov 2003 22:23:50 GMT

Pegboy wrote:
> I am having trouble with malloc() again for a PC app I am developing. The
> method of the suspicious line of code seems to be Ok on a embedded platform,
> but not with the PC platform. The embedded platform uses a different
> compiler. I feel like I'm overlooking a very simple problem, but I can't
> see it. I would appreciate any help. Thank you.
>
> I am trying to allocate memory for a structure of type NAT_S which contains
> a pointer to a structure of type NAT_ENTRY_S (see code below).
>
> The compiler reports warning 'Suspicious pointer conversion' as indicated
> below in the code. This is the only warning or error I get.
>
> This code appears to work but the next call to malloc() following this, the
> app crashes. In some instances, ReadNat() is not called and the app works
> Ok. So the problem points to within ReadNat(), which is where the warning
> is.
>
> I examined/watched the address given to 'nat' and 'nat->entries' and they
> are different by 24 bytes when they should be different by 6. The sizeof(
> NAT_S ) is 6.
>
> /************************************************************/
> typedef struct
> {
> short a;
> short b;
> long c;
> long d;
> } NAT_ENTRY_S;
>
> typedef struct
> {
> short num_entries;
> NAT_ENTRY_S *entries;
> } NAT_S;
>
> NAT_S *ReadNat( FILE *file, long offset )
> {
> NAT_S *nat;
> short i, num_entries;
>
> fseek( file, offset, SEEK_SET );
> num_entries = ReadShort( file );
>
> if( (nat = malloc( sizeof( NAT_S ) + (num_entries * sizeof(
> NAT_ENTRY_S )) )) != NULL )
> {
> nat->num_entries = num_entries;
> nat->entries = nat + sizeof( NAT_S ); /* COMPILER WARNING:
> Suspicious pointer conversion */

I'll say it's suspicious. It's also most likely the cause of your
problem. What makes you think that nat + sizeof(NAT_S) yields a memory
address that will be properly aligned for a NAT_ENTRY_S? You cannot
simply place an object anywhere in memory that is convenient. Some
objects in some implementations must be aligned on certain boundaries
(e.g., a 4-byte integer might be required to start on an address that is
divisible by 4).

malloc() is required to return a pointer to memory that is properly
aligned for any type, but that address plus some arbitrary value is NOT
required to yield a pointer to memory that is properly aligned for some
random type.

-Kevin

-- 
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Relevant Pages

  • Re: sizeof(ptr) = ?
    ... The value returned by malloc() is of type 'void*', ... The memory is typeless until an object has been written ... Since 'void' is defined to be an incomplete ... an lvalue of a complete type, there must be a pointer conversion ...
    (comp.lang.c)
  • Re: Trouble with malloc().
    ... >> I am having trouble with malloc() again for a PC app I am developing. ... This is the only warning or error I get. ... not-necessarily-compatible pointer type. ...
    (comp.lang.c)
  • Re: Smart Pointers: Is there something similar to smart pointers in C?
    ... Worse yet, as soon as you ask for more than 24-bits of memory, the high ... The app may keep the pointer, so the GC won't toss out the block, ... the app need NOT keep a pointer to this request ...
    (comp.lang.c)
  • Re: Checking validity of a file pointer
    ... because the pointer looks valid. ... detected (preferably by having fclose return an error indication ... if the buffer of the file is returned from the malloc() function, ... with memory returned from mallocfunction in that buff ...
    (comp.lang.c)
  • Re: memory allocation wrapper
    ... I've written a wrapper for malloc and friends. ... The reason for doing writing this so that newbies ... How do I know how much memory a pointer points to? ...
    (comp.lang.c)