-1

I am creating a decompiler from IL (Compiled C#\VB code). Is there any way to create reference in C?

Edit:
I want something faster than pointer like stack. Is there a thing like that?

Super File
  • 31
  • 1
  • 6
  • 1
    Intuitively, it's the _allocation_ that is slower on the heap than on the stack, not the actual use of the variable. See also [this answer](http://stackoverflow.com/a/79936/1025391). You might need to optimize your allocation strategy if this is indeed an issue for you. – moooeeeep Jul 17 '12 at 09:36
  • I knew that ,and my decompiler optimize the allocate (About x11.5 faster) and I saw when I use alot of references (That converts to pointer after decomplication) the performance going down. – Super File Jul 17 '12 at 09:46
  • then you probably have another problem with your decompiler, that is apparantly not subject of this question? – moooeeeep Jul 17 '12 at 09:55
  • @moooeeeep Yeah ,it's taking a little perf. (With alot of dereferencing i get only x8 more perf than C# ,and with stack i can 40x) – Super File Jul 17 '12 at 09:56
  • Make this question active again. – Super File Jul 17 '12 at 10:50

2 Answers2

6

A reference is just a syntactically sugar-coated pointer–a pointer will do just fine.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Joel Falcou
  • 6,247
  • 1
  • 17
  • 34
  • There is a way to replace heap with stack and skip the indirection anytime I use that variable\pointer? – Super File Jul 17 '12 at 09:22
  • @SuperFile, No. [Note: What a pointer points to can be on the stack or heap. Ditto a reference.](http://stackoverflow.com/a/57492/1025391) – moooeeeep Jul 17 '12 at 09:33
  • @moooeeeep I want something faster than pointer like stack. (Opposite of pointer) There is a thing like that? Something that doesn't need indirection. – Super File Jul 17 '12 at 09:34
  • @SuperFile I think that a C++ compiler translates references to derefereneced pointers, so there's is no performance gain. Besides, what are you coding that is sensitive to dereferencing? – Eitan T Jul 17 '12 at 09:42
  • @EitanT Yeah ,it's taking a little perf. (With alot of dereferencing i get only x8 more perf ,and with stack i can 40x) – Super File Jul 17 '12 at 09:49
  • 2
    @EitanT: It's possible that the questioner is talking about something like this: `int i = 0; int &j = i; j = 1;`. Then a sensible compiler will *not* translate the reference `j` to a pointer, it will just use `j` as an alternative name for the same variable that `i` is a name for. Or perhaps the compiler could treat it as a pointer and then the optimizer eliminates the pointer with identical results as if it had never been a pointer in the first place. – Steve Jessop Jul 17 '12 at 10:18
  • @SteveJessop I was actually referring to variables passed by reference. In your example I agree that no dereferencing is done, but the OP was talking about the C# `ref` keyword, no? – Eitan T Jul 17 '12 at 11:01
  • @EitanT: I'm not certain what the questioner is talking about, I just think it *might* be that, because of this talk of "replacing heap with stack and skip the indirection". It may mean something completely different, but to me it brought to mind that snippet of C++. Which I concede is a language the questioner hasn't mentioned at all :-) – Steve Jessop Jul 17 '12 at 11:30
  • @SteveJessep Yeah, most of this discussion is just guesswork. – Eitan T Jul 17 '12 at 11:32
  • @EitanT Yeah I meant the reference of C# that it's working like normal variable but reference (Pointer in my decompiler) – Super File Jul 17 '12 at 13:12
1

Stack and pointer are two completely independent concepts.

A reference is just like a pointer, a way to access/pass a variable without copying it. On the other hand, stack and heap are two different places where variables live. The decision whether or not a variable should live on the stack or on the heap is totally independent from the way you pass it around.

  • If you need a local variable, with a lifetime automatically coupled to your function scope declare it on the stack. Allocation is fast, but the object is gone when the function scope ends. Taking this into account, you can pass the variable by value or by pointer to other functions.
  • If you need a variable that survives the function scope, you need to make it global (or static), or to put the variable dynamically on the heap. Allocation is a bit slower, but once it's there you can use it like the other. You can pass it by value or by pointer then, either. (Bear in mind, that you need to de-allocate dynamically created objects eventually.)

If heap allocation is indeed a performance bottleneck, you should make sure that you use automatic variables (on stack) where possible. Then, do profiling of your allocation patterns. And finally optimize your allocation strategy.

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187