P
Pegboy
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 */
for( i = 0; i < num_entries; i++ )
{
/* load entries here */
}
}
return ifd;
}
/********************************************************/
Again, thank you.
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 */
for( i = 0; i < num_entries; i++ )
{
/* load entries here */
}
}
return ifd;
}
/********************************************************/
Again, thank you.