将值附加到动态数组的末尾
这个寒假我一直在学习一点 C 语言,在我的冒险中我偶然发现了动态数组的问题。
这确实是一个相当简单的程序。我想做的是创建一个包含斐波那契数列数字的数组。这是代码:
#include <stdio.h>
#include <stdlib.h>
int dynamic_arry_append(int* arry, int* number, int* size);
int main() {
int i, n, size = 3, *arry = NULL, fibarr[size];
printf("Dynamic array, Fibonacci series. \n");
printf("Capture upto element: ");
scanf("%d", &n);
i = 0;
// passing the first elements
fibarr[0] = 0;
fibarr[1] = 1;
fibarr[2] = 1;
while ( i < n ) {
printf("**%d\n",fibarr[0]);
dynamic_arry_append( arry, &fibarr[0], &size );
fibarr[0] = fibarr[1];
fibarr[1] = fibarr[2];
fibarr[2] = fibarr[1] + fibarr[0];
i++;
}
for ( i = 0 ; i < size ; i++)
printf("Element %d of the array: %d.\n", i, arry[i]);
return 0;
}
int dynamic_arry_append(int* arry, int* number, int* size) {
int i;
int bacon = *size; // first name i thought of
bacon++;
int *new_addr = realloc(arry, bacon * sizeof(int));
if( new_addr != NULL ) {
arry = new_addr;
arry[bacon-1] = *number;
// printf for easier debugging, or so i thought
for ( i = 0 ; i < bacon ; i++ )
printf("%d\t%d\n", i+1, arry[i]);
printf("\n");
*size = bacon;
} else {
printf("Error (re)allocating memory.");
exit (1);
}
return 0;
}
至少在我看来这是有效的。然而,在实践中我得到了有趣的结果:
Dynamic array, Fibonacci series.
Capture upto element: 5
**0 // next fibonacci number
1 5256368
2 5246872
3 1176530273
4 0
**1
1 5256368
2 5246872
3 1768053847
4 977484654
5 1
**1
1 5256368
2 5246872
3 1551066476
4 1919117645
5 1718580079
6 1
**2
1 5256368
2 5246872
3 977484645
4 1852397404
5 1937207140
6 1937339228
7 2
**3
1 5256368
2 5246872
3 1551071087
4 1953724755
5 842231141
6 1700943708
7 977484653
8 3
/* Code::Blocks output */
Process returned -1073741819 (0xC0000005) execution time : 17.886 s
Press any key to continue.
我真的对这个错误感到困惑,经过搜索后我没有找到解决方案......任何人都可以帮忙吗?非常感谢。
Well I have been studying a little C this winter break and in my adventures I stumbled upon an issue with a Dynamic Array.
It's a fairly simple program really. What I am trying to do is to create an array that holds the numbers of the Fibonacci series. Here is the code:
#include <stdio.h>
#include <stdlib.h>
int dynamic_arry_append(int* arry, int* number, int* size);
int main() {
int i, n, size = 3, *arry = NULL, fibarr[size];
printf("Dynamic array, Fibonacci series. \n");
printf("Capture upto element: ");
scanf("%d", &n);
i = 0;
// passing the first elements
fibarr[0] = 0;
fibarr[1] = 1;
fibarr[2] = 1;
while ( i < n ) {
printf("**%d\n",fibarr[0]);
dynamic_arry_append( arry, &fibarr[0], &size );
fibarr[0] = fibarr[1];
fibarr[1] = fibarr[2];
fibarr[2] = fibarr[1] + fibarr[0];
i++;
}
for ( i = 0 ; i < size ; i++)
printf("Element %d of the array: %d.\n", i, arry[i]);
return 0;
}
int dynamic_arry_append(int* arry, int* number, int* size) {
int i;
int bacon = *size; // first name i thought of
bacon++;
int *new_addr = realloc(arry, bacon * sizeof(int));
if( new_addr != NULL ) {
arry = new_addr;
arry[bacon-1] = *number;
// printf for easier debugging, or so i thought
for ( i = 0 ; i < bacon ; i++ )
printf("%d\t%d\n", i+1, arry[i]);
printf("\n");
*size = bacon;
} else {
printf("Error (re)allocating memory.");
exit (1);
}
return 0;
}
At least in my mind this works. However, in practice I get funny results:
Dynamic array, Fibonacci series.
Capture upto element: 5
**0 // next fibonacci number
1 5256368
2 5246872
3 1176530273
4 0
**1
1 5256368
2 5246872
3 1768053847
4 977484654
5 1
**1
1 5256368
2 5246872
3 1551066476
4 1919117645
5 1718580079
6 1
**2
1 5256368
2 5246872
3 977484645
4 1852397404
5 1937207140
6 1937339228
7 2
**3
1 5256368
2 5246872
3 1551071087
4 1953724755
5 842231141
6 1700943708
7 977484653
8 3
/* Code::Blocks output */
Process returned -1073741819 (0xC0000005) execution time : 17.886 s
Press any key to continue.
I am really baffled by this error, and after searching around I found no solution...Can anyone help? Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注意事项:
main
并存储在pointer-to-int
中(或)传递pointer-to -pointer-to-int
并在重新分配后相应地更新一次fibarr
。它不能解决任何问题。size
和number
。只需发送size
,它就会选择n-1
和n-2
来计算n
。效率极低
。因为如果您知道n
,那么您可以一次性为n
个整数分配内存并计算 fib 系列。Points to note:
main
and stored in apointer-to-int
(or) passpointer-to-pointer-to-int
and update it accordingly once after reallocingfibarr
is not needed. It doesn't solve any problem.size
and thenumber
. Just send thesize
and it will pick then-1
andn-2
to calculaten
.highly inefficient
. Because if you know then
then you can allocate memory forn
integers in one shot and calculate the fib series.问题可能是
arry
指针变量按值传递给函数dynamic_arry_append
。这意味着,您对该函数内的 arry 变量所做的更改不会被该函数外部的任何变量反映。例如:The problem may be that the
arry
pointer variable is passed by value to the functiondynamic_arry_append
. That means, that changes that you make to thearry
variable within that function will not be reflected by any variables outside of that function. For example:您应该将
fibarr
声明为指针(因此以不同的方式命名)而不是数组。您应该将该指针的地址传递给dynamic_arry_append
,例如&fibarr
。并且您应该使用calloc
在main
中初始化fibarr
。最后,您应该动态更新(并保留和传递)分配的数组的大小。You should declare your
fibarr
as a pointer (so name it differently) not an array. And you should pass to yourdynamic_arry_append
the address of that pointer, like&fibarr
. And you should initializefibarr
in yourmain
withcalloc
. At last you should dynamically update (and keep, and pass) the size of the allocated array.您没有返回数组的新地址......并且您正在读取/写入而不是您的内存。在调试器下运行包含所有错误消息的程序,您将看到问题出在这一行:
You are not returning the new address of the array... and you are reading/writing not your memory. Run the program with all error messages under debugger and you'll see the problem is in this line: