0

I'm working on a project what is becoming more and more complicated. Project consists of independent from each other sections of code containing a lot of math computations followed by complicated output generation. All that is wrapped inside a socket with pthread.

Whole thing looks something like this:

Main() -> socket -> thread -> request processing -> one or more of the independent sections

I wanted to enclose each independent section of code inside a function, and for that function to inherit variables from a caller function, but that didn't work.

Below is simplified version of what I want to achieve:

void func_a(){int c;b+=a;c=1;b+=c;}
int main(){
 int a,b,c;
 a=3;b=2;c=0;
 func_a();
 printf("b:%d c:%d\n",b,c);
 return 1;
}

Above code doesn't work, but this one works fine, and does what I want:

int main(){
 int a,b,c;
 a=3;b=2;c=0;
 {int c;b+=a;c=1;b+=c;}
 printf("b:%d c:%d\n",b,c);
 return 1;
}

I could just put code for a function into a file, and then do { #include ... }, but perhaps there's a better way to do that?

Any other ideas and suggestions on managing such thing are appreciated too.

Thanks.

P.S.

Using global scope is not an option, because of threading. Just passing variables to a function is not an option, it will take 30+ arguments. I already have a bunch of struct's going on, so putting all variables into a single struct is not an option ether.

Ignas2526
  • 532
  • 7
  • 12

1 Answers1

0

There are different ways to achieve what you need.

The first is to use gcc nested functions. Unfortunately, they are nonstandard, and as such are entirely compiler-dependent. You can find some information here : Stack Overflow nested C function and on GCC Nested functions manual. If you don't need portability this may be a valid solution.

Another solution may be to define a macro with the code of your pseudo function. The compiler will substitute every times the macros is called and it automatically inheritances the variable defined into the caller function.

I rewrote your example with both solutions.

#include <stdio.h>

#define FUNC_A()  {int c;b+=a;c=1;b+=c;} 
#define FUNC_A2(_a,_b) {int c;_b+=_a;c=1;_b+=c;} 

int main(){
   int a,b,c;
   void func_a(){int c;b+=a;c=1;b+=c;}

   // nested solution
    a=3;b=2;c=0;
    func_a();
    printf("b:%d c:%d\n",b,c);

    // macro solution
    a=3;b=2;c=0; 
    FUNC_A(); 
    printf("b:%d c:%d\n",b,c);

    // macro solution
    a=3;b=2;c=0; 
    FUNC_A2(a,b);
    printf("b:%d c:%d\n",b,c);

    return 0;
}

All solutions have the same result. :D

Community
  • 1
  • 1
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
  • One of the reasons I tried to go for a function is because I was expecting that compiler will have better time optimizing repeating code. I would like to stick to a standard, so any thoughts on how well compiler optimizes repeating scopes? – Ignas2526 Jul 25 '13 at 12:09
  • Using the solution with the macro, the compiler will optimize your code as if it was written within the caller function. Therefore, you are avoiding all the overhead to call a function ( push/pop parameters, addresses in the stack). if you are using a variable before the macro and then you use the same variable in the macro the compiler is smart enough to keep it in the register without need for going in the memory. If you are concern about performance, the MACROS will not affect your performance for sure. – Giuseppe Pes Jul 25 '13 at 12:23