c - 遇到内存泄漏问题
问题描述
我试图通过为 2 个元素分配空间来生成斐波那契数列,所以我需要我的数组a[0]
并a[1]
不断更新,直到它输出 89 作为最终数字。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
void fib2(int* a);
int main()
{
int *pointer;
//allocates space for 2 elements for pointer
pointer = (int*)malloc(2 * sizeof(int*));
//prints first two fibonacci values
printf("0 1 ");
//calls fib2 func and apsses pointer into it
fib2(pointer);
//frees pointer memory
free(pointer);
printf("\n");
return 0;
}
//generates fibonacci sequence
void fib2(int* a)
{
int i;
//allocates space for 2 elements
a = (int*)malloc(2 * sizeof(int*));
//initial fibonacci array initialized
a[0] = 0;
a[1] = 1;
//generates and calculates fibonacci sequence and prints
for(i = 2; i < 12; i++)
{
a[i] = a[i - 1] + a[i - 2];
printf("%d ", a[i]);
}
}
a[]
我尝试通过做来释放,free(a);
但它像这样输出到控制台
解决方案
有很多问题。
问题 1错误malloc
int *pointer;
//allocates space for 2 elements for pointer
pointer = (int*)malloc(2 * sizeof(int*));
^^^^^^^^^^^^
sizeof
应该是sizeof(int)
您想要为数字 (2) int 分配空间。除此之外,您不需要演员表。更好的写法是:
pointer = malloc(2 * sizeof *pointer);
问题 2你从不pointer
用于任何事情
您确实将其值传递给,fib2
以便其值进入 variable a
。但是,在您这样做之后:
a = (int*)malloc(2 * sizeof(int*)); // also sizeof wrong again
所以你实际上覆盖了传递的任何值。您的电话fib2
也可以是:
fib2(NULL);
换句话说:不要同时做malloc
inmain
和 in fib2
。选择一个地方。
问题 3 malloc'ed in 中的内存fib2
永远不会 free'ed
您当前的代码会泄漏内存,因为fib2
它没有以如下代码结尾:free(a);
问题 4你分配的内存太少
显然你想要整数数组中有 12 个元素,但你只分配!将代码更改为:
a = malloc(12 * sizeof *a);
把事情放在一起,它可能看起来:
#include <stdio.h>
#include <stdlib.h>
void fib2(int* a, int n);
#define NUMBERS_TO_CALCULATE 12
int main()
{
int *pointer;
//allocates space for NUMBERS_TO_CALCULATE elements for pointer
pointer = malloc(NUMBERS_TO_CALCULATE * sizeof *pointer);
if (pointer == NULL) exit(1);
//calls fib2 func and apsses pointer into it
fib2(pointer, NUMBERS_TO_CALCULATE);
// ... use pointer for other things ...
//frees pointer memory
free(pointer);
return 0;
}
//generates fibonacci sequence
void fib2(int* a, int n)
{
int i;
if (n < 2) return;
//initial fibonacci array initialized
a[0] = 0;
a[1] = 1;
//prints first two fibonacci values
printf("0 1 ");
//generates and calculates fibonacci sequence and prints
for(i = 2; i < n; i++)
{
a[i] = a[i - 1] + a[i - 2];
printf("%d ", a[i]);
}
printf("\n");
}
注意:如果您不想pointer
用于 中的其他东西main
,我建议您将malloc
andfree
移入fib2
根据 OP 的评论进行编辑
在评论中,OP 讲述了一些限制,例如:
必须使用
malloc
只允许
malloc
2 个整数(我假设这也意味着不允许使用局部变量fib2
)函数原型必须是
void fib2(int* a)
必须打印小于或等于 89 的值
有了这些限制,程序可能看起来:
#include <stdio.h>
#include <stdlib.h>
void fib2(int* a);
int main()
{
int *pointer;
//allocates space for 2 integer elements for pointer
pointer = malloc(2 * sizeof *pointer);
if (pointer == NULL) exit(1);
//initialize fibonacci start values
pointer[0] = 0;
pointer[1] = 1;
//calls fib2 func and apsses pointer into it
fib2(pointer);
//frees pointer memory
free(pointer);
return 0;
}
//generates fibonacci sequence
void fib2(int* a)
{
//prints first two fibonacci values
printf("%d %d ", a[0], a[1]);
//generates and calculates fibonacci sequence and prints
while(a[1] < 89)
{
a[1] = a[1] + a[0]; // Calculate next number and save in a[1]
printf("%d ", a[1]); // Print it
a[0] = a[1] - a[0]; // Calculate the number for a[0]
}
printf("\n");
}
推荐阅读
- python - 在 tkinter 中创建消息框时禁用窗口控件
- python-3.x - 通过锁定文件来同步同一 python 脚本的 2 个实例
- javascript - 使用 react.js 在社交网络上分享图像
- reactjs - 尝试使用 react-admin 转换功能
- angularjs - SP 发起的 SAML SSO 的实际流程,包括所有组件,即 IDP、SP 客户端和 SP 服务器端
- python - 如何从数据框熊猫创建列表
- ssh - 不会分配伪终端,因为标准输入不是詹金斯管道上的终端
- r - 在 R 中按长格式规范过滤观察结果
- flutter - android studio 不显示 Flutter 项目的异常消息
- r - 在特定时间范围内对 R 中的数据帧进行子集化