1

ユーザーが指定した2つの文字列を連結するプログラムを作成しています。すべて問題ありませんが、プログラムが最終結果のサイズが8ビット長であることを示している理由はわかりません。文字列の長さに関係なく、常に8が表示されます。これはcharのサイズだと思いますが、なぜこのように動作するのかを知りたいと思います。これはコードです:

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

char* concatenate(char *fir, char *sec)
{
int firLen = strlen(fir);
int secLen = strlen(sec);
int len = firLen + secLen + 1;
int i = 0,c=0;
int *wsk = &i;

char *result = (char *)malloc(len*sizeof(char));

while (fir[i]!='\0')
{
    result[i]=fir[i];
    (*wsk)++;
}

while (sec[c]!='\0')
{
    result[i]=sec[c];
    (*wsk)++;
    c++;
}

result[len-1] = '\0';
return result;
}

int main(int argc, char **argv)
{
char *first, *second, *output;
int size1, size2;

printf("How long will your first string be: ");
scanf("%d", &size1);

first = (char *) malloc ((1+size1)*sizeof(char));
if (!first)
{
    puts("\nError. Can't allocate memory!");
    abort();
}

printf("How long will your second string be: ");
scanf("%d", &size2);

second = (char *) malloc ((size2+1)*sizeof(char));
if (!second)
{
    puts("\nError. Can't allocate memory!");
    abort();
}

printf("\nPlease, type in the first string: ");
scanf("%s",first);

printf("\nPlease, type in the second string: ");
scanf("%s",second);

output = (char *)malloc((size1+size2+1)*sizeof(char));
output = concatenate(first, second);

printf("\nConcatenation of the strings: %s", output);
printf("\n%d", sizeof(output));

free(first);
free(second);
free(output);

getchar();
return 0;
}
4

4 に答える 4

6

プログラムの他の部分で行っているように、文字列の長さsizeofを決定するために使用しないでください。関数を使用してください。strlen

printf("\nConcatenation of the strings: %s", output);
printf("\n%d", strlen(output));

、、、配列などのデータsizeofのサイズを決定するために使用します。charstruct

メモリ割り当てですべてが正常かどうかを確認するsizeofために使用したいとおっしゃいましたが、次のように割り当てられたメモリのバッファではこの方法を使用できません。チェック-割り当てが成功したかどうかを確認します。sizeofmallocmalloc

配列のサイズを決定するために使用できます。sizeof

char myStr[13];
printf("%d\n", sizeof(myStr));

ただし、これは配列に対してのみ機能し、を使用して割り当てられたバッファーに対しては機能しませんmalloc

また、次の行のポインタを上書きするため、この行はメモリリークを引き起こします。

output = (char *)malloc((size1+size2+1)*sizeof(char));
于 2012-10-10T15:04:56.900 に答える
2

sizeofサイズをバイト単位で示します。ここで、バイトはのサイズですchar

printf("\n%d", sizeof(output));

outputはであるchar*ため、システムchar*では8バイトの大きさです。strlen0で終了する文字列の長さを取得するにはを使用する必要があります。

于 2012-10-10T15:05:13.493 に答える
1

printf("\n%d", sizeof(output));ポインタのサイズをに出力しますchar。プラットフォームには、サイズが8バイトのポインターがあるようです。

于 2012-10-10T15:05:18.747 に答える
0

初め:

second = (char *) malloc ((size2+1)*sizeof(char)); 

の結果を型キャストしないでくださいmalloc()

2番目: sizeof(char)1になります。charは1バイトで、sizeof()そのデータ型を保持するために必要なバイト数を返します。したがって、は必要ありません。*sizeof(char)のような大きなタイプの場合は必要ですint

ついに:

printf("\nConcatenation of the strings: %s", output);    printf("\n%d", sizeof(output)); 

outputですので、これはあなたのシステムにあなたchar *を与えるでしょう。sizeof(char *)

あなたは前のコメントで言った: I know the differences between strlen and sizeof, but using sizeof(output) I wanted to check if everything is fine with memory allocation

必要なのは、連結された2つの文字列に正しい量のメモリを割り当てたことを確認することだったようです。sizeof()あなたにその情報を与えることはありません。この時点では低レベルの詳細にあります。その情報を見つけるには、OSに依存する何かを行う必要があります。(たとえば、Linuxカーネルでは、kmalloc()メモリを使用ksize()して、取得したバイト数を正確に調べることができます)

于 2012-10-10T15:20:35.560 に答える