こんな人生が嫌になった
typedef struct image_t { int width; int height; char *px; } image_t; int get_image_size(image_t *img) { return img->width * img->height; } int init_image(image_t *img, int width, int height) { img->width = width; img->height = height; img->px = malloc(get_image_size(img)); return img->px != NULL; }
↓ 微妙にC++ wayに近づけた結果がこれだよ
class Image { int width; int height; char *px; public: int size() { return width * height; } void init(int width, int height) { width = width; ← オワタ height = height; px = malloc(size()); } };
こんな人間失格の僕にもValgrindだけは普段と変わらぬ素振りで声をかけてくれる:
==8955== Thread 2: ==8955== Invalid write of size 1 ==8955== at 0x55B602: ○○::△△::ほげ(char const*, ○○::△△::もげ*, ○○::△△::ぽぺ*, int) (もげ.cxx:435) ==8955== by 0x56F8B6: ○○::ぽにょ::init() (ぽにょ.cxx:84) ==8955== by 0x531A09: ふが (はひ.cxx:2373) ==8955== by 0x58F5B1: ○○::ぷぴ(void*) (くにゃ.cxx:130) ==8955== by 0x3A8AC0673C: start_thread (in /lib64/libpthread-2.5.so) ==8955== by 0x3A89CD3D1C: clone (in /lib64/libc-2.5.so) ==8955== Address 0x53b54e0 is 0 bytes after a block of size 0 alloc'd ←←「お前,mallocでなんも割り当てできてねーから!」 ==8955== at 0x4A0614F: malloc (vg_replace_malloc.c:236) ==8955== by 0x557F9E: ○○::△△::ぽぺ::ぴぷ(int, int, ○○::△△::こけ, int) (ぽぺ.cxx:64) ==8955== by 0x55B474: ○○::△△::ほげ(char const*, ○○::△△::もげ*, ○○::△△::ぽぺ*, int) (もげ.cxx:389) ==8955== by 0x56F8B6: ○○::ぽにょ::init() (ぽにょ.cxx:84) ==8955== by 0x531A09: ふが (はひ.cxx:2373) ==8955== by 0x58F5B1: ○○::ぷぴ(void*) (くにゃ.cxx:130) ==8955== by 0x3A8AC0673C: start_thread (in /lib64/libpthread-2.5.so) ==8955== by 0x3A89CD3D1C: clone (in /lib64/libc-2.5.so) ==8955== { <insert_a_suppression_name_here> Memcheck:Addr1 fun:_ZN7○○7△△38ほげEPKcPNS0_34もげEPNS0_11ぽぺ fun:_ZN7○○13ぽにょ4initEv fun:ふが fun:_ZN7○○25ふにゅEPv fun:start_thread fun:clone }
もう俺の人生には水とValgrindだけあればいいや... 食欲無いし...
それとVSのデバッガは一刻も早く,関数の返り値を表示できるようになること*1.