0

I'm a beginner C programmer. I've never worked in Linux before so I'm having some trouble adjusting from Windows. Especially since transitioning from Java. I'm working on a larger problem (homework) and have just now started splitting it into smaller problems, as I was getting a segmentation fault there.

At the very start of my program, I have something like this:

#include <stdio.h>
#include <stdbool.h>
typedef long long llong;

int main() {
    int objectTable[2000001][2000001];
    llong snake [4];
    llong indexZero = 1000000;
    bool snakeTable[2000001][2000001];
    snake[1] = indexZero;
    snake[2] = indexZero;
    snake[3] = indexZero;
    snake[0] = indexZero;
    snakeTable[indexZero][indexZero] = true;
    return 0;
    //printfstatements
}

After this, all I added were some printf statements like these:

printf("The snake's head is located on the coordinates x=%lld, y=%lld \n", snake[0]-indexZero, snake[1]-indexZero);

I also tried storing the snake[x]-indexZero into another variableto no avail.

What am I doing wrong? Kind regards,

Vid Stropnik
  • 182
  • 10
  • 2
    Unless you have 14901 GB of RAM, I think `int objectTable[2000001][2000001];` is likely going to cause issues being as big as it is. Reducing the size of those arrays gets rid of the segmentation fault for me. I think that if you think you need arrays that large, you are doing something very wrong. – Random Davis Apr 03 '17 at 18:12
  • 2
    The array declaration is much too large even for most contemporary computer memories (approx - 4TB*sizeof(int)). Also, you made it a stack variable. (You would probably want a static variable instead assuming you want this array at all.) What makes you believe that your arrays need to be so large? – Ken Clement Apr 03 '17 at 18:18
  • The assignment wants us to simulate the movement of a Snake (ie, the Nokia game). The snake moves on a grid from -10^6 to 10^6. Hence the array length of 2000001. Using the input, we later have to place objects into the "grid" and have the snake move according to the objects. I am aware of the fact that there are better ways to do this, I was just confused as I was under the impression something was wrong with my syntax. Seeing as this is causing problems like this, should I change from my virtual machine, as it only has access to a small amount of RAM? – Vid Stropnik Apr 03 '17 at 18:31
  • 4*10^12 pixels? That's an interesting display. – ThingyWotsit Apr 03 '17 at 19:08
  • 1
    @VidStropnik Changing environments won't help anything. An array of that size is 16 TB, which is probably a few orders of magnitude higher than your host system's hard drive space, let alone RAM, or even the size of the stack frame. Your professor likely chose a size of 10^6 because that would make naive implementations, such as yours, impossible, and so you'd have to come up with a reasonable way to do it. Likely, you should be keeping track of the positions of every item separately, in structs for instance, so you don't have to put them into an array of coordinates. – Random Davis Apr 03 '17 at 19:29

0 Answers0