2

I'm pretty new to C and am trying to do what I assume is very simple - but am getting stuck for some reason:

I have a main in a file called runnable.c where I have:

#include <stdio.h>
#include <windows.h>
#include "utils.h"
#define N 100000
#define num_vars 1

int main() {
    printf("I am running...\n");
    double values[N*num_vars];
    double ders[N*num_vars];
    char variable[] = "params.txt"; 
    read_file_to_array(variable, values); // fills up values 
    test_values(values);
    test_num_points(N);
    test_ders(ders);
    return 0;
}

I then have utils.h which defines

void test_values(double values[]);
void test_ders(double ders[]);
void test_num_points(int num_points);

And utils.c which has

#include <stdio.h>
void test_values(double values[]) {
    printf("I am in test_values\n");
}

void test_num_points(int num_points) {
    printf("I am in test_num_points\n");
}

void test_ders(double ders[]) {
    printf("I am in test_ders\n");
}

I'm compiling on my Windows machine using

cl runnable.c utils.c /link /out:program.exe

Everything works great when I just have test_values and test_num_points - but for some reason it doesn't run successfully when when I add test_ders into the mix. It still compiles correctly, but nothing is outputed.

I'm having trouble getting the debugger to work on my Windows machine and was hoping somebody might be able to help me figure out what's going on.

7
  • Is there a reason you don't fill ders when you fill variable and values? I'm new to C as well, but I'm wondering if you need to initialize ders before passing it to a function. Commented Jun 18, 2019 at 22:35
  • What do you mean by "it all breaks"? Commented Jun 18, 2019 at 22:35
  • @Tordek I mean that it compiles but doesn't run successfully (i.e. doesn't output anything). Commented Jun 18, 2019 at 22:38
  • 3
    Stack sizes are usually pretty small, i.e. only a few megabytes. So you need to be careful when declaring arrays as local variables. double ders[100000*1] uses 800Kbytes, so that's a bit big for a local variable. You can declare with the static keyword, or move it out of main (i.e. make it a global variable). static and global variables can use all of the memory, so they can be much bigger than local variables. Commented Jun 18, 2019 at 22:40
  • You never put any values into ders[], and you never print any values from it, so I'm not sure what output you're expecting. Commented Jun 18, 2019 at 22:43

1 Answer 1

3

Stack sizes are usually pretty small, i.e. only a few megabytes. So you need to be careful when declaring arrays as local variables.

double ders[N*num_vars]; translates to double ders[100000*1]; and will use 800K bytes (assuming a double is 8 bytes). So that's a bit big for a local variable.

You can declare with it the static keyword, or move it out of main (i.e. make it a global variable). static and global variables can use all of the memory, so they can be much bigger than local variables. You can also allocate the memory dynamically, e.g. using malloc or calloc.

Sign up to request clarification or add additional context in comments.

1 Comment

another recommendation would be to heap allocate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.