-2

I have the following C code which gives an error:

Program stopped at 0x4019b3.
It stopped with signal SIGSEGV, Segmentation fault.

when debugging.

Here is the code:

#include <stdio.h>
#include <complex.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    clock_t begin, end;
    double time_spent;

    begin = clock();

    int n  = 100;int i; int j;
    int N  = 64;int r;
    double complex (s)[4] = {-1-1*I, -1+1*I, 1-1*I, 1+1*I};
    double complex symbol[n][N];
    for (i=0; i<n; i++){
        for (j=0; j<N; j++){
            r = rand() % 4;
            symbol[i][j]=s[r];  
        }
        // Now add pilots:
        symbol[i][11] = 1;
        symbol[i][22] = 1;
        symbol[i][33] = 1;
        symbol[i][44] = 1;
    } 
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    return 0;
}

Any idea what's wrong?

EDIT:

Now I can put it all together after these valuable discussions. Here is the working code with timing and memory allocation and every thing:

#include <stdio.h>
#include <complex.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    clock_t begin, end;
    double time_spent;
    begin = clock();

    int n  = 100000; int i; int j;
    int N  = 64;int r;
    double complex (s)[4] = {-1-1*I, -1+1*I, 1-1*I, 1+1*I};
    double complex (*symbol)[N] = malloc(n * sizeof *symbol);
    for (i=0; i<n; i++){
        for (j=0; j<N; j++){
            r = rand() % 4;
            symbol[i][j]=s[r];  
        }
        // Now add pilots:
        symbol[i][11] = 1;
        symbol[i][22] = 1;
        symbol[i][33] = 1;
        symbol[i][44] = 1;
    } 
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("%3.7f\n",time_spent);
    return 0;
}
AboAmmar
  • 5,439
  • 2
  • 13
  • 24

2 Answers2

3

Memory needed to hold the variable declared by the line

double complex symbol[100000][64];

is too much for the stack.

Even a simple program like below runs into Segmentation fault when run an a 64-bit machine.

#include <stdio.h>
#include <complex.h>

void foo()
{
   double complex symbol[100000][64];
   printf("%zu\n", sizeof(symbol));
}

int main(int argc, char** argv)
{
   foo();
   return 0;
}

Consider allocating that memory from the heap, for example:

double complex (*symbol)[N] = malloc(n * sizeof *symbol);

The other problem is that in the loops:

for (i=0; i<n; i++){
    for (j=0; i<N; j++){  // Problem line
        r = rand() % 4;
        symbol[i][j]=s[r];  
    }

You are accessing out of bounds memory. The problem line should be changed to:

    for (j=0; j<N; j++){
              ^^ Use j not i
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

You have a copy and paste error in your second loop test:

for (j=0; j<N; j++){
          ^

It should be j not i

[Also, unrelated but you should not use modulus % on the result of rand() because the low bits are not as random as the high bits. Use division instead.]

[Another answer points out that you may also be exhausting your stack, although I would expect a different error. Worth checking though.]

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • Re. the randomness issue, that would depend on which PRNG was in use – M.M Sep 29 '14 at 02:32
  • @MitchWheat The same error and, BTW, when I run it a window with `ProgName.exe has stopped responding` appears above the black `.exe` output window. – AboAmmar Sep 29 '14 at 02:33
  • @Matt: stdlib.h, rand() – Mitch Wheat Sep 29 '14 at 02:37
  • @MitchWheat `rand()` is a standard C function, the standard does not specify that lower order bits must have lower randomness than higher order bits. The OP's implementation might have one in which lower order bits do not have lesser randomness than higher order bits. – M.M Sep 29 '14 at 02:38
  • I assume you mean LCG. Glibc [defaults to LFSR](http://stackoverflow.com/questions/3932978/gcc-implementation-of-rand) so I question your statistic. It might be better to link to discussion of this topic instead of asserting. – M.M Sep 29 '14 at 02:45