c/Append/Delete basic中的数组函数 #包括 #包括 #包括 int*创建_int_数组(){ int*arr; arr=(int*)calloc(1,sizeof(int)); 返回arr; } 字符**创建字符串数组(){ char**arr=calloc(1,sizeof(char)); 返回arr; } void append_int(int*数组、int元素、int索引){ 数组=(数组+索引); *数组=元素; } void append_字符串(字符**数组,字符*元素,整数索引){ *(数组+索引)=calloc(1,sizeof(char*); strcpy(*(数组+索引),元素); } void delete_字符串(字符**数组,整数索引){ 自由(数组[索引]); } void delete_int(int*数组,int索引){ 数组[索引]=NULL; } ///////我是我的朋友/////// #包括 #包括 #包括 #包括“基本数据文件.h” int main(int argc,char const*argv[] { /*代码*/ 字符**数组; 数组=创建字符串数组(); char*full_name=calloc(strlen(“hamza arslan”)、sizeof(char*); strcpy(全名,“hamza arslan”); char*mail=calloc(strlen(“test@gmail.com)的大小(字符*); strcpy(邮件,“test@gmail.com"); char*address=calloc(strlen(“Hacettepe university”),sizeof(char*); strcpy(地址,“哈塞特大学”); char*statu=calloc(strlen(“学生”),sizeof(char*); strcpy(statu,“student”); 追加字符串(数组,全名,0); 追加_字符串(数组、邮件、1); 追加字符串(数组,地址,2); 追加字符串(数组,statu,4); 对于(int i=0;i
我试图拥有我的基本阵列库。正如您所知,在某些语言中,其他语言具有高级数组类型。他们正在简化我们的库存操作。但在c语言中,它更复杂,特别是关于字符串。我这里有两个问题。首先,当我在append_string函数中给出index=3时,代码会出现中止(内核转储)错误。(./run):双重释放或损坏(out))。其次,当我检查内存泄漏时,即使我使用空闲内存也会导致内存泄漏。我能做什么?会有几次崩溃,但这里有一个:c/Append/Delete basic中的数组函数 #包括 #包括 #包括 int*创建_int_数组(){ int*arr; arr=(int*)calloc(1,sizeof(int)); 返回arr; } 字符**创建字符串数组(){ char**arr=calloc(1,sizeof(char)); 返回arr; } void append_int(int*数组、int元素、int索引){ 数组=(数组+索引); *数组=元素; } void append_字符串(字符**数组,字符*元素,整数索引){ *(数组+索引)=calloc(1,sizeof(char*); strcpy(*(数组+索引),元素); } void delete_字符串(字符**数组,整数索引){ 自由(数组[索引]); } void delete_int(int*数组,int索引){ 数组[索引]=NULL; } ///////我是我的朋友/////// #包括 #包括 #包括 #包括“基本数据文件.h” int main(int argc,char const*argv[] { /*代码*/ 字符**数组; 数组=创建字符串数组(); char*full_name=calloc(strlen(“hamza arslan”)、sizeof(char*); strcpy(全名,“hamza arslan”); char*mail=calloc(strlen(“test@gmail.com)的大小(字符*); strcpy(邮件,“test@gmail.com"); char*address=calloc(strlen(“Hacettepe university”),sizeof(char*); strcpy(地址,“哈塞特大学”); char*statu=calloc(strlen(“学生”),sizeof(char*); strcpy(statu,“student”); 追加字符串(数组,全名,0); 追加_字符串(数组、邮件、1); 追加字符串(数组,地址,2); 追加字符串(数组,statu,4); 对于(int i=0;i,c,arrays,dynamic-memory-allocation,C,Arrays,Dynamic Memory Allocation,我试图拥有我的基本阵列库。正如您所知,在某些语言中,其他语言具有高级数组类型。他们正在简化我们的库存操作。但在c语言中,它更复杂,特别是关于字符串。我这里有两个问题。首先,当我在append_string函数中给出index=3时,代码会出现中止(内核转储)错误。(./run):双重释放或损坏(out))。其次,当我检查内存泄漏时,即使我使用空闲内存也会导致内存泄漏。我能做什么?会有几次崩溃,但这里有一个: #include <stdio.h> #include <stdlib
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* create_int_array(){
int* arr;
arr = (int *)calloc(1,sizeof(int));
return arr;
}
char** create_string_array(){
char** arr = calloc(1,sizeof(char));
return arr;
}
void append_int(int* array, int element, int index){
array = (array+index);
*array = element;
}
void append_string(char** array , char* element,int index){
*(array + index) = calloc(1,sizeof(char*));
strcpy(*(array + index),element);
}
void delete_string(char** array, int index){
free(array[index]);
}
void delete_int(int* array,int index){
array[index] = NULL;
}
/////// M A I N F I L E ///////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "basic_data_file.h"
int main(int argc, char const *argv[])
{
/* code */
char **array;
array = create_string_array();
char *full_name = calloc(strlen("hamza arslan"),sizeof(char*));
strcpy(full_name,"hamza arslan");
char* mail = calloc(strlen("test@gmail.com"),sizeof(char*));
strcpy(mail,"test@gmail.com");
char* address = calloc(strlen("Hacettepe Universty"),sizeof(char*));
strcpy(address,"Hacettepe Universty");
char* statu = calloc(strlen("student"),sizeof(char*));
strcpy(statu,"student");
append_string(array,full_name,0);
append_string(array,mail,1);
append_string(array,address,2);
append_string(array,statu,4);
for(int i=0; i< 3; i++){
printf("%s\n",array[i]);
free(array[i]); // get free double pointer
}
printf("%s\n",array[4]); // because index 3 blow up
free(full_name);
free(mail);
free(address);
free(statu);
return 0;
}
您分配的1个字节不足以存储(char*),它需要2到8个字节,具体取决于操作系统和目标计算机
请尝试以下方法:
char** arr = calloc(1,sizeof(char));
你应该仔细检查每一行代码。C不是一种宽容的语言-错误会受到严厉的惩罚。您的
create\u xy\u array
函数分配一个由1个元素组成的数组,并一直保持到最后。当你有一个单元素数组和索引,读/写它的第二个和更多的元素时,C本身很高兴地同意,但是结果不会起作用,它会默默地破坏它路径中的一切
首先,对于动态数组,您必须自己跟踪其长度,C分配/数组不知道自己的大小。所以你需要一个结构,包含长度和指针,比如
char** arr = calloc(1,sizeof(char*));
然后将其分配给0个元素,因为开始时内部没有任何内容:
typedef struct IntArray {
int length;
int *elements;
} IntArray;
然后你可以试着把东西放进去:
IntArray* create_int_array() {
IntArray* ret = (IntArray*) malloc(sizeof(IntArray));
ret->length = 0;
ret->elements = NULL;
return ret;
}
void free_int_array(IntArray* arr) {
free(arr->elements);
free(arr);
}
(追加意味着在数组末尾添加一些内容,这里不需要索引)
这可能会永远持续下去,删除任意元素会将数组的“上”部分(memcpy)移动,并将结果调整为一个较小的元素,或者您可以跟踪数组的容量,该容量可能大于其当前长度(但随后必须将其合并到append函数中,可能还有其他函数)
(免责声明:我希望代码片段是正确的,但我不太经常使用C语言——出于同样的原因,我不能推荐一个好的教程,但这正是您可能需要的)注意:我已经多年没有使用C语言编写代码了,我也没有检查过代码,所以请仔细检查并让我知道。 根据你的描述,你正在尝试做一个向量 因此,有不同的方法可以处理这个问题 方法1: 创建一个包含阵列、阵列容量和阵列大小的结构
void append_int(IntArray* arr, int element) {
arr->length++;
arr->elements = (int*) realloc(arr->elements, arr->length*sizeof(int));
arr->elements[length-1] = element;
}
这里的诀窍是在增加/减少容量时要注意
如果将容量增加到阵列大小以上,则:
- 您需要创建一个容量加倍的新阵列
- 将所有元素复制到新数组
- 将指针更改为新数组
- 释放旧阵列的内存
typedef struct StrArray{
int capacity;
int size;
int *integers;
}
方法3:
这种方法是前一种方法的延续
为了减少内存分配,您可以创建一个相当于一个包含所有操作函数的单例,然后assi请正确格式化代码。编程时要学习的第一件事是如何组织代码。请特别注意缩进。这传达了意图和结构,对我们快速查看代码时尤为重要。当您说“数组”时,您是指“字符串缓冲区”吗?这看起来像是
append\u string
是带有一些分配功能的strcat
的重新实现。发送可变指针(char**
)也不是真正的C风格,而是返回指针而不是void
。这段代码也充满了内存泄漏,因为它在重新分配和追加时没有正确释放旧的缓冲区。它仍然存在泄漏,没有改变任何东西。@claymorehack查看我关于内存泄漏的答案。我想我看到了你的漏洞。当你有一个2D数组时,你有一个指针列表。列表本身也是一个指针(我这里指的是动态分配的)。你们正在释放所有这些吗?@JoshDetwiler我在for循环双指针中释放的主文件的最后一个,但可能是在函数中分配内存的问题。但是
typedef struct StrStorage{
int capacity;
int size;
int *integers;
} StrStorage;
typedef struct StrArray {
StrStorage storage;
int (*capacity)(StrArray*);
int (*size)(StrArray*);
StrArray *(*append)(StrArray*, int);
void (*increaseStorage)(StrArray*);
// Add other methods here
} StrArray;
int capacity(StrArray *self) {
return self->storage->capacity;
}
int size(StrArray *self) {
return self->storage->size;
}
StrArray *append(StrArray *self, int integer){
if ((self->capacity() + 1) > self->size()){
self->increaseStorage();
}
// The rest of the magic goes here
return self;
}
StrArray *initializeStrArray(int n) {
StrArray* strArray = malloc(sizeof(StrArray));
strArray->chars = malloc(sizeof(char) * n);
strArray->capacity= capacity;
strArray->append = append;
return strArray;
}