LoginSignup
4
0

More than 3 years have passed since last update.

馬鹿の一つ覚え: Cプログラミングの注意点

Last updated at Posted at 2018-07-19

スタック領域にサイズの大きな配列は確保できない

スタック領域にサイズの大きな配列は確保できないので、ヒープ領域を利用し動的に確保するべきだが、関数の外側に外部変数として宣言するとこの問題をある程度回避することができる。
以下の例では float data[1048576][1024]; を関数の中で初期化すると Segmentation fault (core dumped) が発生する。

#include <stdio.h>
#include <stdlib.h>
float data[1048576][1024];
int main(int argc, char *argv[])
{
  /* float data[1048576][1024]; */  /* Segmentation fault (core dumped) */
  data[128][32] = 2.71828f;
  fprintf(stdout, "%f\n", data[128][32]);
  return EXIT_SUCCESS;
}

負の整数を符号無し整数としてscanした時に起こる問題

マイナスが付いていてもそのまま読み込まれ、符号あり整数値から符号無し整数値に変換されてしまう。

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
  int i;
  unsigned int unsigned_integer[128];
  for (i = 1; i < argc; i++)
    sscanf(argv[i], "%u", &unsigned_integer[i]);
  for (i = 1; i < argc; i++)
    fprintf(stdout, "\t%u", unsigned_integer[i]);
  putchar('\n');
  return EXIT_SUCCESS;
}
$ ./scan_unsigned 0 1 2 4 9 -9 -31
    0   1   2   4   9   4294967287  4294967265

グローバル変数の配列をextern宣言する場合はポインタではなく配列で宣言

グローバル変数の配列をextern宣言する場合、ポインタで宣言してもコンパイルできるがsegmentation faultが発生する。配列は配列としてextern宣言する。

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

int tensor[7][6][5][4];

void set_tensor(unsigned int);

int main(int argc, char *argv[])
{
  if (argc < 2) return EXIT_FAILURE;
  set_tensor((unsigned int)atoi(argv[1]));
  fprintf(stdout, "%d\n", tensor[0][1][2][3]);
  return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>

/* extern int ****tensor; can be compiled, but does not work */
extern int tensor[7][6][5][4];

void set_tensor(unsigned int seed)
{
  int i, j, k, l;
  srand(seed);
  for (i = 0; i < 7; i++)
    for (j = 0; j < 6; j++)
      for (k = 0; k < 5; k++)
        for (l = 0; l < 4; l++)
          tensor[i][j][k][l] = rand();
}

C99では標準でヘッダファイルstdbool.hによりbool型が利用できるようになった

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
  bool test = false;
  if (argc == 2) test = true;
  if (test) fprintf(stdout, "%s\n", argv[1]);
  return EXIT_SUCCESS;
}

Intelコンパイラ (Intel Parallel Studio XE 2016 Update 1 Composer Edition for Fortran and C++ Linux) インストール

