0

I'm using the latest version of TCC (tcc-0.9.26-win64-bin.zip). For some reason the output of this code is not what I'm expecting (the same).

#include <stdio.h>

struct CELL {
  int row;
  int col;
};

typedef struct CELL Cell;

Cell newCell(const int row, const int col) {
    printf("Input %d %d\n", row, col);

    Cell cell;
    cell.row = row;
    cell.col = col;

    return cell;
}

int main(int argc, char *argv[]) {
    Cell cell = newCell(2, 5);

    printf("Output %d %d\n", cell.row, cell.col); 
}

I run the script with:

C:\tcc\tcc.exe -run D:\cell.c

It works fine in eval so what am I doing wrong?

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • 1
    BTW: you are missing a `return 0` at the end of `main()`. – Paul J. Lucas Feb 22 '16 at 21:23
  • 1
    Your compiler is bugged. You could report a bug to the mailing list and see if someone fixes it. – M.M Feb 22 '16 at 21:23
  • It works fine in MSVC. – Weather Vane Feb 22 '16 at 21:24
  • BTW: For `struct`s, it's more efficient if you do something like have an `init()` function that takes a pointer to a `struct` and initializes the members rather than passing a `struct` by value as a return value. – Paul J. Lucas Feb 22 '16 at 21:24
  • @PaulJ.Lucas disagree, this is perfectly fine (especially if the system has 64bit registers). For larger structs the compiler is likely to perform your sugested optimization behind the scenes anyway – M.M Feb 22 '16 at 21:25
  • 1
    @PaulJ.Lucas `main()` is the one function that will supply a default return value if you do not give one. – Weather Vane Feb 22 '16 at 21:25
  • @WeatherVane only since C99 ... and since this compiler apparently has trouble with features that were added in C89 (returning structs by value) I am not really confident about its C99 compliance! – M.M Feb 22 '16 at 21:26
  • @Dave Chen, as the first answer posted says, it looks like the bug is not in your code. That answers your question, but doesn't solve the problem for you. As a workaround attempt, what happens if you pass a `Cell*` as a parameter and modify `newCell()` (and `main()`)? – donjuedo Feb 22 '16 at 21:28
  • @M.M: For portable code, I'd prefer not to have to rely on what the compiler _might_ do. – Paul J. Lucas Feb 22 '16 at 21:28
  • @PaulJ.Lucas portability and optimization are orthogonal. Your suggestion will be less efficient for small structs (e.g. this struct of two 32-bit ints is the same size as a 64-bit `double` .. and you wouldn't resort to an init function for initializing a `double` would you?) – M.M Feb 22 '16 at 21:31
  • @PaulJ.Lucas: This is not just a matter of the compiler, but of the ABI/PCS for your target platform (CPU and OS). E.g. AAPCS clearly states small `structs` like the one in the question are passed in registers. So it is what the compiler **has to** do – too honest for this site Feb 22 '16 at 21:34
  • Do not post images of text! Paste the text instead. – too honest for this site Feb 22 '16 at 21:50
  • use `tcc-0.9.26-win32-bin.zip`. Perhaps I think the run-time of MSVC are trying to use a 64-bit, but int is 32bit. – BLUEPIXY Feb 22 '16 at 21:57

1 Answers1

4

It works fine in eval so what am I doing wrong?

Nothing. It works fine for me using gcc.

Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
  • Thank you. I've just recently started out learning C, so the last thing I would blame is the compiler. I've switched my compiler to gcc. The reason I used tcc is because they had the `-run` switch so it wouldn't produce an exe. – Dave Chen Feb 22 '16 at 22:44