0

I am working on a task for the university, there is a webiste that checks my memory usage and it compiles the .c files with:

 /usr/bin/gcc -DEVAL -std=c11 -O2 -pipe -static -s -o program programname.c -lm

and it says my program exceeds the memory limits of 4 Mib which is a lot i think. I was told this command makes it use more memory that the standard compilation I use on my pc, like this:

 gcc myprog.c -o myprog

I launched the executable created by this one compilation with:

/usr/bin/time -v ./myprog

and under "maximum resident set size" it says 1708 kilobytes, which should be 1,6 Mibs. So how can it be that for the university checker my program goes over 4 Mibs? I have eliminated all the possible mallocs i have, I just left the essential ones but it still says it goes over the limit, what else should I improve? I'm almost thinking the wesite has an error or something...

3
  • Try compiling your executable with the same options that the site uses and observe the results. Commented Sep 9, 2018 at 16:07
  • @bool3max i used /usr/bin/time -v on the executable created by the website command, the max resident set size resulted 868 kb which is even lower Commented Sep 9, 2018 at 16:16
  • Because -static will put the code of all the libraries your executable depends on in it. Since a C program need the CRT the libc is included. It must be static too so it also includes all its dependencies. See this. Commented Sep 9, 2018 at 16:31

1 Answer 1

1

From GNU GCC Manual, Page 197:

-static On systems that support dynamic linking, this overrides ‘-pie’ and prevents linking with the shared libraries. On other systems, this option has no effect.

If you don't know about the pie flag quoted here, have a look at this section:

-pie Produce a dynamically linked position independent executable on targets that support it. For predictable results, you must also specify the same set of options used for compilation (‘-fpie’, ‘-fPIE’, or model suboptions) when you specify this linker option.

To answer your question: yes is it possible this overhead generated by the static flag, because in that case, the compiler can not do the basic optimization by merging stdlib's code with the one you've produced.

As it was suggested in the comments you shall compile your code with the same flag of the website to have an idea of the real overhead of your program (be sure that your gcc version is the same of the website) and also you shall do some common manual optimization such constant folding, function inlining etc. A good reference to these optimizations could be this one

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.