0

I am trying to understand the storage allocator program shown in Kernighan and Ritchie's book "The C Programming Language, 2nd edition". I think I understood most, but when I code the program in Windows 8.1 x86_64 with TDM GCC version 5.1.0. It outputs error undefined reference to 'sbrk'. These are my includes:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

I've searched quite a lot, but no other answers have helped me. It is supposed to be inside unistd.h but it's not. Is it because, since it is a system call it cannot be found in Windows? What is wrong?

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
KeyC0de
  • 4,728
  • 8
  • 44
  • 68
  • K&R is mostly based on unix, `sbrk` is Unix system call. I thought `unistd.h` is also Unix only stuff. Did your code compile through it before linking. – dlmeetei Jul 11 '17 at 17:20

1 Answers1

2

Yes, sbrk is a unix system call. It's not available on Windows.

(Side note: Functions themselves aren't inside headers; headers generally only contain declarations. The actual functions are in some library (libc in this case).)

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Other programs of his book use syscalls, such as `open`, `read`, `write` etc. These work on my Win system. But I think it's because Windows has ones with the same name, am i right? – KeyC0de Jul 11 '17 at 17:26
  • @RestlessC0bra Yes, that's correct. See e.g. https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/open for the `open` function on windows. – melpomene Jul 11 '17 at 17:30
  • As I remember K&R malloc implementation in the book (maybe it was later changed) was not a very good example how to write the malloc function. At least it was full or constructions we consider now as a bad programming practice (many returns etc etc). So you can probably skip this example or you should take a look on any modern implementation instead :) – 0___________ Jul 11 '17 at 17:45
  • 2
    You're probably right in general, but "many returns" is a good thing. – melpomene Jul 11 '17 at 17:47