需要帮助在C中创建动态char数组

时间:2021-07-21 02:08:10

I'm having trouble creating a dynamic char array. This is what I have so far.

我在创建动态char数组时遇到了麻烦。这就是我到目前为止所拥有的。

char * arr;
arr = (char*)malloc (2 * sizeof (char));

It's not allocating space for only 2 characters, it's letting me enter up to arr[8] and then giving me strange errors after 8.

它不是仅为2个字符分配空间,它让我进入arr [8]然后在8之后给我奇怪的错误。

I also tried making a 2 dimensional char array. The first dimension allocates correctly, but then the second dimension has more space than I allow it to have and gets an error at around 12 characters or so. Any help would be greatly appreciated. I would prefer to make a 1 dimensional dynamic array if possible.

我也试过制作一个二维char数组。第一个维度正确分配,但是第二个维度有比我允许的更多的空间,并且大约12个字符左右出现错误。任何帮助将不胜感激。如果可能的话,我更愿意制作一维动态数组。

2 个解决方案

#1


1  

This line arr = (char*)malloc (2 * sizeof (char)); will allocate memory for 2 bytes only. But you are overwriting the memory by accessing the more 8 or more than 8 byes. If you access more than two byes means, it will give some unpredictable issue. In case you want more memory please follow the below code.

这行arr =(char *)malloc(2 * sizeof(char));将仅为2个字节分配内存。但是你通过访问8个或8个以上的字节来覆盖内存。如果你访问两个以上的byes方法,它将给出一些不可预测的问题。如果您想要更多内存,请遵循以下代码。

#define USER_SIZE 10
arr = (char*)malloc ( USER_SIZE * sizeof (char));

Assign the value in USER_SIZE macro and then allocate the memory as much as you want.

在USER_SIZE宏中分配值,然后根据需要分配内存。

Example for 2D pointer ( 5 X 10 )

2D指针示例(5 X 10)

#define ROW 5
#define COLUMN 10
main()
{
  unsigned char **p = NULL, colum = 0;
  p = malloc ( ROW * sizeof ( unsigned char *) );
  for (;colum< ROW; ++colum )
  {
    p[colum] = malloc (COLUMN * sizeof (unsigned char  ));
  }
}

#2


0  

What you are doing is called buffer overflow by writing beyond the bounds of memory allocated by malloc call. The compiler doesn't do bounds checking (it assumes you know what you are doing, and you only pay for what you use) and allow you to compile and run. However, it will lead to undefined behaviour and your program may crash. You shouldn't rely on such behaviour.

你正在做的是通过写入超出malloc调用分配的内存范围来调用缓冲区溢出。编译器不进行边界检查(它假定您知道自己在做什么,并且只需为使用的内容付费)并允许您编译和运行。但是,它将导致未定义的行为,您的程序可能会崩溃。你不应该依赖这种行为。

You, the programmer, has to make sure that you don't do illegal memory access. You should not cast the result of malloc. Also, malloc can fail to allocate memory in which case it returns NULL, the null pointer, which you should take care of. You can combine the two statements into one.

程序员必须确保不进行非法内存访问。你不应该转换malloc的结果。此外,malloc可能无法分配内存,在这种情况下它会返回NULL,即空指针,您应该注意它。您可以将两个语句合并为一个。

int length = 8; // you can also use a macro
char *arr = malloc(length * sizeof *arr);
if(arr) {
    // malloc call successful
    // do stuff with arr
} 

#1


1  

This line arr = (char*)malloc (2 * sizeof (char)); will allocate memory for 2 bytes only. But you are overwriting the memory by accessing the more 8 or more than 8 byes. If you access more than two byes means, it will give some unpredictable issue. In case you want more memory please follow the below code.

这行arr =(char *)malloc(2 * sizeof(char));将仅为2个字节分配内存。但是你通过访问8个或8个以上的字节来覆盖内存。如果你访问两个以上的byes方法,它将给出一些不可预测的问题。如果您想要更多内存,请遵循以下代码。

#define USER_SIZE 10
arr = (char*)malloc ( USER_SIZE * sizeof (char));

Assign the value in USER_SIZE macro and then allocate the memory as much as you want.

在USER_SIZE宏中分配值,然后根据需要分配内存。

Example for 2D pointer ( 5 X 10 )

2D指针示例(5 X 10)

#define ROW 5
#define COLUMN 10
main()
{
  unsigned char **p = NULL, colum = 0;
  p = malloc ( ROW * sizeof ( unsigned char *) );
  for (;colum< ROW; ++colum )
  {
    p[colum] = malloc (COLUMN * sizeof (unsigned char  ));
  }
}

#2


0  

What you are doing is called buffer overflow by writing beyond the bounds of memory allocated by malloc call. The compiler doesn't do bounds checking (it assumes you know what you are doing, and you only pay for what you use) and allow you to compile and run. However, it will lead to undefined behaviour and your program may crash. You shouldn't rely on such behaviour.

你正在做的是通过写入超出malloc调用分配的内存范围来调用缓冲区溢出。编译器不进行边界检查(它假定您知道自己在做什么,并且只需为使用的内容付费)并允许您编译和运行。但是,它将导致未定义的行为,您的程序可能会崩溃。你不应该依赖这种行为。

You, the programmer, has to make sure that you don't do illegal memory access. You should not cast the result of malloc. Also, malloc can fail to allocate memory in which case it returns NULL, the null pointer, which you should take care of. You can combine the two statements into one.

程序员必须确保不进行非法内存访问。你不应该转换malloc的结果。此外,malloc可能无法分配内存,在这种情况下它会返回NULL,即空指针,您应该注意它。您可以将两个语句合并为一个。

int length = 8; // you can also use a macro
char *arr = malloc(length * sizeof *arr);
if(arr) {
    // malloc call successful
    // do stuff with arr
}