M
M
MrBman2018-12-18 11:58:42
C++ / C#
MrBman, 2018-12-18 11:58:42

How does the C language: 1) implement the concept of "inline" function? 2) Is it possible to implement a common function name across different types?

Hello! I continue to study the C language.
1)
Now I understand macros, and what can be built on their basis. I came across such concepts as built-in, inline functions. If I understand correctly, then the point is that if the function is small enough and does relatively little work, then it would be much more efficient to paste the code of this function in the place where it is used in order to avoid calling the function? Is this task taken over by the compiler itself? If so, how does he implement it?
For example, I could come up with only such a mechanism:

#include <stdio.h>

long unsigned str_len(char const *beg)
{
        char const *end;

        for (end = beg; *end != '\0'; ++end)
                ;
        return end - beg;
}

#define INLINE_STR_LEN(beg, len)                   \
{                                                  \
        char const *end__;                         \
                                                   \
        for (end__ = beg; *end__ != '\0'; ++end__) \
                ;                                  \
        len = end__ - beg;                         \
}

int main(void)
{
        char *str = "Hello, world!";
        long unsigned len;

//      len = str_len(str);
        INLINE_STR_LEN(str, len)
        printf("len = %lu\n", len);

        return 0;
}

It looks so-so, and besides, there are obvious flaws!
How is it correct, and in general, Is it possible to implement this without going into the details of how the compiler works, Or in order to do something like this, you need to know how it works And already on the basis of this write an extension for it?: (((
2)
Again, digging in macros, I decided to use them as templates in order to get functions for use with different types:
It turned out something like this:
fold.c
#include "fold.h"

#define FOLD(N, T, Op)                         \
T N##_##T(T const *a, long unsigned n)         \
{                                              \
        T r;                                   \
                                               \
        for (r = *a++; --n != 0; r Op *a++)    \
                ;                              \
        return r;                              \
}

FOLD(sum    , int   , +=)
FOLD(product, int   , *=)
FOLD(sum    , double, +=)
FOLD(product, double, *=)

fold.h
#ifndef FOLD_H
#define FOLD_H

#define FOLD(N, T) T N##_##T(T const *a, long unsigned n);
        FOLD(sum    , int)
        FOLD(product, int)
        FOLD(sum    , double)
        FOLD(product, double)
#undef  FOLD

#endif

main.c
#include <stdio.h>
#include "fold.h"

#define ISIZE 4
#define DSIZE 5

int main(void)
{
        int    iarr[ISIZE] = { 1  , 2  , 3  , 4 };
        double darr[DSIZE] = { 1.1, 2.2, 3.3, 4.4, 5.5 };

        printf("The sum of iarr: %d\n"    , sum_int       (iarr, ISIZE));
        printf("The sum of darr: %.1f\n"  , sum_double    (darr, DSIZE));
        printf("The product of iarr: %d\n", product_int   (iarr, ISIZE));
        printf("The product of darr: %f\n", product_double(darr, DSIZE));

        return 0;
}

Is it possible to trick me into using functions with the same common name sum everywhere, or is it impossible and I have to use another language?:
sum(iarr, ISIZE);
sum(darr, DSIZE);
P\S The more I dig, the more I come to the conclusion that in order to become a real professional it is not enough just to know the language itself, you need to know how the compiler for this language works! However, if I have enough brains, then I plan to figure it out as soon as I finish with the language itself!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaM, 2018-12-18
@MaM

Haha, you're a funny fellow. In general, inline is an advice to the compiler to inline a function, there are also things specific to the gcc or clan, for example, that require you to do this. But since these people have often abused the compiler sometimes ignores the requirements of the programmer. It is better to use special macros like "hot code" or vice versa "cold" in the specification they are. Regarding the implementation of templates in C, try googling templates in c directly. The technology is as old as the world. In the general case, this method of providing polymorphism is of course used, but it is still better to use some kind of void and avoid generalized C code as much as possible. Most application tasks are covered by things like glib, and macros are often used there with a specific idiom, an example would be glib vector

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question