Archived

This topic is now archived and is closed to further replies.

Laverok

About inline-type-stuff and arrays

Recommended Posts

I read somewhere about inline functions, procedures, (I can''t remember anyway) and I was wondering, when do you use it and how? I think it does something like make the program run faster at the exspense of some space or memory or both. Do any of you out there use it? Another question: Let us assume (for the sake of my question) that a string can only be 100 characters long. OK. Now what would use less memory: A string called, say LINE, or A char array, like this: char line[100] uh... yeah, I think that''s about it. thanx.

Share this post


Link to post
Share on other sites
Usually a function call translates into a machine''s "call" instruction and a bunch of stack manipulation instructions. To avoid that kind of overhead, functions can be declared inline. Inline functions have their code built directly into the caller''s code, so no "call" instructions and the stack need not be mucked with.

It can increase the size of your code if there''s more than one call to that function. It''s generally only useful in tight inner loops that get called hundreds or thousands of times in a row.

As for your second question, I can only assume you mean STL strings. Anyway, char line[ 100 ] will use exactly 100 bytes (plus any padding, which only increases performance, but you won''t typically have to deal with that). An STL string will have to allocate 100 bytes for the string, plus any other member data which makes other things faster (like a running total of the size of its contents).

I like pie.

Share this post


Link to post
Share on other sites
An inline function is expanded in place at its point of call. For example, suppose you have a class that has private members. Because users of the class cannot access the memory directly we need to declare public methods to provide users access to these objects.

class string
{
public:
    ...
    std::size_t size() const;
    ...
private:
    std::size_t _size;
    ...
};

std::size_t string::size()
{
    return _size;
}

Consider this:

string str(...);

int str_size = str.size();

and then suppose _size was public:

int str_size = str._size;

The first intialisation of of str_size requires a function to be invoked, whilst the second is merely a direct memory access. Generally, a function call is significantly more expensive than a direct memory access.

In this case, does encapsulation impose a significant expense? No. Inline functions, as mentioned before, expand in place at their points of call. So, with our string class:

for (int i = 0; i < str.size(); ++i)
    // ...

size() is not actually invoked _size times, it is instead inline-expanded during compilation into something along the lines of this:

for (int i = 0; i < str._size; ++i)
    // ...


If you define methods within the class definition they are treated as inline functions. Alternatively, the inline keyword can be used. So, with our string class we would do something like so:

class string
{
public:
    ...
    std::size_t size() const    // function inlined
    {
        return _size;
    }
    ...
private:
    ...
    std::size_t _size;
    ...
};

Bare in mind that it is a request to the compiler and so it is not guaranteed to be inlined.

As for your second question, I am not really sure what you are asking. When you say a "string", do you mean std::string? A std::string allocates only as much memory as it needs, so if you construct or assign it with a string less than 100 characters long, it will allocate exactly how much you need, and so will take up less memory than a static character array of dimension 100. If, though, you assign in a string of more than 100 characters, it will, again, allocate exactly the right amount, but this will be more than the character array, line.

char c_line[100] = "I am a string"; // 14 characters used, 86 "wasted"
std::string str_line = "I am a string"; // 14 characters allocated

In short, if you are using C++, use std::string. It is generally more efficient and easier to use.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 25, 2003 7:55:43 PM]

Share this post


Link to post
Share on other sites