開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第8章(UNIXシステム・インタフェース)、8.6(例 - 記憶割当て)、演習8-6を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 8-6.
コード
sample.c
int main(int artc, char *argv[])
{
return 0;
}
#include <unistd.h>
#include <stdlib.h>
#undef calloc
void *calloc(unsigned n, unsigned size)
{
unsigned nbytes, i;
char *p, *q;
n = n * size;
if ((p = q = malloc(nbytes)) != NULL) {
for (i = 0; i < nbytes; i++) {
*p++ = 0;
}
}
return q;
}
入出力結果(Terminal)
$ cc sample.c
sample.c:10:7: warning: incompatible redeclaration of library function 'calloc'
void *calloc(unsigned n, unsigned size)
^
/usr/include/stdlib.h:157:7: note: 'calloc' is a builtin with type
'void *(size_t, size_t)'
void *calloc(size_t, size_t);
^
1 warning generated.
$
警告は出たけどエラーは出ず、とりあえずコンパイルできた。#undefで標準ライブラリ関数のcallocと名前が衝突しないようにしたつもりだったけど出来てないみたい…
0 コメント:
コメントを投稿