title: making your own malloc function in C
body: <p>I need your help in this. I have an average knowledge of C and here is the problem. I am about to use some benchmarks to test some computer architecture stuff (branch misses, cache misses) on a new processor. The thing about it is that benchmarks are in C but I must not include any library calls. For example, I cannot use malloc because I am getting the error</p>
  
  <pre><code>"undefined reference to malloc" 
  </code></pre>
  
  <p>even if I have included the library. So I have to write my own malloc. I do not want it to be super efficient - just do the basics. As I am thinking it I must have an address in memory and everytime a malloc happens, I return a pointer to that address and increment the counter by that size. Malloc happens twice in my program so I do not even need large memory.</p>
  
  <p>Can you help me on that? I have designed a Verilog and do not have so much experience in C.</p>
  
  <p>I have seen previous answers but all seem too complicated for me. Besides, I do not have access to K-R book.</p>
  
  <p>Cheers!</p>
  
  <p>EDIT: maybe this can help you more:
  I am not using gcc but the sde-gcc compiler. Does it make any difference? Maybe that's why I am getting an undefined reference to malloc?</p>
  
  <p>EDIT2:
  I am testing a MIPS architecture:</p>
  
  <p>I have included:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  </code></pre>
  
  <p>and the errors are:</p>
  
  <pre><code>undefined reference to malloc
  relocation truncated to fit: R_MIPS_26 against malloc
  </code></pre>
  
  <p>and the compiler command id:</p>
  
  <pre><code>test.o: test.c cap.h
  sde-gcc -c -o test.s test.c -EB -march=mips64 -mabi=64 -G -O -ggdb -O2 -S
      sde-as -o test.o test.s EB -march=mips64 -mabi=64 -G -O -ggdb
      as_objects:=test.o init.o
  </code></pre>
  
  <p>EDIT 3:
  ok, I used implementation above and it runs without any problems. The problem is that when doing embedded programming, you just have to define everything you are using so I defined my own malloc. sde-gcc didn't recognize the malloc function.</p>