free()函数没有malloc或calloc

时间:2021-07-30 07:21:51

quick question

简单的问题

Can you use the free() function without having to prior call a malloc ??

可以使用free()函数而不必事先调用malloc吗?

ei.

ei。

void someFunc( void )
{
   char str[6] = {"Hello"};

   //some processing here ....

   free(str);
}

I get no compiling errors but Does this work or is it correct at all ?

我没有编译错误,但是这个有用吗?还是完全正确?

Thank you,

谢谢你!

5 个解决方案

#1


9  

This is not at all correct:

这一点也不正确:

  1. You cannot free a static array such as char str[6].
  2. 不能释放静态数组,如char str[6]。
  3. free() should only be called on memory you allocated (or on NULL).
  4. free()应该只在分配的内存(或NULL)上调用。

#2


5  

When you call malloc() or any other allocation function, memory will be allocated on the heap. This is the only memory that can be freed. When you declare a static string, as you've done in your example, the string is allocated at compile time in another memory segment. The same goes for the str pointer itself which is allocated on the stack, and thus cannot be freed either.

当您调用malloc()或任何其他分配函数时,内存将被分配到堆上。这是唯一可以释放的内存。当您声明一个静态字符串时,就像您在示例中所做的那样,该字符串在编译时被分配到另一个内存段中。在堆栈上分配的str指针本身也是如此,因此也不能释放它。

#3


3  

Using free on a non-malloc'd variable will result in a Segfault generally. Example:

在非malloc的变量上使用free通常会导致分段错误。例子:

#include <stdlib.h>

int main()
{
  char str[6] = {"Hello"};
  free(str);
}

$ gcc test.c -o test

美元gcc测试。c - o测试

$ ./test

美元/测试。

Segmentation fault

段错误

#4


2  

free() uses data prepended to the allocated block to manage the heap. If the memory pointed to was not allocated by a heap allocation function such as malloc() or calloc(), then the data preceeding the block will be meaningless as heap management data.

free()使用预置到分配块的数据来管理堆。如果指向的内存不是由诸如malloc()或calloc()之类的堆分配函数分配的,那么块前面的数据作为堆管理数据将毫无意义。

Some libraries will detect invalid heap data and yieled a runtime error, otherwise the behaviour is undefined. Often the consequences of such an error will remain unnoticed until you later attempt to allocate further memory. This can make debugging such errors very difficult.

一些库将检测无效的堆数据并产生运行时错误,否则行为未定义。通常,这样的错误的结果将被忽略,直到您稍后尝试分配更多的内存。这将使调试此类错误变得非常困难。

You would not get a compiler error because it is not a syntactic error and is not detectable at compile time. The compiler has no knowledge of the semantics of library functions. All it knows is that malloc() returns a void* and that free() accepts a void*; there is no way of knowing at compile time whether the pointer refers to a dynamically allocated block because the memory is by definition allocated at runtime. Also a pointer may be modified at runtime to point to any memory type, or may be aliased - copied to another pointer and then free'd through the second pointer. You expect a lot of the compiler if you expect an error message; however some static analysis tools may be able to warn if such an error may occur, and dynamic analysis tools such as valgrind may detect the error when and if it actually occurs during testing.

您不会得到编译器错误,因为它不是语法错误,并且在编译时不能检测到。编译器对库函数的语义一无所知。它只知道malloc()返回一个void*,而free()接受一个void*;在编译时无法知道指针是否指向动态分配的块,因为内存是在运行时分配的。此外,一个指针可以在运行时被修改为指向任何内存类型,也可以被别名化——复制到另一个指针,然后通过第二个指针释放。如果您希望得到错误消息,那么您需要很多编译器;然而,一些静态分析工具可以在可能发生错误时发出警告,而动态分析工具,如valgrind,可以在测试过程中发现错误。

#5


0  

No


The free(3) function takes a void * parameter, so you can pass it any sort of pointer without a compile-time error. But bad things will happen if the pointer wasn't originally returned by malloc(3) and never previously given back to free(3).

