C语言中的动态数组—我对malloc和realloc的理解正确吗? [英] Dynamic array in C — Is my understanding of malloc and realloc correct?

查看:412
本文介绍了C语言中的动态数组—我对malloc和realloc的理解正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在C语言中创建动态一维数组.下面的代码尝试执行以下操作:

I am learning how to create dynamic 1D arrays in C. The code below tries to do the following:

  1. 使用malloc,创建长度为10的动态数组,其中包含类型为double的值.
  2. j = 0, 1,..., 9的数组的每个条目设置为j/100.然后将其打印出来.
  3. 使用realloc在数组末尾添加一个额外的空条目.
  4. 将新条目设置为j/100,然后再次打印出每个条目.
  1. Using malloc, create a dynamic array of length 10, that holds values of type double.
  2. Set each entry of the array to j/100 for j = 0, 1,..., 9. Then print it out.
  3. Add an additional empty entry to the end of the array using realloc.
  4. Set the new entry to j/100 and print out each entry again.

测试:

 double* data = (double*)malloc(10*sizeof(double));

 for (j=0;j<10;j++)
 {
      data[j]= ((double)j)/100;
      printf("%g, ",data[j]);
 }

 printf("\n");

 data = (double*)realloc(data,11*sizeof(double));

 for (j=0;j<11;j++)
 {
     if (j == 10){ data[j]= ((double)j)/100; }
     printf("%g, ",data[j]);
 }

 free((void*) data);

问题

  1. 我可以对此编码吗?

  1. Am I coding this right?

教程使用malloc而没有将(double*)放在前面.例如,

Tutorials I found use malloc without putting the (double*) in front. E.g.,

int *指针;
指针= malloc(2 * sizeof(int));

int *pointer;
pointer = malloc(2*sizeof(int));

这在Visual Studio 2010,Windows 7上无法为我编译.错误消息是

This does not compile for me on Visual Studio 2010, Windows 7. The error message is

void类型的值不能分配给int类型的实体.

为什么对那些教程不起作用?我猜对了,这是因为在我的示例中,他们使用的编译器会自动为他们填充(int*)吗?

Why does it work for those tutorials and not for me? Am I right to guess that it is because the compilers they are using automatically fill in the (int*) for them in my example?

推荐答案

您已经关闭.

在C中(至少从1989年版本的标准开始),不需要mallocrealloc进行强制转换,因为C可以将类型void *的值转换为int *而无需强制转换.对于C ++,这不是 ,因此,根据收到的错误,这听起来像是您将此代码编译为C ++而不是C.请查看VS2010的文档以确定如何编译代码作为C.

In C (at least since the 1989 version of the standard), the cast before malloc and realloc is unnecessary, since C can convert values of type void * to int * without a cast. This is not true for C++, so based on the error you're getting, it sounds like you're compiling this code as C++ and not C. Check the documentation for VS2010 to determine how to compile code as C.

以下是我编写malloc调用的首选样式:

The following is my preferred style for writing a malloc call:

double *data = malloc(10 * sizeof *data);

由于表达式*data的类型为double,因此sizeof *data等效于sizeof (double).这也意味着,如果data的类型发生更改,则不必调整malloc调用.

Since the type of the expression *data is double, sizeof *data is equivalent to sizeof (double). This also means you don't have to adjust your malloc calls if the type of data changes.

对于realloc调用,将结果分配给临时指针值更安全. realloc如果无法扩展缓冲区,将返回NULL,因此更安全地写入

As for the realloc call, it's safer to assign the result to a temporary pointer value. realloc will return NULL if it cannot extend the buffer, so it's safer to write

double *tmp;
...
tmp = realloc(data, 11 * sizeof *data);
if (!tmp)
{
  // could not resize data; handle as appropriate
}
else
{
  data = tmp;
  // process extended buffer
}

请注意,Microsoft对C的支持以该语言的1989年版本结束.从那时起,对语言标准进行了两次修订,引入了一些新功能,而弃用了旧功能.因此,尽管某些C编译器支持混合声明和代码,可变长度数组等C99功能,但VS2010不会.

Be aware that Microsoft's support for C ends with the 1989 version of the language; there have been two revisions of the language standard since then, which have introduced some new features and deprecated old ones. So while some C compilers support C99 features like mixed declarations and code, variable length arrays, etc., VS2010 will not.

这篇关于C语言中的动态数组—我对malloc和realloc的理解正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