在C动阵列 - 是我的malloc / realloc的的理解是否正确? [英] Dynamic Array in C - Is my understanding of malloc/realloc correct?

查看:211
本文介绍了在C动阵列 - 是我的malloc / realloc的的理解是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在C中创建动态一维数组下code试图做到这一点:

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


  1. 创建长度为10的动态数组,保存双打,使用
    malloc的。

  2. 数组中的每个条目设置为焦耳/ 100 对于j = 0,1,...,9。然后打印
    出来。

  3. 使用realloc的添加额外的空项到数组的结尾。

  4. 将新条目焦耳/ 100 ,再次打印出每个条目。

  1. Create a dynamic array of length 10, that holds doubles, using malloc.
  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?

2)教程,我发现使用的malloc 不把(双*)在前面。例如。

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

int *pointer;

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

这不编译我的Visual Studio 2010中,Windows 7的错误是void类型的值不能被分配​​到int类型的实体。

This does not compile for me on Visual Studio 2010, Windows 7. Error is "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年版标准至少),的malloc 前演员和的realloc 是不必要的,因为C可转换类型的值无效* 为int * 没有一个演员。这是的的真正的C ++,所以根据你得到的错误,它听起来就像你编写本code和C ++而不是C.检查VS2010的文档以确定如何编译code为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 preferred风格电话:

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

由于前pression类型 *数据双击 * sizeof的数据等同于的sizeof(双)。这也意味着您不必调整的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
}

要知道,微软的支持C与1989年版的语言结束;已经有自那时以来,语言标准的两个版本,已引入了一些新的特点和德precated旧的。因此,虽然一些C编译器支持C99的功能,如混合声明和code,变长数组等,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天全站免登陆