gpt4 book ai didi

C - 2D 字符指针数组重新分配

转载 作者:行者123 更新时间:2023-11-30 17:08:03 24 4
gpt4 key购买 nike

所以我有这个代码:

#include <stdio.h>
#include <stdlib.h>
void init_pointer_arr(void)
{
char *arr[1];
int command_count = 1;
int length_counter = 1;
int fixed_cols = 5;

int i, j;

// malloc -first- initialization of the array
*arr = malloc(sizeof(char) * 5);
if (arr == NULL) {
printf("17:malloc NULL");
}

/**************************************DEBUG***************************/
// init with char 'a' for debug purpose
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
arr[i][j] = 'a';
}
}

// print
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
printf("%c", arr[i][j]);
}
}
\*************************************END DEBUG*******************/

printf("\nrealloc\n"); // DEBUG
// realloc
//arr = realloc(arr, sizeof(arr[0]));
arr[0] = malloc(sizeof(char) * 5);
arr[1] = malloc(sizeof(char) * 5);
if (arr == NULL)
{
printf("51:malloc NULL");
}

/***************Re init newly allocate space for DEBUG*********************/
// init newly allocated space
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
arr[i][j] = 's';
}
}
printf("s\n"); // DEBUG
// print
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
printf("%c", arr[i][j]);
}
}

\******************************END DEBUG*****************************/

}

int main() {
init_pointer_arr();
system("pause");
return 0;
}

我基本上想用指针数组实现一个二维数组,但我不断收到错误:“内存arround arr已损坏”,此错误的来源是这两行:

    arr[0] = malloc(sizeof(char) * 5);
arr[1] = malloc(sizeof(char) * 5);

我看过其他关于此问题的帖子,但似乎没有人按照我尝试的方式解决这个问题,而且上面的代码编译得很好,没有警告。

我很困惑,为什么会发生这种情况。任何帮助将不胜感激。提前致谢。

编辑〜一开始 arr 只有一行,但在某些时候(上面的麻烦代码)我想添加更多行,我也尝试过 realloc (您可以在与其他行相同的位置的注释中看到它) 2 上面)。但也没有运气。

最佳答案

问题

  • storing char * array on the stack not allowing expansion
  • printing hardcoded line numbers ( which might change )
  • not exiting on failure ( eg malloc failure )
  • memory leaks ( not freeing the memory afterwards )
  • system("pause") is windows specific

调整后的代码

#include <stdio.h>
#include <stdlib.h>
void init_pointer_arr(void)
{
/* char *arr[1]; */
// store the char * s on the heap
char **arr, **temp;
int command_count = 1;
int length_counter = 1;
int fixed_cols = 5;

int i, j;

// malloc storage for the char * s
arr = malloc(command_count * sizeof(char *));
if(!arr)
{
goto err0;
}

// malloc -first-
arr[0] = malloc(sizeof(char) * 5);
if (arr[0] == NULL) {
// printf("17:malloc NULL");
printf("%d:malloc NULL\n", __LINE__);
// should return on failure :
goto err1;
}

// init
// you might want to put some \n in these printfs..
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
arr[i][j] = 'a';
}
}

// print
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
printf("%c", arr[i][j]);
}
}


printf("\nrealloc\n");
// realloc
command_count = 2;
temp = realloc(arr, command_count * sizeof(char *));
if(!temp)
{
goto err2;
}
arr = temp;
// arr[0] has already been alloced, not necessary again
// and will cause a leak
// arr[0] = malloc(sizeof(char) * 5);
arr[1] = malloc(sizeof(char) * 5);
if (arr[1] == NULL)
{
printf("%d:malloc NULL\n", __LINE__);
goto err2;
}


// init newly allocated space
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
arr[i][j] = 's';
}
}
printf("s\n");
// print
for (i = 0; i < command_count; i++)
{
for (j = 0; j < length_counter*fixed_cols; j++)
{
printf("%c", arr[i][j]);
}
}

err3:
free(arr[1]);
err2:
free(arr[0]);
err1:
free(arr);
err0:
return ;
}

int main() {
init_pointer_arr();
#if defined(_WIN32) || defined(WIN32)
system("pause");
#endif
return 0;
}

输出

$ gcc -g test.c -o test
$ valgrind ./test
==3612== Memcheck, a memory error detector
==3612== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3612== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==3612== Command: ./test
==3612==
aaaaa
realloc
s
ssssssssss==3612==
==3612== HEAP SUMMARY:
==3612== in use at exit: 0 bytes in 0 blocks
==3612== total heap usage: 4 allocs, 4 frees, 34 bytes allocated
==3612==
==3612== All heap blocks were freed -- no leaks are possible
==3612==
==3612== For counts of detected and suppressed errors, rerun with: -v
==3612== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
<小时/>

引用

关于C - 2D 字符指针数组重新分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com