Thread: specific compiler ques

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    1

    specific compiler ques

    Lets say I declare two STL list<T> in main:

    list<Animal> a;
    list<Person> p;

    how many copies of the STL list functions will be in your object code?

    My educated guess is two since both are instantiated at compile time..

    Can anyone verify this ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That depends on the quality of your implementation.

    A particularly bad compiler might instantiate everything.

    A particularly good compiler might realise that nothing is actually used and remove everything.

    Eg
    Code:
    $ cat bar.cpp
    #include <iostream>
    #include<cstdlib>
    #include <list>
    #include <ctime>
    using namespace std;
    struct Animal {
    };
    struct Person {
    };
    int main ( ) {
      list<Animal> a;
      list<Person> p;
    }
    
    g++ bar.cpp
    nm -C a.out 
    g++ -O2 bar.cpp
    nm -C a.out
    nm is the Unix tool to tell you what symbol names exist in an object file.
    Specifying -O2 gets a different answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Only functions that are used will be instantiated. In this case, just the constructor and destructor for both objects, four functions total. As Salem points out, even those may not be generated if the variables can be proven to be unused and be removed.

    However, I disagree that looking at the symbol names is a good test, because if the functions are inlined, they will still generate code, but they won't appear in the symbol table.
    Last edited by King Mir; 06-30-2013 at 10:02 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Note that the STL is actual code (as opposed to headers) that is included with your code. This is how the templates can handle type specific objects with what is otherwise generic code.

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by rcgldr View Post
    Note that the STL is actual code (as opposed to headers) that is included with your code. This is how the templates can handle type specific objects with what is otherwise generic code.
    I've honestly tried to understand what you just said there, but I can't make heads or tails of it. Care to rephrase (or set me straight)?

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    Note that the STL is actual code (as opposed to headers) that is included with your code. This is how the templates can handle type specific objects with what is otherwise generic code.
    Quote Originally Posted by antred View Post
    I've honestly tried to understand what you just said there, but I can't make heads or tails of it. Care to rephrase (or set me straight)?
    What I meant was that STL is C++ template source code, instead of prototypes to functions in a library. So if you use a template function from the STL, the code for that template function is getting complied along with your code. I'm not sure how the compiler handles the case where a template function is used multiple times with multiple types, in terms in how many instances of that function (but with different types) your program will end up with.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rcgldr View Post
    ...I'm not sure how the compiler handles the case where a template function is used multiple times with multiple types, in terms in how many instances of that function (but with different types) your program will end up with.
    The rule is that one type = one function, so N types = N functions, unless the compiler can prove that there are essentially semantically equivalent, and so can merge them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ques..??
    By P3st in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2004, 09:41 PM
  2. ques
    By tim in forum C Programming
    Replies: 1
    Last Post: 04-22-2002, 10:06 AM
  3. Got ques
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-07-2002, 02:18 AM
  4. variable sized arrays: compiler specific?
    By dirkduck in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2002, 11:01 PM
  5. compiler specific
    By iain in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 08-12-2001, 08:13 PM