munmap_chunck():c中的指针无效

munmap_chunck():c中的指针无效,c,pointers,memory-management,global-variables,C,Pointers,Memory Management,Global Variables,我写了一个程序来做一些数据分析,这个数据存储在一个叫做p的全局结构中。我在一个函数中为这个结构分配内存,然后,因为我需要整个程序使用它,所以在main的最后才调用free。Malloc不在main中调用,因为数组的大小是从一个文件中获取的,所以我认为在文件中读取并分配内存是最有意义的,而不是在main中执行所有操作 #include <stdlib.h> #include <stdio.h> typedef struct DATA { /* variables*

我写了一个程序来做一些数据分析,这个数据存储在一个叫做p的全局结构中。我在一个函数中为这个结构分配内存,然后,因为我需要整个程序使用它,所以在main的最后才调用free。Malloc不在main中调用,因为数组的大小是从一个文件中获取的,所以我认为在文件中读取并分配内存是最有意义的,而不是在main中执行所有操作

#include <stdlib.h>
#include <stdio.h>

typedef struct DATA
{
    /* variables*/
} DATA;

DATA *P;
void function1(void);

int main(int argc, char **argv)
{
    function1();
    /*do some stuff*/
    free(P);
    return 0;
}

void function1(void)
{
    if(!(P = (DATA *)malloc(nums * sizeof(DATA))))
    {
        printf("Error!\n");
        exit(EXIT_FAILURE);
    }
    /*do stuff*/
}
#包括
#包括
typedef结构数据
{
/*变数*/
}数据;
数据*P;
无效功能1(无效);
int main(int argc,字符**argv)
{
功能1();
/*做点什么*/
自由基(P);
返回0;
}
无效功能1(无效)
{
如果(!(P=(数据*)malloc(nums*sizeof(数据)))
{
printf(“错误!\n”);
退出(退出失败);
}
/*做事*/
}

本质上,当我运行代码时,我得到了错误:munmap_chunck():无效指针。我做了一些阅读,它似乎与函数无关。我还读过,malloc和free应该在同一个函数中调用。如果是这样的话,那么我的问题是:既然P是一个全局变量,为什么为这个特定变量调用哪个函数malloc和free很重要?事实上,如果问题不是由malloc和free在不同的函数中被调用引起的,那么有人对它可能是什么有什么建议吗?多谢各位

这是一个常见问题,当您提供的
空闲
指针不指向已分配内存块的开始时会出现此问题。例如,代码

int* something = malloc(sizeof(int)); // Allocate space for 1 int
...
free(something);
将正常工作,因为您使用
malloc
返回的原始指针提供
free
。但是,如果您这样做:

int* something = malloc(sizeof(int) * 5); // Allocate space for 5 ints
...
something += sizeof(int); // Shift the allocated memory by 1 int
...
free(something);
#include <stdlib.h>
#include <stdio.h>

typedef struct DATA
{
    /* variables*/
} DATA;

/* Declare another DATA*, P1 */
DATA *P, *P1;
void function1(void);

int main(int argc, char **argv)
{
    function1();
    /*do some stuff*/

    /* Free the original pointer, in P1 */
    free(P1);
    return 0;
}

void function1(void)
{
    if(!(P = (DATA *)malloc(nums * sizeof(DATA))))
    {
        printf("Error!\n");
        exit(EXIT_FAILURE);
    }

    /* Store the original value of `P` in P1 */
    P1 = P;

    /*do stuff*/
}
<> >代码>免费<代码>已经提供了指向分配内存块中间某个位置的指针。这意味着基本上,
free
不知道块的开始或结束位置,因此抛出错误。可以通过将原始指针值存储在另一个指针中来解决此问题:

int* something = malloc(sizeof(int) * 5);
int* another_something = something; // `another_something` contains the same pointer value as `something`
...
something += sizeof(int); // `something` changes, `another_something` doesn't
...
free(another_something); // Free the original pointer
那么,在你的情况下,类似这样的事情:

int* something = malloc(sizeof(int) * 5); // Allocate space for 5 ints
...
something += sizeof(int); // Shift the allocated memory by 1 int
...
free(something);
#include <stdlib.h>
#include <stdio.h>

typedef struct DATA
{
    /* variables*/
} DATA;

/* Declare another DATA*, P1 */
DATA *P, *P1;
void function1(void);

int main(int argc, char **argv)
{
    function1();
    /*do some stuff*/

    /* Free the original pointer, in P1 */
    free(P1);
    return 0;
}

void function1(void)
{
    if(!(P = (DATA *)malloc(nums * sizeof(DATA))))
    {
        printf("Error!\n");
        exit(EXIT_FAILURE);
    }

    /* Store the original value of `P` in P1 */
    P1 = P;

    /*do stuff*/
}
#包括
#包括
typedef结构数据
{
/*变数*/
}数据;
/*声明另一个数据*,P1*/
数据*P,*P1;
无效功能1(无效);
int main(int argc,字符**argv)
{
功能1();
/*做点什么*/
/*在P1中释放原始指针*/
自由(P1);
返回0;
}
无效功能1(无效)
{
如果(!(P=(数据*)malloc(nums*sizeof(数据)))
{
printf(“错误!\n”);
退出(退出失败);
}
/*将'P'的原始值存储在P1中*/
P1=P;
/*做事*/
}

最后,请修正您的命名约定
P
function1
都是可怕的名字。

你不需要在C中投射
malloc
的结果。
P=malloc(nums*sizeof(DATA))
很好。哦,好的,酷。我一直被教导这样做是好的,但老实说,我从来没有真正想过为什么。我得调查一下,谢谢!太好了,谢谢!这似乎已经解决了问题。至于名字,他们是可怕的名字,而不是我实际使用的lol,我只是为了让这个尽可能简单。