最初に、あなたの仕様によれば、max_size
は符号なし整数でなければならないので、これを反映するようにVector
をsize_t
に変更しました。この新しいタイプに合わせて、関連するフォーマット指定子を%d
から%zu
に変更しました。
initVector()
機能がVector
のメモリを割り当てる必要があり、追加されています。さらに、ここでData
構造体の動的配列にメモリを割り当てる必要がないため、v->data
はNULL
に設定されています。この関数は、Vector
の.data
フィールドへのポインタではなく、新しく割り当てられたメモリvへのポインタも返さなければなりません。
vectorInsert()
関数では、メモリ割り当てエラーをチェックするのを忘れてしまったので、割り当てが試行された後にチェックが追加され、エラーがあれば0が返されます。新しいData
構造体を挿入した後、インクリメント.current_size
へのチェックが間違っています。まず、array->current_size <= index
の場合は増分する必要があります。次に、.current_size
に1つ追加する必要があります..current_size
をインデックス値よりも大きいものに設定しないでください。また、ここに挿入された値を印刷するときは、.value
フィールドへのアクセスを忘れました。私はこれがに渡されたData
構造体のために使用した混乱した名前のためかもしれないと思います。この構造体をvalue
と呼びます。前の行では、array->data[index] = value
があり、value
という構造体をarray->data[index]
に割り当てています。しかし、printf()
への呼び出しでは、構造体value
が保持する値を表示する必要があります。より良い名前を選ぶことは常に勝利です!最後に、この関数は挿入が成功すると1を返します。
Iはvect
とvect->data
の内容を表示するテストコードに加え、また、任意のインデックスへの挿入をテストするData
構造体、test_insert
を添加。
最後に、この後にメモリ割り当てを解放する必要があるため、free()
への2回の呼び出しを追加しました。ここで
はコードです:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int value;
}Data;
/* Specs say that max_size should be an unsigned integer */
typedef struct{
size_t max_size; //initialize to 0
size_t current_size; //initialize to 0
Data *data; // array of integers we're storing
} Vector;
/* Modified function */
Vector * initVector(void){
Vector *v = malloc(sizeof(*v));
v->max_size=0;
v->current_size=0;
v->data = NULL;
return v;
}
int vectorInsert(Vector * array, size_t index, Data value)
{
if(index >= array->max_size)
{
array->max_size = index * 2 + 1;
printf("Inside Vect max_size is : %zu\n", array->max_size);
Data *new_array = malloc(sizeof(Data) * array->max_size);
/* Added check for allocation error */
if (new_array == NULL)
return 0;
if(array->data != NULL)
{
memcpy(new_array, array->data, sizeof(Data)*array->current_size);
free(array->data);
array->data = NULL;
}
array->data = new_array;
}
array->data[index] = value;
printf("Main : %d\n", array->data[index].value);
/* Modified current_size increment logic */
if(array->current_size <= index)
{
array->current_size += 1;
}
/* Successful insertion */
return 1;
}
int main(void)
{
size_t i;
Vector *vect = initVector();
Data data_array[20];
Data test_insert = { -5 }; // to test index insertion
for(i = 0 ; i < 20 ; i++){
data_array[i].value = (rand() % 20) + 1;
vectorInsert(vect, i, data_array[i]);
}
/* Display results */
printf("vect->max_size = %zu\n", vect->max_size);
printf("vect->current_size = %zu\n", vect->current_size);
printf("vect->data contains:\n");
for (i = 0; i < vect->current_size; i++)
printf("%d ", vect->data[i].value);
putchar('\n');
/* Insert test_insert at index 5 */
vectorInsert(vect, 5, test_insert);
/* Display results */
printf("vect->max_size = %zu\n", vect->max_size);
printf("vect->current_size = %zu\n", vect->current_size);
printf("vect->data contains:\n");
for (i = 0; i < vect->current_size; i++)
printf("%d ", vect->data[i].value);
putchar('\n');
/* Free memory allocations */
free(vect->data);
free(vect);
return 0;
}
そして、ここでは、結果のサンプルです:
vect->max_size = 31
vect->current_size = 20
vect->data contains:
4 7 18 16 14 16 7 13 10 2 3 8 11 20 4 7 1 7 13 17
vect->max_size = 31
vect->current_size = 20
vect->data contains:
4 7 18 16 14 -5 7 13 10 2 3 8 11 20 4 7 1 7 13 17
あなたが投稿したコードに問題がありますか?何を聞いていますか? –
また、 'Vector'はあなたが作成した型です、これはC++の標準ライブラリ' Vector'です – Mobius
Vectorを作成しました。私はもう一度コードを編集しました。 – King