$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 
$ ls -l parallel_studio_xe_2016_composer_edition_update1.tgz
-r-------- 1 glire glire 2912617145 Nov 11  2015 parallel_studio_xe_2016_composer_edition_update1.tgz
$ umask 0022
$ tar xvzf parallel_studio_xe_2016_composer_edition_update1.tgz
$ ls -ld parallel_studio_xe_2016_composer_edition_update1*
drwxr-xr-x 4 glire glire       4096 Nov 11  2015 parallel_studio_xe_2016_composer_edition_update1
-r-------- 1 glire glire 2912617145 Nov 11  2015 parallel_studio_xe_2016_composer_edition_update1.tgz
$ cd parallel_studio_xe_2016_composer_edition_update1/
$ ls -l
total 196
-rw-r--r--  1 glire glire  1006 Nov 11  2015 PUBLIC_KEY.PUB
-rwxr-xr-x  1 glire glire  3264 Nov 11  2015 cd_eject.sh
-rwxr-xr-x  1 glire glire 28235 Nov 11  2015 install.sh
-rwxr-xr-x  1 glire glire   180 Nov 11  2015 install_GUI.sh
-rw-r--r--  1 glire glire  1295 Nov 11  2015 ipsxe_support_compxe.txt
-rw-r--r--  1 glire glire 50319 Oct 24  2015 license.txt
-rw-r--r--  1 glire glire 59023 Oct 24  2015 license_ja.txt
drwxr-xr-x 11 glire glire  4096 Nov 11  2015 pset
drwxr-xr-x  2 glire glire 16384 Nov 11  2015 rpm
-rwxr-xr-x  1 glire glire  1664 Nov 11  2015 silent.cfg
$ sudo ./install.sh
$ ls -l /opt/intel/
total 0
drwxr-xr-x 8 root root 132 Apr  5 18:17 ism
drwxr-xr-x 2 root root  27 Apr  5 18:14 licenses
$ ls -l /net/gpfs/opt/intel/
total 17
drwxr-xr-x 2 root root 4096 Apr  5 18:17 bin
lrwxrwxrwx 1 root root   28 Apr  5 18:17 compilers_and_libraries -> compilers_and_libraries_2016
drwxr-xr-x 3 root root 4096 Apr  5 18:17 compilers_and_libraries_2016
drwxr-xr-x 4 root root 4096 Nov 11  2015 compilers_and_libraries_2016.1.150
lrwxrwxrwx 1 root root   34 Apr  5 18:17 daal -> compilers_and_libraries/linux/daal
drwxr-xr-x 7 root root 4096 Apr  5 18:17 debugger_2016
drwxr-xr-x 4 root root 4096 Nov 11  2015 documentation_2016
drwxr-xr-x 3 root root 4096 Nov 11  2015 ide_support_2016
drwxr-xr-x 2 root root 4096 Apr  5 18:15 impi
lrwxrwxrwx 1 root root   37 Apr  5 18:17 include -> compilers_and_libraries/linux/include
lrwxrwxrwx 1 root root   33 Apr  5 18:17 ipp -> compilers_and_libraries/linux/ipp
lrwxrwxrwx 1 root root   33 Apr  5 18:17 lib -> compilers_and_libraries/linux/lib
lrwxrwxrwx 1 root root   33 Apr  5 18:17 man -> compilers_and_libraries/linux/man
lrwxrwxrwx 1 root root   33 Apr  5 18:17 mkl -> compilers_and_libraries/linux/mkl
drwxr-xr-x 5 root root 4096 Apr  5 18:17 parallel_studio_xe_2016.1.056
drwxr-xr-x 4 root root 4096 Nov 11  2015 samples_2016
lrwxrwxrwx 1 root root   33 Apr  5 18:17 tbb -> compilers_and_libraries/linux/tbb
$ ls -l /net/gpfs/opt/intel/bin/
total 23
lrwxrwxrwx 1 root root 57 Apr  5 18:17 codecov -> ../compilers_and_libraries_2016/linux/bin/intel64/codecov
lrwxrwxrwx 1 root root 58 Apr  5 18:17 compilervars.csh -> ../compilers_and_libraries_2016/linux/bin/compilervars.csh
lrwxrwxrwx 1 root root 57 Apr  5 18:17 compilervars.sh -> ../compilers_and_libraries_2016/linux/bin/compilervars.sh
lrwxrwxrwx 1 root root 53 Apr  5 18:17 fpp -> ../compilers_and_libraries_2016/linux/bin/intel64/fpp
lrwxrwxrwx 1 root root 58 Apr  5 18:17 gcore-ia -> ../compilers_and_libraries_2016/linux/bin/intel64/gcore-ia
lrwxrwxrwx 1 root root 59 Apr  5 18:17 gcore-mic -> ../compilers_and_libraries_2016/linux/bin/intel64/gcore-mic
lrwxrwxrwx 1 root root 56 Apr  5 18:17 gdb-ia -> ../compilers_and_libraries_2016/linux/bin/intel64/gdb-ia
lrwxrwxrwx 1 root root 58 Apr  5 18:17 gdb-igfx -> ../compilers_and_libraries_2016/linux/bin/intel64/gdb-igfx
lrwxrwxrwx 1 root root 57 Apr  5 18:17 gdb-mic -> ../compilers_and_libraries_2016/linux/bin/intel64/gdb-mic
lrwxrwxrwx 1 root root 53 Apr  5 18:17 icc -> ../compilers_and_libraries_2016/linux/bin/intel64/icc
lrwxrwxrwx 1 root root 53 Apr  5 18:17 iccvars.csh -> ../compilers_and_libraries/linux/bin/compilervars.csh
lrwxrwxrwx 1 root root 52 Apr  5 18:17 iccvars.sh -> ../compilers_and_libraries/linux/bin/compilervars.sh
lrwxrwxrwx 1 root root 54 Apr  5 18:17 icpc -> ../compilers_and_libraries_2016/linux/bin/intel64/icpc
lrwxrwxrwx 1 root root 55 Apr  5 18:17 ifort -> ../compilers_and_libraries_2016/linux/bin/intel64/ifort
lrwxrwxrwx 1 root root 53 Apr  5 18:17 ifortvars.csh -> ../compilers_and_libraries/linux/bin/compilervars.csh
lrwxrwxrwx 1 root root 52 Apr  5 18:17 ifortvars.sh -> ../compilers_and_libraries/linux/bin/compilervars.sh
lrwxrwxrwx 1 root root 58 Apr  5 18:17 map_opts -> ../compilers_and_libraries_2016/linux/bin/intel64/map_opts
lrwxrwxrwx 1 root root 57 Apr  5 18:17 profdcg -> ../compilers_and_libraries_2016/linux/bin/intel64/profdcg
lrwxrwxrwx 1 root root 59 Apr  5 18:17 profmerge -> ../compilers_and_libraries_2016/linux/bin/intel64/profmerge
lrwxrwxrwx 1 root root 59 Apr  5 18:17 proforder -> ../compilers_and_libraries_2016/linux/bin/intel64/proforder
lrwxrwxrwx 1 root root 57 Apr  5 18:17 tselect -> ../compilers_and_libraries_2016/linux/bin/intel64/tselect
lrwxrwxrwx 1 root root 54 Apr  5 18:17 xiar -> ../compilers_and_libraries_2016/linux/bin/intel64/xiar
lrwxrwxrwx 1 root root 54 Apr  5 18:17 xild -> ../compilers_and_libraries_2016/linux/bin/intel64/xild
$ which gcc
/usr/bin/gcc
$ export PATH=/net/gpfs/opt/intel/bin:$PATH
$ which icc
/net/gpfs/opt/intel/bin/icc
$ icc -v
icc version 16.0.1 (gcc version 4.8.5 compatibility)
$ icc -O -o foo foo.c
$ ls -l foo
-rwxr-xr-x 1 glire glire 22544 Apr  5 18:22 foo
$ file foo
foo: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=bd85569ac5ee887045cd26dfb4822b0f59d05f07, not stripped
4
0
3

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
0