Unix Programming - inet_ntop doesn't work with malloc'ed chars*

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > inet_ntop doesn't work with malloc'ed chars*





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author inet_ntop doesn't work with malloc'ed chars*
pmatos

2005-08-03, 6:00 pm

Hi all,
I've tried this:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(void) {

struct hostent *hptr;
char *ipaddr;

hptr = gethostbyname("mega.ist.utl.pt");

switch(hptr->h_addrtype) {
case AF_INET:
#ifdef AF_INET6
case AF_INET6:
#endif /* AF_INET6 */

ipaddr = hptr->h_addr_list[0];

char * str = malloc(INET6_ADDRSTRLEN * sizeof(*str));

inet_ntop(hptr->h_addrtype, ipaddr, str, sizeof(str));

printf("addr: %s\n", str);
break;
default:
exit(1);
break;
}

return 0;

}

This won't work as it is. str is empty. However if I change:
char * str = malloc(INET6_ADDRSTRLEN * sizeof(*str));
to:
char str[INET6_ADDRSTRLEN];

it works... why?

Can somebody help?

Cheers,

Paulo Matos

shakahshakah@gmail.com

2005-08-03, 6:00 pm

pmatos wrote:
> Hi all,
> I've tried this:
> #include <stdio.h>
> #include <stdlib.h>
> #include <assert.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netdb.h>
> #include <arpa/inet.h>
>
> int main(void) {
>
> struct hostent *hptr;
> char *ipaddr;
>
> hptr = gethostbyname("mega.ist.utl.pt");
>
> switch(hptr->h_addrtype) {
> case AF_INET:
> #ifdef AF_INET6
> case AF_INET6:
> #endif /* AF_INET6 */
>
> ipaddr = hptr->h_addr_list[0];
>
> char * str = malloc(INET6_ADDRSTRLEN * sizeof(*str));
>
> inet_ntop(hptr->h_addrtype, ipaddr, str, sizeof(str));
>
> printf("addr: %s\n", str);
> break;
> default:
> exit(1);
> break;
> }
>
> return 0;
>
> }
>
> This won't work as it is. str is empty. However if I change:
> char * str = malloc(INET6_ADDRSTRLEN * sizeof(*str));
> to:
> char str[INET6_ADDRSTRLEN];
>
> it works... why?
>
> Can somebody help?
>
> Cheers,
>
> Paulo Matos


Is the problem with the following line?
inet_ntop(hptr->h_addrtype, ipaddr, str, sizeof(str));

In the malloc'ed case sizeof(str) evaluates to 4 or 8, in the static
array case it is the size of the array (i.e. INET6_ADDRSTRLEN). Change
the malloc'ed case to:
inet_ntop(hptr->h_addrtype, ipaddr, str, INET6_ADDRSTRLEN);
-- or, depending on what's conceptually correct in this context --
inet_ntop(hptr->h_addrtype, ipaddr, str, INET6_ADDRSTRLEN *
sizeof(char));


and you should be good-to-go.

Fletcher Glenn

2005-08-03, 6:00 pm

pmatos wrote:
> Hi all,
> I've tried this:
> #include <stdio.h>
> #include <stdlib.h>
> #include <assert.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netdb.h>
> #include <arpa/inet.h>
>
> int main(void) {
>
> struct hostent *hptr;
> char *ipaddr;
>
> hptr = gethostbyname("mega.ist.utl.pt");
>
> switch(hptr->h_addrtype) {
> case AF_INET:
> #ifdef AF_INET6
> case AF_INET6:
> #endif /* AF_INET6 */
>
> ipaddr = hptr->h_addr_list[0];
>
> char * str = malloc(INET6_ADDRSTRLEN * sizeof(*str));
>
> inet_ntop(hptr->h_addrtype, ipaddr, str, sizeof(str));
>
> printf("addr: %s\n", str);
> break;
> default:
> exit(1);
> break;
> }
>
> return 0;
>
> }
>
> This won't work as it is. str is empty. However if I change:
> char * str = malloc(INET6_ADDRSTRLEN * sizeof(*str));
> to:
> char str[INET6_ADDRSTRLEN];
>
> it works... why?
>
> Can somebody help?
>
> Cheers,
>
> Paulo Matos
>


"sizeof" is a compile time directive. Since str does not have
a size at compile time, you cannot use the sizeof directive
for something that is defined at run time.

--

Fletcher Glenn

shakahshakah@gmail.com

2005-08-03, 6:00 pm

Fletcher Glenn wrote:
>
> "sizeof" is a compile time directive. Since str does not have
> a size at compile time, you cannot use the sizeof directive
> for something that is defined at run time.
>


For accuracy's sake, str does have a size at compile time, it's just
equivalent to sizeof(char *) in the OP's "malloc case".

pmatos

2005-08-03, 6:00 pm

Indeed, you guys are right. Thanks a lot for the help.

shakahshakah@gmail.com wrote:
> Fletcher Glenn wrote:
>
> For accuracy's sake, str does have a size at compile time, it's just
> equivalent to sizeof(char *) in the OP's "malloc case".


Casper H.S. Dik

2005-08-03, 6:00 pm

"shakahshakah@gmail.com" <shakahshakah@gmail.com> writes:

>In the malloc'ed case sizeof(str) evaluates to 4 or 8, in the static
>array case it is the size of the array (i.e. INET6_ADDRSTRLEN). Change
>the malloc'ed case to:
> inet_ntop(hptr->h_addrtype, ipaddr, str, INET6_ADDRSTRLEN);
> -- or, depending on what's conceptually correct in this context --
> inet_ntop(hptr->h_addrtype, ipaddr, str, INET6_ADDRSTRLEN *
>sizeof(char));


sizeof (char) is always 1 so why use it?

Casper
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com