Aus irgendeinem Grund valgrind
hält den folgenden Fehler zu werfen:C- gethostbyaddr & Valgrind
==6263== Invalid read of size 4
==6263== at 0x40151B9: (within /lib/ld-2.7.so)
==6263== by 0x4005C29: (within /lib/ld-2.7.so)
==6263== by 0x4007A47: (within /lib/ld-2.7.so)
==6263== by 0x40114F3: (within /lib/ld-2.7.so)
==6263== by 0x400D585: (within /lib/ld-2.7.so)
==6263== by 0x4010F0D: (within /lib/ld-2.7.so)
==6263== by 0x4141391: (within /lib/tls/i686/cmov/libc-2.7.so)
==6263== by 0x400D585: (within /lib/ld-2.7.so)
==6263== by 0x4141554: __libc_dlopen_mode (in /lib/tls/i686/cmov/libc-2.7.so)
==6263== by 0x411B286: __nss_lookup_function (in /lib/tls/i686/cmov/libc-2.7.so)
==6263== by 0x411B39F: (within /lib/tls/i686/cmov/libc-2.7.so)
==6263== by 0x411CFC5: __nss_hosts_lookup (in /lib/tls/i686/cmov/libc-2.7.so)
==6263== Address 0x4183d24 is 36 bytes inside a block of size 37 alloc'd
==6263== at 0x4022AB8: malloc (vg_replace_malloc.c:207)
Hier ist die Art, wie ich es mache. Irgendwelche Gründe, warum das passiert? Dank
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
char *IPtoHostname(const char *ipaddress)
{
struct hostent *host;
unsigned int ip = 0;
ip = inet_addr (ipaddress);
host = gethostbyaddr ((char *) &ip, sizeof (unsigned int), AF_INET);
if (host == NULL) return NULL;
return strdup(host->h_name);
}
int main() {
const char *ip = "65.55.4.170";
char *a = NULL;
a = IPtoHostname(ip);
printf ("%s\n", a);
free (a);
return 0;
}
aktualisieren: Es passiert, wenn ich es unter Linux hardy 2.6.24-16-generic
laufen Es geschieht nicht unter Ubuntu 9.10
vg_replace_malloc existiert nur innerhalb valgrind – thkala
Sie Ihr Programm mit -O0 -g kompiliert haben, nicht wahr? – thkala
@thkala: Ich kompiliert mit: 'gcc -Wall -O0 -g prog.c' – RichardThomson