Reputation: 633
typedef struct Matrix
{
double * matrix;
int sizex;
int sizey;
}Matrix;
int nn = 257;
Matrix * g = (Matrix *)malloc(sizeof(Matrix *));
g->matrix = malloc(sizeof(double) * nn * nn);
g->sizex = nn;
g->sizey = nn;
This code give an error when it gets to g->matrix = malloc(sizeof(double) * nn * nn);
anyone see a problem with it ?
edit: found problem to be accessing unallocated memory in a place before the allocation shown, it was causing a SIGSEGV:Segmentation fault.
Upvotes: 3
Views: 2043
Reputation: 1
I'll just add the direction of not casting malloc's return. It is unnecessary, pollutant, and might lead to unwanted behavior (as described here: http://c-faq.com/malloc/mallocnocast.html
Upvotes: 0
Reputation: 106236
Matrix * g = (Matrix *)malloc(sizeof(Matrix *));
This reserves enough heap space for a pointer to a Matrix, but you want enough heap space for the Matrix itself. Try:
Matrix* g = (Matrix*)malloc(sizeof(Matrix));
For a complete, working program:
#include <stdlib.h>
#include <stdio.h>
typedef struct Matrix {
double * matrix;
int sizex;
int sizey;
} Matrix;
int main()
{
int nn = 257;
Matrix * g = (Matrix *)malloc(sizeof(Matrix));
if (g == NULL)
{
printf("g = malloc() failed\n");
return 1;
}
g->matrix = malloc(sizeof(double) * nn * nn);
g->sizex = nn;
g->sizey = nn;
printf("g %p, g->matrix %p, g->sizex %d, g->sizey %d\n",
g, g->matrix, g->sizex, g->sizey);
return 0;
}
Output on my Linux box:
g 0x8822008, g->matrix 0xf6ea6008, g->sizex 257, g->sizey 257
Upvotes: 0
Reputation: 455380
You need to pass malloc
the sizeof the Matrix
not sizeof pointer to the Matrix
.
Change
Matrix * g = (Matrix *)malloc(sizeof(Matrix *));
^^
to
Matrix * g = (Matrix *)malloc(sizeof(Matrix));
Also you must always check the return value of malloc
and make sure allocation succeed before you go and use the allocated memory.
Upvotes: 6
Reputation: 80031
You do not allocate memory for a Matrix
object, you are allocating for a pointer.
Change your first malloc
call to do this:
Matrix * g = malloc(sizeof(*g));
I prefer this style as you do not need explicit pointer cast from void *
in C, and you are allowed to do sizeof
on the variable's underlying type. This could save you headaches just in case you change the type of g
(or for future code).
Similarly for style:
g->matrix = malloc(sizeof(double) * nn * nn);
should be:
g->matrix = malloc(sizeof(*(g->matrix)) * nn * nn);
Upvotes: 0
Reputation: 6249
Matrix * g = (Matrix *)malloc(sizeof(Matrix *));
should be
Matrix * g = (Matrix *)malloc(sizeof(Matrix));
You're only allocating the size of a pointer to Matrix, rather than a Matrix itself.
Upvotes: 0
Reputation: 215547
I'm guessing you're using some ancient 16-bit compiler, probably Turbo C. Junk it and get gcc, either djgpp if you want to build DOS programs or mingw or cygwin if you want to build Windows programs.
Assuming I'm right, 257*257 overflows the maximum addressable size, 65536, not to mention what happens when you multiply it by 8.
Edit: OP changed the question after I wrote this so it may be completely off. If so I'll delete it.
Upvotes: 0