我正在開發一個使用 C 語言動態分配陣列的基本框架。我創建了一個函式來創建一個名為init_string_vector. 可以使用函式將資料附加到陣列中,append_string_vector并且可以使用函式從堆中釋放資料free_string_array。我目前正在研究一個標題為replace_string_vector_index允許用戶將陣列索引傳遞給函式以及指向字串陣列的指標的函式。如果陣列型別為STRING陣列且索引未超出范圍,則函式應將現有資料替換為用戶傳遞給函式的字串。
該replace_string_vector_index函式似乎可以正常作業,并且確實將索引處的字串替換為用戶傳遞給該函式的另一個字串。但是,一旦我習慣了作用于陣列的函式,該free_string_array函式就不再起作用了。replace_string_vector_index這讓我認為函式中的程序導致了問題,但我看不出如何。一個例子如下所示。當free_string_array函式失敗時,我收到以下錯誤,free(): invalid pointer.
矢量.h
#ifndef ARRAY_H
#define ARRAY_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef enum
{
FLOAT,
DOUBLE,
CHAR,
INT,
STRING
} dat_type;
// --------------------------------------------------------------------------------
typedef struct
{
char **array;
size_t len;
int elem;
dat_type dat;
} StringVector;
// --------------------------------------------------------------------------------
int string_vector_mem_alloc(StringVector *array, size_t num_indices);
// --------------------------------------------------------------------------------
StringVector init_string_vector();
// --------------------------------------------------------------------------------
int append_string_vector(StringVector *s, char *value);
// --------------------------------------------------------------------------------
void free_string_array(StringVector *array);
// --------------------------------------------------------------------------------
int replace_string_vector_index(StringVector *array, int index, char string[]);
// --------------------------------------------------------------------------------
矢量.c
#include "vector.h"
int string_vector_mem_alloc(StringVector *array, size_t num_indices) {
// Determine the total memory allocation and assign to pointer
void *pointer;
pointer = malloc(num_indices * array->elem);
// If memory is full fail gracefully
if (pointer == NULL) {
printf("Unable to allocate memory, exiting.\n");
free(pointer);
return 0;
}
// Allocate resources and instantiate Array
else {
array->array = pointer;
array->len = 0;
return 1;
}
}
// --------------------------------------------------------------------------------
StringVector init_string_vector() {
StringVector array;
array.dat = STRING;
array.elem = sizeof(char *);
string_vector_mem_alloc(&array, array.elem);
return array;
}
// --------------------------------------------------------------------------------
int append_string_vector(StringVector *array, char *value) {
value = strdup(value);
if (!value) {
return -1;
}
array->len ;
char **resized = realloc(array->array, sizeof(char *)*array->len 1);
if (!resized) {
free(value);
return -1;
}
resized[array->len-1] = value;
array->array = resized;
return 0;
}
// --------------------------------------------------------------------------------
void free_string_array(StringVector *array) {
if (array != NULL) {
for (int i = 0; i < array->len; i ) {
free(array->array[i]);
}
}
free(array->array);
// Reset all variables in the struct
array->array = NULL;
array->len = 0;
array->elem = 0;
}
// --------------------------------------------------------------------------------
int replace_string_vector_index(StringVector *array, int index, char string[]) {
if (array->dat != STRING) {
printf("Array data type must be a STRING");
return 0;
}
if (index > array->len) {
printf("Index is greater than array length");
return 0;
}
* (char **) ((char *) array->array index * array->elem) = string;
return 1;
}
// --------------------------------------------------------------------------------
主程式
#include <stdio.h>
#include "vector.h"
int main(int argc, const char * argv[]) {
StringVector arr_test = init_string_vector();
char one[] = "Hello";
char two[] = "World";
char three[] = "Hello";
char four[] = "Goodbye";
append_string_vector(&arr_test, one);
append_string_vector(&arr_test, two);
append_string_vector(&arr_test, three);
append_string_vector(&arr_test, four);
// I can free the array at this point
free_string_array(&arr_test)
StringVector arr_test = init_string_vector();
append_string_vector(&arr_test, one);
append_string_vector(&arr_test, two);
append_string_vector(&arr_test, three);
append_string_vector(&arr_test, four);
replace_string_vector_index(&arr_test, 1, one);
// - Once I envoke replace_string_vector_index, free_string_array
// no longer works, and I get an invalid pointer error.
free_string_array(&arr_test);
}
uj5u.com熱心網友回復:
如果我了解您的replace_string_vector_index功能的要求,您應該首先釋放 的記憶體array->array[index],然后將結果分配strdup(string)給該元素。
無需強制轉換,無需復雜的指標運算。簡單地說:
free(array->array[index]);
array->array[index] = strdup(string);
現在發生的事情(我認為)是您array->array[index]指向包含字串的陣列(即您忘記了strdup步驟)。不是由 分配的陣列,malloc也不能傳遞給free。
由于您將它free作為您的一部分傳遞給您,因此free_string_array您將有未定義的行為。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/480167.html
上一篇:“C”中函式內部的資料操作
