Jump to content

Simple C code would not compile because of matrix with variable length

nightmarevoid

Hello All.

I'm taking a introductory C course at uni, and I had a really weird issue with my compiler. First some background though.

As per the instructors instructions, I am using VS code with the C/C++ extension and GCC 11.3.0 installed through cygwin64. All of my paths are set up in system environment variables, and in VS code (VS code automatically sees the correct paths). Up until everything has worked fine, but this one problem refused to compile.

 

SO, the goal of the problem was extremely simple, just query the user for the size of a square matrix, then have the user populate it. take the populated matrix and print it back to the terminal with the lower left triangle of the matrix replaced with zeros. My issue wasn't with the code, but with the compiler having a strange problem with it.

Whenever I try to compile it, I end up with the dreaded segmentation fault. I deleted lines of code one by one until I found the one causing the fault:

 ...
 scanf("%d", &nsize);
 //create matrix
 int sqtrix[nsize][nsize];
 ...

When the code gets to the matrix initialization, it just hangs and gives the segmentation fault.

I checked it with online compilers, and I ran it through linux using WSL, and it worked everywhere except for on my pc. 

Can anyone give me insight into this? I'll be going to office hours to get this sorted, but some insight before then would be great.

Ryzen 7 3700X

Aorus GTX 1080ti

G.Skill TridentZ 3200MHz 2x8GB

Corsair SFX 750W

Phanteks Evolve Shift Air (glass front)

2x Corsair Force GS 120GB SSD (RAID 0)

Link to comment
Share on other sites

Link to post
Share on other sites

Interesting. What happens if you set the standard to -std=c99 or -std=c11 ?

 

Edit: I compiled this code with gcc 11.3.0 under WSL (ubuntu) and it works just fine, but some versions of C only allow const size arrays.

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, shadow_ray said:

Interesting. What happens if you set the standard to -std=c99 or -std=c11 ?

 

Edit: I compiled this code with gcc 11.3.0 under WSL (ubuntu) and it works just fine, but some versions of C only allow const size arrays.

While googling the issue that was what I found as well, but all of my settings should allow for variable size arrays. I'll try changing the standard though. right now it's set to C17 which I believe should work, but maybe I misunderstood.

Ryzen 7 3700X

Aorus GTX 1080ti

G.Skill TridentZ 3200MHz 2x8GB

Corsair SFX 750W

Phanteks Evolve Shift Air (glass front)

2x Corsair Force GS 120GB SSD (RAID 0)

Link to comment
Share on other sites

Link to post
Share on other sites

yes, c17 should work as well.

 

Can you compile and run this code?

#include <stdio.h>

int main(){
    int nsize;
    scanf("%d", &nsize);
    //create matrix
    int sqtrix[nsize][nsize];

    printf("%ld", sizeof(sqtrix));
}

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, nightmarevoid said:

Whenever I try to compile it, I end up with the dreaded segmentation fault.

14 hours ago, nightmarevoid said:

When the code gets to the matrix initialization, it just hangs and gives the segmentation fault.

Just to clarify - does the compiler give you a segmentation fault, or the program itself when you test it?

 

Anyway this is pretty much the reason Windows comes with a Linux subsystem now; setting up build environments is a huge pain in normal Windows. If you have a cygwin installation have you tried compiling the program manually outside of vscode? it's possible the extension for vscode is misconfigured or bugged

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×