free(3)函数接受void *参数,因此您可以传递任何类型的指针,而不会出现编译时错误。但是,如果指针最初不是由malloc(3)返回,并且之前从未返回到free(3),那么就会发生糟糕的事情。

#1


9  

This is not at all correct:

这一点也不正确:

  1. You cannot free a static array such as char str[6].
  2. 不能释放静态数组,如char str[6]。
  3. free() should only be called on memory you allocated (or on NULL).
  4. free()应该只在分配的内存(或NULL)上调用。

#2


5  

When you call malloc() or any other allocation function, memory will be allocated on the heap. This is the only memory that can be freed. When you declare a static string, as you've done in your example, the string is allocated at compile time in another memory segment. The same goes for the str pointer itself which is allocated on the stack, and thus cannot be freed either.

当您调用malloc()或任何其他分配函数时,内存将被分配到堆上。这是唯一可以释放的内存。当您声明一个静态字符串时,就像您在示例中所做的那样,该字符串在编译时被分配到另一个内存段中。在堆栈上分配的str指针本身也是如此,因此也不能释放它。

#3


3  

Using free on a non-malloc'd variable will result in a Segfault generally. Example:

在非malloc的变量上使用free通常会导致分段错误。例子:

#include <stdlib.h>

int main()
{
  char str[6] = {"Hello"};
  free(str);
}

$ gcc test.c -o test

美元gcc测试。c - o测试

$ ./test

美元/测试。

Segmentation fault

段错误

#4


2  

free() uses data prepended to the allocated block to manage the heap. If the memory pointed to was not allocated by a heap allocation function such as malloc() or calloc(), then the data preceeding the block will be meaningless as heap management data.

free()使用预置到分配块的数据来管理堆。如果指向的内存不是由诸如malloc()或calloc()之类的堆分配函数分配的,那么块前面的数据作为堆管理数据将毫无意义。

Some libraries will detect invalid heap data and yieled a runtime error, otherwise the behaviour is undefined. Often the consequences of such an error will remain unnoticed until you later attempt to allocate further memory. This can make debugging such errors very difficult.

一些库将检测无效的堆数据并产生运行时错误,否则行为未定义。通常,这样的错误的结果将被忽略,直到您稍后尝试分配更多的内存。这将使调试此类错误变得非常困难。

You would not get a compiler error because it is not a syntactic error and is not detectable at compile time. The compiler has no knowledge of the semantics of library functions. All it knows is that malloc() returns a void* and that free() accepts a void*; there is no way of knowing at compile time whether the pointer refers to a dynamically allocated block because the memory is by definition allocated at runtime. Also a pointer may be modified at runtime to point to any memory type, or may be aliased - copied to another pointer and then free'd through the second pointer. You expect a lot of the compiler if you expect an error message; however some static analysis tools may be able to warn if such an error may occur, and dynamic analysis tools such as valgrind may detect the error when and if it actually occurs during testing.

您不会得到编译器错误,因为它不是语法错误,并且在编译时不能检测到。编译器对库函数的语义一无所知。它只知道malloc()返回一个void*,而free()接受一个void*;在编译时无法知道指针是否指向动态分配的块,因为内存是在运行时分配的。此外,一个指针可以在运行时被修改为指向任何内存类型,也可以被别名化——复制到另一个指针,然后通过第二个指针释放。如果您希望得到错误消息,那么您需要很多编译器;然而,一些静态分析工具可以在可能发生错误时发出警告,而动态分析工具,如valgrind,可以在测试过程中发现错误。

#5


0  

No


The free(3) function takes a void * parameter, so you can pass it any sort of pointer without a compile-time error. But bad things will happen if the pointer wasn't originally returned by malloc(3) and never previously given back to free(3).

free(3)函数接受void *参数,因此您可以传递任何类型的指针,而不会出现编译时错误。但是,如果指针最初不是由malloc(3)返回,并且之前从未返回到free(3),那么就会发生糟糕的事情。