0

I would like to pass a double array to a function via a pointer. However, despite trying a lot of things suggested here, I always get a segmentation fault upon initialization. I feel that it is very simple code (I am new to C), yet I cannot find my mistake. The code is as follows:

double *tMatrix = (double *)malloc(N*sizeof(double));
int i;

for (i=0;i<N;i++)
{
    tMatrix[i] = 0.0;
}

computeMatrix(tMatrix);

I get a segmentation fault on the first initialization of tMatrix, that is, when i=0. I want to use a pointer for the array tMatrix as I want to pass the matrix to the function computeMatrix.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Drent
  • 19
  • 4
  • 9
    Is `tMatrix == NULL`, perhaps because `N` is very large? You should check such things in your program. – M Oehm Nov 06 '18 at 09:13
  • 2
    Also, in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Some programmer dude Nov 06 '18 at 09:15
  • 5
    This or `N = 0` – Cid Nov 06 '18 at 09:15
  • @MOehm, indeed tMatrix == NULL, however this even holds for small N, say N=10? Ideally, I would like to have N set to 10^9. – Drent Nov 06 '18 at 09:26
  • 2
    The shown code has no obvious problems which could expalin a segfault (except for above comments). More context is needed. Please provide a [mcve]. – Yunnosch Nov 06 '18 at 09:31
  • Size of double is 8 bytes. 8 bytes * 10^9 =~ 7629 MB. Do you have enough RAM? – s.paszko Nov 06 '18 at 09:32
  • 2
    A typical `double` has 8 bytes, so you are requesting a _contiguous_ block of about 8GB, which in your case the system cannot provide. Depending on what you want to achieve, you could try to allocate smaller chunks (perhaps the matrix rows?) separately or you could try to implement some kind of sparse matrix. – M Oehm Nov 06 '18 at 09:32
  • s.paszko and @MOehm. Please note OPs comment "this even holds for small N, say N=10". – Yunnosch Nov 06 '18 at 09:33
  • 1
    Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us? – Some programmer dude Nov 06 '18 at 09:34
  • And you do include ``? – Some programmer dude Nov 06 '18 at 09:38
  • @Someprogrammerdude Yes I do, I will try to come up with a minimal example. – Drent Nov 06 '18 at 09:40
  • 2
    Size of double is 8 bytes. 8 bytes * 10^9 =~ 7629 MB. Even if you have enough RAM, if you are on Windows and if your program is compiled as a 32 bit program you won't be able to allocate more than ~2000 MB of memory. However for small values of `N` it is virtually impossible that `malloc(N*sizeof(double))` returns `NULL`. There is something you're not telling us. Show a [mcve]. – Jabberwocky Nov 06 '18 at 09:48
  • 2
    It would also be good to see the definition of `N` and what its value and the value of `N * sizeof(double)` are when you allocate. There's always the possibility of a `const int` being overwritten by UB elsewhere or of a macro slip-up such as `#define N 12 - 2` without parentheses. – M Oehm Nov 06 '18 at 09:51

0 Answers0