lkml.org 
[lkml]   [1997]   [Jul]   [5]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
Subjectwhy doesn't the following cause a coredump in Linux?
The following program will not cause a coredump even though it looks like
I am assigning a value to a illegal memory reference. Why is this? Am I
allocating more memory than I need in my dim2d or am I allocating it the
wrong way? I am not sure why the main() references won't cause a core
dump, since it looks like I'm poking past the array's boundaries (at least
in the static case, one would think so). Can someone give me a hint on
memory allocation and the layout of a statis/dynamic array in memory under
Linux(or UNIX even, perhaps)? Thank you dearly.

M. Naskovski

--
#include <stdio.h>
#include <stdlib.h>

double **dim2d(int n, int m) /* n = # of rows, m = # of columns */
{
int i;
double **narr;
narr=(double **)calloc(n, sizeof(double *));
if (narr == NULL) return NULL;
for (i=0; i < n; i++) {
narr[i]=(double *)calloc(m, sizeof(double));
if (narr[i] == NULL) return NULL;
}
return narr;
}

main()
{
double **a=dim2d(1,1);
double a2[1][1];
a[0][0]=255;
a[0][5]=256;

a2[0][0]=255;
a2[0][5]=256;
}




\
 
 \ /
  Last update: 2005-03-22 13:39    [W:0.035 / U:0.036 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site