LoginSignup
4
1

More than 3 years have passed since last update.

C言語 mallocで確保していない領域に対してfree()を使うと出るエラーについて

Last updated at Posted at 2020-03-01

mallocしてない領域をfree()すると出てくるエラー

malloctest.out(24922,0x1000cddc0) malloc: *** error for object 0x7ffeefbff5b0: pointer being freed was not allocated
malloctest.out(24922,0x1000cddc0) malloc: *** set a breakpoint in malloc_error_break to debug

変数の宣言直後にNULLで初期化すると上記のエラーはでなくなる。
NULLはfree()でスキップされるから?

List *a;
a = NULL;

free()を2重で行っても同様のエラーが出る。
確保してない領域に対してfree()を行うと出てくるエラーだと理解。

malloctest.out(25177,0x1000cddc0) malloc: *** error for object 0x100201bf0: pointer being freed was not allocated
malloctest.out(25177,0x1000cddc0) malloc: *** set a breakpoint in malloc_error_break to debug

このエラーは確保されていない領域に対して、確保されている前提で処理を行うと出てくるエラー。free()で解放した後の領域に対して、値を格納しても同じエラーが出ます。

参考

【C言語】mallocが原因と思われるエラーに対して - 現在C言語... - Yahoo!知恵袋
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q10116060110

4
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1