首页 > 解决方案 > 遇到内存泄漏问题

问题描述

我试图通过为 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);但它像这样输出到控制台

在此处输入图像描述

**编辑这是 valgrind 输出 在此处输入图像描述

标签: cvalgrind

解决方案


有很多问题。

问题 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);

换句话说:不要同时做mallocinmain和 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,我建议您将mallocandfree移入fib2

根据 OP 的评论进行编辑

在评论中,OP 讲述了一些限制,例如:

  1. 必须使用malloc

  2. 只允许malloc2 个整数(我假设这也意味着不允许使用局部变量fib2

  3. 函数原型必须是void fib2(int* a)

  4. 必须打印小于或等于 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");
}

推荐阅读