1

I have the following code in C which fails on compilation. I think I am missing something about the way array of structs work or about memory models or something else I am not aware of it.

#include <stdio.h>

#define BASE_YEAR 2013

struct struct_tpo_line {
    int price;
    char tpo_list[255];
    int tpo_count;
};

struct struct_bar {
    unsigned int open;
    unsigned int high;
    unsigned int low;
    unsigned int close;
    unsigned int flags_volume;  
};

struct struct_bar data[20][12][31][24][60];

main() {

    char                        filename[255];
    struct struct_bar           bar;
    struct struct_tpo_line      array1[10000];

    printf("20030101 193200;1.048400;1.048500;1.048300;1.048500;0\n");
    bar = data[2013 - BASE_YEAR][1 - 1][1 - 1][19][32];
    printf("%d %d %d %d\n", bar.open, bar.high, bar.low, bar.close);

    printf("Hello World.\n");
}

I try to compile it under Windows 10 32 bits Home using Digital Mars C compiler and I get:

C:\Users\...\DATA>dmc test2.c -o test2.exe
link test2,test2,,user32+kernel32/noi;

And the following popup:

Errormsg

I was previously using PCC compiler but I changed to Digital Mars as I thought it might be compiler-related issue.

apaderno
  • 28,547
  • 16
  • 75
  • 90
M.E.
  • 4,955
  • 4
  • 49
  • 128
  • I am editing the question to include a whole example that fails together with the compiling error. According to my calculation the array takes more than 200Mb. – M.E. Aug 21 '19 at 09:47
  • 2
    Yes, it takes up 171,417,600 bytes of memory (assuming 32-bit `int`s) which will be allocated in the process's global data space. That shouldn't be too excessive, even on a 32-bit system. – Ian Abbott Aug 21 '19 at 09:51
  • Looks like a bug in the linker. Is there any reason you use the Digital Mars compiler rather than e.g gcc? – Jabberwocky Aug 21 '19 at 09:54
  • With the edited question, it now takes up 214,272,000 bytes. – Ian Abbott Aug 21 '19 at 09:54
  • @jabberwocky I am trying to use a lightweight compiler instead of gcc, do not have too much space on the netbook I use for this task. – M.E. Aug 21 '19 at 09:55
  • 1
    `struct struct_tpo_line array1[10000];` is a huge for a local variable. Try `static struct struct_tpo_line array1[10000];`, or declare it as global variable. Same thing for `struct struct_bar bar;` – Jabberwocky Aug 21 '19 at 09:56
  • You may have better luck allocating space for those huge objects with `malloc` or `calloc`. – Ian Abbott Aug 21 '19 at 10:00
  • Is it an option for you to simply _[change your stack size?](https://digitalmars.com/ctg/ctgCompilingCode.html)_. ( Look about half way down on the linked page. ) I did this on my system and your sample program worked just as you have shown it without edits. (I am not recommending disregarding the other suggestions, just offering a new one.) – ryyker Aug 21 '19 at 12:17
  • Just in case it helps someone, issue was not related to large local variable and a stack overflow (as marked by the duplicated). Moving the variable to the global area did not solve the issue. It seems a linker error as pointed in the comments.Using another compiler (Pelles C) fixed the issue. – M.E. Aug 21 '19 at 22:48

1 Answers1

1

Your local variables are too big. Usually local variables are alloocated on the stack and the default stack space of a process is 1Mb on Windows and 8Mb on Linux.

Declare them as global variables or as static variables:

...
int main() {    
    char filename[255];
    static struct struct_bar           bar;
    static struct struct_tpo_line      array1[10000];
    ...
}

or

...
struct struct_bar           bar;
struct struct_tpo_line      array1[10000];

int main() {    
    char filename[255];
    ...
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Thanks for the advice, it does not fix the compiling issue though. (I am trying Pelles C compiler now). – M.E. Aug 21 '19 at 10:03
  • @M.E. looks like a compiler/linker bug to me then. Your original code should compile correctly, but it is expected to fail at run time. – Jabberwocky Aug 21 '19 at 10:05
  • I downloaded Pelles C (more lightweight than the whole GCC) and it compiles fine. I used static for the large local array. Thanks for checking the code, it helped. – M.E. Aug 21 '19 at 10:12