1

これは非常によくある質問だと思いますが、次の機能についての考えを知りたいです。アイデアは、この関数が結果のサイズを気にせずに文字列を連結できるということです:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TAM 15 

char* concat(char* answer, char* src, unsigned int* limit, unsigned int* total)
{
 unsigned int length = strlen(src);

 if((*limit - *total) > length)
  strcat(answer, src); 
 else
 {
  *limit *= 2;
  answer = realloc(answer, sizeof(char)*(*limit));
  printf("RESIZING...\n");
  if(answer == NULL)
  {  
   printf("RESIZING ERROR...\n"); 
   return NULL;
  }

  strcat(answer, src); 
 }

 *total += length;

 return answer;
}

int main(void)
{
 char* strings[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE" };

 unsigned int LIMIT = TAM;   //<------
 unsigned int total = 0;     //<------

 char* mem_block = calloc(LIMIT, sizeof(char));

 unsigned int i;
 for(i = 0; i<5; ++i)
  mem_block = concat(mem_block, strings[i], &LIMIT, &total);  //<------

 printf("RES : %s -- %d\n", mem_block, strlen(mem_block));

 free(mem_block);

 return 0;  
}

この例はうまく動作し、valgrind はすべて問題なく報告します。

elias@elias-VirtualBox ~/Desktop $ valgrind ./reSize2
==2939== Memcheck, a memory error detector
==2939== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2939== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==2939== Command: ./reSize2
==2939== 
RESIZING...
RES : ONETWOTHREEFOURFIVE -- 19
==2939== 
==2939== HEAP SUMMARY:
==2939==     in use at exit: 0 bytes in 0 blocks
==2939==   total heap usage: 2 allocs, 2 frees, 45 bytes allocated
==2939== 
==2939== All heap blocks were freed -- no leaks are possible
==2939== 
==2939== For counts of detected and suppressed errors, rerun with: -v
==2939== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 6)

しかし、私が好きではないことがいくつかあります。この変数 //<---- を関数に渡す必要はありません。それを行うより良い方法はありますか?また、リターン ポインター "mem_block" が返されない場合、valgrind は次のように出力します。

==2923== Memcheck, a memory error detector
==2923== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2923== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==2923== Command: ./reSize2
==2923== 
RESIZING...
==2923== Invalid read of size 1
==2923==    at 0x4028D51: strcat (mc_replace_strmem.c:176)
==2923==    by 0x8048523: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923==  Address 0x41ca028 is 0 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid read of size 1
==2923==    at 0x4028D59: strcat (mc_replace_strmem.c:176)
==2923==    by 0x8048523: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923==  Address 0x41ca029 is 1 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid write of size 1
==2923==    at 0x4028D6F: strcat (mc_replace_strmem.c:176)
==2923==    by 0x8048523: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923==  Address 0x41ca033 is 11 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid write of size 1
==2923==    at 0x4028D7C: strcat (mc_replace_strmem.c:176)
==2923==    by 0x8048523: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923==  Address 0x41ca035 is 13 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid write of size 1
==2923==    at 0x4028D81: strcat (mc_replace_strmem.c:176)
==2923==    by 0x8048523: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923==  Address 0x41ca037 is 0 bytes after a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid read of size 1
==2923==    at 0x804864A: main (in /home/elias/Desktop/reSize2)
==2923==  Address 0x41ca028 is 0 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid read of size 1
==2923==    at 0x408A75C: vfprintf (vfprintf.c:1629)
==2923==    by 0x4092C9E: printf (printf.c:35)
==2923==    by 0x405F112: (below main) (libc-start.c:226)
==2923==  Address 0x41ca028 is 0 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid read of size 1
==2923==    at 0x40B374B: _IO_file_xsputn@@GLIBC_2.1 (fileops.c:1317)
==2923==    by 0x4092C9E: printf (printf.c:35)
==2923==    by 0x405F112: (below main) (libc-start.c:226)
==2923==  Address 0x41ca036 is 14 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid read of size 1
==2923==    at 0x40B375F: _IO_file_xsputn@@GLIBC_2.1 (fileops.c:1317)
==2923==    by 0x408A734: vfprintf (vfprintf.c:1629)
==2923==    by 0x4092C9E: printf (printf.c:35)
==2923==    by 0x405F112: (below main) (libc-start.c:226)
==2923==  Address 0x41ca035 is 13 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid read of size 1
==2923==    at 0x40B36E8: _IO_file_xsputn@@GLIBC_2.1 (fileops.c:1349)
==2923==    by 0x408A734: vfprintf (vfprintf.c:1629)
==2923==    by 0x4092C9E: printf (printf.c:35)
==2923==    by 0x405F112: (below main) (libc-start.c:226)
==2923==  Address 0x41ca028 is 0 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== Invalid read of size 1
==2923==    at 0x40B36F4: _IO_file_xsputn@@GLIBC_2.1 (fileops.c:1348)
==2923==    by 0x408A734: vfprintf (vfprintf.c:1629)
==2923==    by 0x4092C9E: printf (printf.c:35)
==2923==    by 0x405F112: (below main) (libc-start.c:226)
==2923==  Address 0x41ca02a is 2 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
RES : ONETWOTHREEFIVE -- 15
==2923== Invalid free() / delete / delete[]
==2923==    at 0x4027C02: free (vg_replace_malloc.c:366)
==2923==    by 0x8048677: main (in /home/elias/Desktop/reSize2)
==2923==  Address 0x41ca028 is 0 bytes inside a block of size 15 free'd
==2923==    at 0x402896C: realloc (vg_replace_malloc.c:525)
==2923==    by 0x8048546: concat (in /home/elias/Desktop/reSize2)
==2923==    by 0x8048624: main (in /home/elias/Desktop/reSize2)
==2923== 
==2923== 
==2923== HEAP SUMMARY:
==2923==     in use at exit: 30 bytes in 1 blocks
==2923==   total heap usage: 2 allocs, 2 frees, 45 bytes allocated
==2923== 
==2923== LEAK SUMMARY:
==2923==    definitely lost: 30 bytes in 1 blocks
==2923==    indirectly lost: 0 bytes in 0 blocks
==2923==      possibly lost: 0 bytes in 0 blocks
==2923==    still reachable: 0 bytes in 0 blocks
==2923==         suppressed: 0 bytes in 0 blocks
==2923== Rerun with --leak-check=full to see details of leaked memory
==2923== 
==2923== For counts of detected and suppressed errors, rerun with: -v
==2923== ERROR SUMMARY: 80 errors from 12 contexts (suppressed: 11 from 6)

