problems with malloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • antoroma
    New Member
    • Mar 2007
    • 3

    #1

    problems with malloc

    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

    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;
    }
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by antoroma
    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.

    Comment

    • antoroma
      New Member
      • Mar 2007
      • 3

      #3
      Originally posted by arne
      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

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        if u use MEM instead of MEMSIZE then what happens with it.....

        Comment

        • arne
          Recognized Expert Contributor
          • Oct 2006
          • 315

          #5
          Originally posted by antoroma
          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 :)

          Comment

          • antoroma
            New Member
            • Mar 2007
            • 3

            #6
            Originally posted by arne
            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

            Comment

            • arne
              Recognized Expert Contributor
              • Oct 2006
              • 315

              #7
              Originally posted by antoroma
              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
              Code:
              char *p;
              p = (char *) malloc( 100 * sizeof(char) );

              Comment

              Working...