C 如何将临时值指定给二维数组
因此,我有一个2d结构数组,用于存储csv文件中的数据。我正在读取文件中的字符串,然后我想在2d数组中将它们的值指定为该字符串。例如,位置[0][0]为红色,位置[1][1]为蓝色或其他颜色。问题是,我正在从文件中读取字符并将它们存储在临时字符串中,然后我想将该临时字符串值指定给我所在的当前位置。因此,我将temp设置为红色,我希望[0][0]为红色,然后它从文件中读取更多内容,temp现在为蓝色,我希望[0][1]为蓝色。当我分配[0][0]=temp,然后分配[0][1]=temp时,它们会相互覆盖,因为它是指针。我已经尝试过strduptemp,但这似乎并不能解决问题,或者他们有时还会相互重写。我知道我可以使用strcpy,但对于strcpy,我必须有另一个字符串来复制它,所以我尝试每次只创建一个新的char数组,但这也不起作用。它仍然会被写下来。我知道我可以解决这个问题,如果每次我分配一个值,我用一个新的名称创建一个新的字符数组,并指向该字符数组,但这是一个非常大的csv文件,那么我将不得不创建大约1000万个字符数组,我不知道如何做,似乎应该有一个更简单的方法 非常感谢您的帮助,谢谢C 如何将临时值指定给二维数组,c,arrays,csv,multidimensional-array,struct,C,Arrays,Csv,Multidimensional Array,Struct,因此,我有一个2d结构数组,用于存储csv文件中的数据。我正在读取文件中的字符串,然后我想在2d数组中将它们的值指定为该字符串。例如,位置[0][0]为红色,位置[1][1]为蓝色或其他颜色。问题是,我正在从文件中读取字符并将它们存储在临时字符串中,然后我想将该临时字符串值指定给我所在的当前位置。因此,我将temp设置为红色,我希望[0][0]为红色,然后它从文件中读取更多内容,temp现在为蓝色,我希望[0][1]为蓝色。当我分配[0][0]=temp,然后分配[0][1]=temp时,它们会
struct node {
char* value;
};
char temp[500];
struct node ** arrayofnodes[35];
for(int i=0; i<35; i++) {
arrayofnodes[i] = malloc(test * sizeof (struct node));
for(int j=0; j<test; j++) arrayofnodes[i][j] = malloc(sizeof (struct node));
}
//(all of this below happens inside a function because it is going to be called like 10 million times)
arrayofnodes[row][count]->value=temp; //way 1
arrayofnodes[row][count]->value=strdup(temp); //way 2
char fun[500];
strcpy(fun,temp);
arrayofnodes[row][count]->value=fun;//way 3
arrayofnodes[i]=mallocN*sizeof结构节点;为N个结构留出足够的空间。您需要的是N个结构指针的空间
但是我不认为这是你的主要问题。唯一的方法2是有效的-您确实需要为您读取的每个新字符串创建存储。方法1是重新使用临时存储器,方法3是重新使用“乐趣”存储器,一旦退出函数,该存储器将无效
如果方法2不适用于您,我将查看生成行和计数的代码。这些索引是否总是分别小于35和test?使用malloc或strdup时,需要额外的16字节空间进行管理。 当您分配大量小内存时,这个额外的空间也会占用大量内存。 因此,考虑自己的管理内存,或者TCMALOLC,JEMALOC 以下是测试数据: gcc-otest试验。c-壁-O3-g
[test_strdup] cost time: 1085247 us, use memory: 839.81 MB
[test_optimized] cost time: 394635 us, use memory: 411.71 MB
gcc-otest试验.c-壁-O3-g-低温低熔点
[test_strdup] cost time: 627160 us, use memory: 461.07 MB
[test_optimized] cost time: 397938 us, use memory: 422.85 MB
gcc-otest试验.c-壁-O3-g-ljemalloc
[test_strdup] cost time: 749875 us, use memory: 481.77 MB
[test_optimized] cost time: 330825 us, use memory: 451.96 MB
下面是测试代码
为了比较内存分配,测试代码中存在内存泄漏
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/sysinfo.h>
#define ROWS 35
#define COLS (1000*10000/ROWS)
#define MB (1024*1024)
#define TEST_STR "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
struct node {
char *value;
};
long current_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000L + tv.tv_usec;
}
long current_usemem()
{
FILE *fp;
long resident = 0;
fp = fopen("/proc/self/statm", "r");
if (fp) {
if (fscanf(fp, "%*s %ld ", &resident) != 1)
resident = 0;
fclose(fp);
}
resident *= 4096;
return resident;
}
void test_strdup()
{
char temp[500];
struct node **arrayofnodes[ROWS];
int i, j;
long start_time, end_time;
long start_usemem, end_usemem;
strcpy(temp, TEST_STR);
start_usemem = current_usemem();
start_time = current_time();
for(i = 0; i < ROWS; i++) {
arrayofnodes[i] = (struct node **)malloc(COLS * sizeof(struct node *));
for(j = 0; j < COLS; j++) {
arrayofnodes[i][j] = (struct node *)malloc(sizeof (struct node));
}
}
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
arrayofnodes[i][j]->value = strdup(temp);
}
}
end_time = current_time();
end_usemem = current_usemem();
printf("[%s] cost time: %ld us, use memory: %.2f MB\n",
__FUNCTION__, end_time - start_time,
(end_usemem - start_usemem) / 1024.0 / 1024);
}
struct memory_chunk {
struct memory_chunk *next;
char *cur;
char *end;
char buf[0];
};
struct memory_pool {
struct memory_chunk *head;
};
void *pool_alloc(struct memory_pool *pool, size_t size)
{
void *ret;
struct memory_chunk *chunk;
chunk = pool->head;
if (chunk == NULL || chunk->cur + size >= chunk->end) {
size_t len = (size < MB ? MB : size + sizeof(*chunk));
chunk = (struct memory_chunk *)malloc(len);
chunk->next = pool->head;
chunk->end = (char *)chunk + len;
chunk->cur = chunk->buf;
pool->head = chunk;
}
ret = chunk->cur;
chunk->cur += size;
return ret;
}
char *pool_strdup(struct memory_pool *pool, const char *s)
{
size_t size = strlen(s) + 1;
void *ret = pool_alloc(pool, size);
memcpy(ret, s, size);
return ret;
}
void test_optimized()
{
char temp[500];
struct node ***arrayofnodes;
int i, j;
long start_time, end_time;
long start_usemem, end_usemem;
struct memory_pool pool = {NULL};
strcpy(temp, TEST_STR);
start_usemem = current_usemem();
start_time = current_time();
arrayofnodes = (struct node ** *)pool_alloc(&pool, ROWS * sizeof(struct node **));
for(i = 0; i < ROWS; i++) {
arrayofnodes[i] = (struct node **)pool_alloc(&pool, COLS * sizeof(struct node *));
for(j = 0; j < COLS; j++) {
arrayofnodes[i][j] = (struct node *)pool_alloc(&pool, sizeof(struct node));
}
}
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
arrayofnodes[i][j]->value = pool_strdup(&pool, temp);
}
}
end_time = current_time();
end_usemem = current_usemem();
printf("[%s] cost time: %ld us, use memory: %.2f MB\n",
__FUNCTION__, end_time - start_time,
(end_usemem - start_usemem) / 1024.0 / 1024);
}
int main()
{
test_strdup();
test_optimized();
return 0;
}
建议你读一下这个。strdup方法是一种可行的方法,并且是三种方法中唯一可行的方法。如果它对你不起作用,那么我们就需要考虑A来做更多的事情而不是猜测为什么不这样做。