By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,123 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,123 IT Pros & Developers. It's quick & easy.

problems with malloc

P: 3
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

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define TOTAL_LENGTH   170 
  5. #define POPSIZE               10
  6. #define MEM1 (2 * POPSIZE * sizeof(int *))
  7. #define MEM2 (2 * POPSIZE * TOTAL_LENGTH * sizeof(int))
  8. #define MEM (MEM1 + MEM2)
  9.  
  10. int main()
  11.   int i,j;
  12.   int **Value_old, **Value_new;
  13.   double *ptr;
  14.  
  15.   ptr = malloc(MEMSIZE);
  16.   if(!ptr) exit(-1);
  17.  
  18.   Value_old = (int **)  ptr;
  19.   Value_new = (int **) (ptr + POPSIZE * sizeof(int *));
  20.   for(i = 0; i < POPSIZE; ++i)
  21.   {
  22.         Value_old[i] = (int *)(ptr + MEM1 + i * TOTAL_LENGTH * sizeof(int));
  23.         Value_new[i] = (int *)(ptr + MEM1 + (POPSIZE + i) * TOTAL_LENGTH * sizeof(int));
  24.         for(j = 0; j < TOTAL_LENGTH; ++j)
  25.         {
  26.              Value_old[i][j] = 0;
  27.              Value_new[i][j] = 0; 
  28.         }                         
  29.   }
  30.  
  31.   return 1;
  32. }
Mar 18 '07 #1
Share this Question
Share on Google+
6 Replies


arne
Expert 100+
P: 315
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

#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;
}
This should not compile at all, since MEMSIZE is not defined. Please define it and see if your code works as desired.
Mar 18 '07 #2

P: 3
This should not compile at all, since MEMSIZE is not defined. Please define it and see if your code works as desired.
Hi:
MEMSIZE is in fact what I call MEM. But even with such change it seems that my code does not work. Any clue? Thanks much

A
Mar 18 '07 #3

dmjpro
100+
P: 2,476
if u use MEM instead of MEMSIZE then what happens with it.....
Mar 19 '07 #4

arne
Expert 100+
P: 315
Hi:
MEMSIZE is in fact what I call MEM. But even with such change it seems that my code does not work. Any clue? Thanks much

A
Doesn't crash when I try it. But I can't tell if it works as desired, of course :)
Mar 19 '07 #5

P: 3
Doesn't crash when I try it. But I can't tell if it works as desired, of course :)
Hi,
it seems that if I define ptr as a void * instead of double * the code goes thru fine. I am not sure way though.

Thanks for the help guys.

A
Mar 19 '07 #6

arne
Expert 100+
P: 315
Hi,
it seems that if I define ptr as a void * instead of double * the code goes thru fine. I am not sure way though.

Thanks for the help guys.

A
Yes, the return value of malloc is a pointer to void. Usually one uses it with a cast, like
Expand|Select|Wrap|Line Numbers
  1. char *p;
  2. p = (char *) malloc( 100 * sizeof(char) );
  3.  
Mar 19 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.