Frage

Mein Programm ist wie folgt ( main.c ):

#include <stdlib.h>
#include <stdio.h>
void main(){
  char *first="hello ";
  char *second="world!";
  char *seq=(char *)malloc((strlen(first)+1)*sizeof(char));
  strcat(strcpy(seq,first),second);
  printf("%s\n",seq);
  free(seq);
}

und ich debuggen mit dem Werkzeug valgrind, hieß es, dass ($: valgrind --tool = memcheck --leak-check = full --track-origins = yes ./main):

==5118== Memcheck, a memory error detector.
==5118== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==5118== Using LibVEX rev 1884, a library for dynamic binary translation.
==5118== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==5118== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==5118== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==5118== For more details, rerun with: -v
==5118== 
==5118== Invalid write of size 1
==5118==    at 0x402575B: strcat (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x80484EB: main (main.c:7)
==5118==  Address 0x418a02f is 0 bytes after a block of size 7 alloc'd
==5118==    at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x80484C3: main (main.c:6)
==5118== 
==5118== Invalid write of size 1
==5118==    at 0x4025777: strcat (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x80484EB: main (main.c:7)
==5118==  Address 0x418a034 is 5 bytes after a block of size 7 alloc'd
==5118==    at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x80484C3: main (main.c:6)
==5118== 
==5118== Invalid read of size 1
==5118==    at 0x4025963: strlen (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x40A0FA4: puts (in /lib/libc-2.10.1.so)
==5118==    by 0x80484F7: main (main.c:8)
==5118==  Address 0x418a02f is 0 bytes after a block of size 7 alloc'd
==5118==    at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x80484C3: main (main.c:6)
==5118== 
==5118== Invalid read of size 1
==5118==    at 0x40ACEFE: _IO_default_xsputn (in /lib/libc-2.10.1.so)
==5118==    by 0x40AA3D0: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.10.1.so)
==5118==    by 0x40A1020: puts (in /lib/libc-2.10.1.so)
==5118==    by 0x80484F7: main (main.c:8)
==5118==  Address 0x418a02f is 0 bytes after a block of size 7 alloc'd
==5118==    at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x80484C3: main (main.c:6)
hello world!
==5118== 
==5118== ERROR SUMMARY: 17 errors from 4 contexts (suppressed: 13 from 1)
==5118== malloc/free: in use at exit: 7 bytes in 1 blocks.
==5118== malloc/free: 1 allocs, 0 frees, 7 bytes allocated.
==5118== For counts of detected errors, rerun with: -v
==5118== searching for pointers to 1 not-freed blocks.
==5118== checked 47,492 bytes.
==5118== 
==5118== 
==5118== 7 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5118==    at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5118==    by 0x80484C3: main (main.c:6)
==5118== 
==5118== LEAK SUMMARY:
==5118==    definitely lost: 7 bytes in 1 blocks.
==5118==      possibly lost: 0 bytes in 0 blocks.
==5118==    still reachable: 0 bytes in 0 blocks.
==5118==         suppressed: 0 bytes in 0 blocks.

Wer kann mir sagen, warum und wie es zu beheben.

War es hilfreich?

Lösung

 char *seq=(char *)malloc((strlen(first)+1)*sizeof(char));

Sie sind Zuweisung Speicher für eine Zeichenfolge der Größe von nur ‚first‘.

  strcat(strcpy(seq,first),second);

Und dann versuchen Sie beide ersten und zweiten darin zu passen. Das wird nie funktionieren. strcat nicht mehr Speicher erstellen, müssen Sie enthalten haben, dass in den malloc.

Es gibt keine Notwendigkeit, das Ergebnis von malloc in reinen C gegossen

Es ist auch nicht notwendig, sizeof(char) zu tun, als dass es garantiert ist 1. Manche mögen werden es trotzdem haben die Art sein explict etwa für den Fall, es ändert sich, einige es betrachten Unordnung.

Andere Tipps

Wo ist der entsprechende free() für die malloc()?

Sie sind nur genügend Platz für die erste in Seq zuordnet.

seq nur (strlen (erster) + 1) * sizeof (char) lang, nicht genug, um die verkettete Zeichenfolge zu halten ersten + zweite.

Ich kann sehen, dass die Zeile:

strcat (strcpy (seq, first), Sekunde);

ist nicht falsch umrahmt. Der Grund hierfür ist, können Sie eine String-Verkettung tun, wo Sie nicht eine richtige Quelle zu geben. Es wird funktionieren, wenn Sie die obige Syntax in 2 Zeilen seggregate.

strcpy (Seq, zuerst); strcat (seq, Sekunde);

Das ist, weil, wenn Sie eine Zeichenfolge Kopie tun, wird es die Zeichenfolge „ersten“ bis „f“ kopieren. Nun, für String-Verkettung, da es nicht die richtige Quelle gefunden [erinnert, dass Sie ausdrücklich erwähnt didnot, dass die Quelle „seq“], wird ein ungültiges Schreibspeicherverlust Problem zu geben.

Hope dies verdeutlicht Ihre Frage. Bitte zufällt, ob weitere Informationen erforderlich ist, für das gleiche.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top