0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

segmentation faultと動的割り当て(mallocとfree)

0
Last updated at Posted at 2023-01-13
更新履歴
  • sample.cの中身を書き換えた。参考にしたのはNUMERICAL RECIPES in C。

憎きsegmentation faultへの対応を記録する。
なんとなくメモリの使いすぎな気はしている。
結局はメモリの動的割り当て(mallocとfree)で解決した。

デバッグ

まずはgdbのインストール。

% brew install gdb
gdb: The x86_64 architecture is required for this software.
Error: gdb: An unsatisfied requirement failed this build.

どうやらM1 Macでgdbが使えなくなっているらしい。
その代わりにこちらを参考にlldbを使う。

% gcc -g -O0 -o hogehoge hogehoge.c -lm
% lldb hogehoge
(lldb) run
...
... stop reason = EXC_BAD_ACCESS
...

なんだかよくわからない。
直感を信じてメモリの使いすぎ(10,000×10,000の配列を使ってるだけなのに)だと思い、
メモリの動的割り当て(mallocとfree)をすることにする。

mallocとfree

mallocとfreeに関しては、NUMERICAL RECIPES in Cという本を使うことをおすすめする。
特に、付録のユーティリティルーチンはとても使いやすい。
例えば、私は以下のように使っている。

sample.c
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define NR_END 1
#define FREE_ARG char*

void nrerror(char error_text[]);
long int *ivector(long nl, long nh);
void free_ivector(long int *v, long nl, long nh);
long int **imatrix(long nrl, long nrh, long ncl, long nch);
void free_imatrix(long int **m, long nrl, long nrh, long ncl, long nch);
double *dvector(long nl, long nh);
void free_dvector(double *v, long nl, long nh);

int main(void)
{
  long int *intonearray, **inttwoarray;
  intonearray = ivector(0,10);
  inttwoarray = imatrix(0,10,0,10);
  double *doubleonearray;
  doubleonearray = dvector(0,10);

  // ここにコードを書く

  free_ivector(intonearray,0,10);
  free_imatrix(inttwoarray,0,10,0,10);
  free_dvector(doubleonearray,0,10);

  return 0;
}

void nrerror(char error_text[])
/* Numerical Recipes standard error handler */
{
  // ここに本の通りのコードを書く
}

long int *ivector(long nl, long nh)
/* allocate an int vector with subscript range v[nl..nh] */
{
  // ここに本の通りのコードを書く
}

void free_ivector(long int *v, long nl, long nh)
/* free an int vector allocated with ivector() */
{
  // ここに本の通りのコードを書く
}

long int **imatrix(long nrl, long nrh, long ncl, long nch)
/* allocate an int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
  // ここに本の通りのコードを書く
}

void free_imatrix(long int **m, long nrl, long nrh, long ncl, long nch)
/* free an int matrix allocated by imatrix() */
{
  // ここに本の通りのコードを書く
}

double *dvector(long nl, long nh)
/* allocate a double vector with subscript range v[nl..nh] */
{
  // ここに本の通りのコードを書く
}

void free_dvector(double *v, long nl, long nh)
/* free a double vector allocated with ivector() */
{
  // ここに本の通りのコードを書く
}

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?