Dear members, I am new to this forum and to the C language. I have stumble to this memory allocation problem and I shave spent the last 10 hours trying to find why this piece of code does not work (I compile with Dev C++): as I try to set the vectors Value_old and Value_new to zero the code crushes. I hope you guys can help me on this. Thanks much.
AntoRoma
AntoRoma
Code:
#include <stdlib.h>
#include <stdio.h>
#define TOTAL_LENGTH 170
#define POPSIZE 10
#define MEM1 (2 * POPSIZE * sizeof(int *))
#define MEM2 (2 * POPSIZE * TOTAL_LENGTH * sizeof(int))
#define MEM (MEM1 + MEM2)
int main()
{
int i,j;
int **Value_old, **Value_new;
double *ptr;
ptr = malloc(MEMSIZE);
if(!ptr) exit(-1);
Value_old = (int **) ptr;
Value_new = (int **) (ptr + POPSIZE * sizeof(int *));
for(i = 0; i < POPSIZE; ++i)
{
Value_old[i] = (int *)(ptr + MEM1 + i * TOTAL_LENGTH * sizeof(int));
Value_new[i] = (int *)(ptr + MEM1 + (POPSIZE + i) * TOTAL_LENGTH * sizeof(int));
for(j = 0; j < TOTAL_LENGTH; ++j)
{
Value_old[i][j] = 0;
Value_new[i][j] = 0;
}
}
return 1;
}
Comment