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

查看:61
本文介绍了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/100,因为 j = 0, 1,..., 9.然后打印出来.
  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 类型的实体.

value of type void cannot be assigned to entity of type 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的类型是doublesizeof *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天全站免登陆