関数がポインターを直接変更するため、これは必要ないと思ったので、少し混乱しています。

この機能を改善する方法についてのコメントは大歓迎です。

どうもありがとうございました。長い投稿で申し訳ありません。

4

2 に答える 2

3

関数で answer を変更する場合、関数のスコープでのみ変更すると、呼び出し元の関数には古い値が表示されます (realloc を呼び出したので無効です)。

新しいポインターを返し、呼び出し元の関数でそれを使用し始めると、すべてが正常に機能します。

これを修正する最も簡単な方法は、concat 関数を次のようにすることです。

void concat(char** answer, char* src, unsigned int* limit, unsigned int* total) {
if((*limit - *total) > length)
  strcat(*answer, src); 
 else
 {
  *limit *= 2;
  *answer = realloc(*answer, sizeof(char)*(*limit));
  printf("RESIZING...\n");
  if(*answer == NULL)
  {  
   printf("RESIZING ERROR...\n"); 
   return NULL;
  }

  strcat(*answer, src); 
 }

 *total += length;
}

そしてそれを次のように呼び出します:

concat(&mem_block, strings[i], &LIMIT, &total);

ただし、文字列の長さを手動で一緒に渡すのではなく、文字列を独自の型に抽象化する必要があります。何かのようなもの:

typedef struct _dynamic_string {
    char *s;
    unsigned int capacity;
    unsigned int length;
} dynamic_string;

良い出発点である必要があり、代わりにこの構造体を使用して動的関数の実装を開始できます。

于 2012-10-04T16:28:23.233 に答える
0

limit * 2式が現在の制限 + よりも低いかどうかを確認する必要はありませんlengthか? その場合は、制限を増やす必要があります。そうしないと、強制的にバッファ オーバーフローが発生する可能性があると思います。

場合によっては編集されるため、言及した変数をポインターとして渡す必要があると思います。意味的には同じですが、構文的には「より簡単」な代替案は、次のように定義することstruct stringです。

struct string {
    char* buffer;
    int limit;
    int total;
};

これは、文字列を表すすべての情報を抽象化するために使用できます。

問題に関しては、realloc のドキュメントmem_blockによると、それを受け取る必要があります。

The function may move the memory block to a new location, in which case the new location is returned.

そのため、パラメータとして渡された文字列アドレスが実際には返されたアドレスと異なる場合があります。ポインターへのポインターを引数として渡すことで修正できるため、ポインターが変更されたときにその場で更新できます。

void concat(char** answer, char* src, unsigned int* limit, unsigned int* total) {
    /* When using strcat: */
    strcat(*answer, src);

    /* When reallocating */
    *answer = realloc(*answer, *limit);
            /* Also, sizeof(char) is always 1, by definition */

    /* ... */
}

これが役立つことを願っています=)

于 2012-10-04T16:23:14.803 に答える