Help with malloc()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schizoid Man

    Help with malloc()

    Hi,

    I have a function in which I am reading in an integer value, and
    dynamically creating an array of type double and size of the integer value:

    double compute(int steps, int typeopt)
    {
    double *price_array;

    price_array = (double *) malloc(sizeof(i nt) * steps);

    ...

    }

    So far so good.

    Now in my main method, I invoke this function twice:

    value = compute(steps, 0);
    printf("\n%5.5l f", value);

    value = compute(steps, 1);
    printf("\n%5.5l f", value);


    I get a correct answer for the first time it is invoked, but I get the
    following error when the function is invoked the second time (i.e
    typeopt = 1): Unhandled exception at 0x00411816: 0xC0000005: Access
    violation writing location 0x00000000.

    I then go back to the compute method and put a free(price_arra y);
    statement at the end of the method.

    I then get the following message:

    HEAP: Heap block at 00355870 modified at 0035606C past requested size of
    7f4 Windows has triggered a breakpoint. This may be due to a corruption
    of the heap, and indicates a bug in or any of the DLLs it has loaded.
    The output window may have more diagnostic information.

    I would really appreciate any help at all.

    Thanks in advance,
    Schiz



  • Chris McDonald

    #2
    Re: Help with malloc()

    Schizoid Man <schiz@sf.comwr ites:
    >I have a function in which I am reading in an integer value, and
    >dynamically creating an array of type double and size of the integer value:
    >double compute(int steps, int typeopt)
    >{
    > double *price_array;
    >
    > price_array = (double *) malloc(sizeof(i nt) * steps);
    ^^^^^^^^^^^
    Possibly: (sizeof *price_array)

    ?

    --
    Chris.

    Comment

    • Morris Dovey

      #3
      Re: Help with malloc()

      Schizoid Man (in eah6o0$j43$1@ge raldo.cc.utexas .edu) said:

      | Hi,
      |
      | I have a function in which I am reading in an integer value, and
      | dynamically creating an array of type double and size of the
      | integer value:
      |
      | double compute(int steps, int typeopt)
      | {
      | double *price_array;
      |
      | price_array = (double *) malloc(sizeof(i nt) * steps);
      |
      | ...
      |
      | }
      |
      | So far so good.
      |
      | Now in my main method, I invoke this function twice:
      |
      | value = compute(steps, 0);
      | printf("\n%5.5l f", value);
      |
      | value = compute(steps, 1);
      | printf("\n%5.5l f", value);

      <snipperectom y>

      You haven't really provided enough info to allow a definitive answer.
      From what you have provided, I'd suggest:

      #include <stdio.h>
      #include <stdlib.h>
      double compute(unsigne d,int);

      double *price_array;
      price_array = malloc(steps * (sizeof (double)));

      Note addition of stdlib.h header and discard of cast. My prototype and
      alteration of the statement in which you invoke malloc() makes
      (possibly incorrect) assumptions as to how you intend to use the steps
      variable; but at that point negative values seem inappropriate.

      --
      Morris Dovey
      DeSoto Solar
      DeSoto, Iowa USA



      Comment

      • Default User

        #4
        Re: Help with malloc()

        Schizoid Man wrote:
        Hi,
        >
        I have a function in which I am reading in an integer value, and
        dynamically creating an array of type double and size of the integer
        value:
        >
        double compute(int steps, int typeopt)
        {
        double *price_array;
        >
        price_array = (double *) malloc(sizeof(i nt) * steps);
        >
        ...
        >
        }
        >
        So far so good.
        So far NOT so good. You have the wrong type with the sizeof operator.
        If double is a different size than int, you won't have the right amount
        allocated.

        Get rid of the cast, and change the allocation to:

        price_array = malloc(steps * sizeof *price_array);


        That way you can't screw up the type.

        Comment

        • Andrew Poelstra

          #5
          Re: Help with malloc()

          On 2006-07-30, Schizoid Man <schiz@sf.comwr ote:
          Hi,
          >
          I have a function in which I am reading in an integer value, and
          dynamically creating an array of type double and size of the integer value:
          >
          double compute(int steps, int typeopt)
          {
          double *price_array;
          >
          price_array = (double *) malloc(sizeof(i nt) * steps);
          >
          double *price_array = malloc (sizeof *price_array * steps);
          if (!price_array)
          return (double) -1;

          Will fix all of the above problems. What were you doing with sizeof (int)?

          --
          Andrew Poelstra <website down>
          To reach my email, use <email also down>
          New server ETA: 42

          Comment

          • Schizoid Man

            #6
            Re: Help with malloc()

            Morris Dovey wrote:
            Schizoid Man (in eah6o0$j43$1@ge raldo.cc.utexas .edu) said:
            >
            | Hi,
            |
            | I have a function in which I am reading in an integer value, and
            | dynamically creating an array of type double and size of the
            | integer value:
            |
            | double compute(int steps, int typeopt)
            | {
            | double *price_array;
            |
            | price_array = (double *) malloc(sizeof(i nt) * steps);
            |
            | ...
            |
            | }
            |
            | So far so good.
            |
            | Now in my main method, I invoke this function twice:
            |
            | value = compute(steps, 0);
            | printf("\n%5.5l f", value);
            |
            | value = compute(steps, 1);
            | printf("\n%5.5l f", value);
            >
            <snipperectom y>
            >
            You haven't really provided enough info to allow a definitive answer.
            From what you have provided, I'd suggest:
            >
            #include <stdio.h>
            #include <stdlib.h>
            double compute(unsigne d,int);
            >
            double *price_array;
            price_array = malloc(steps * (sizeof (double)));
            >
            Note addition of stdlib.h header and discard of cast. My prototype and
            alteration of the statement in which you invoke malloc() makes
            (possibly incorrect) assumptions as to how you intend to use the steps
            variable; but at that point negative values seem inappropriate.
            Hi Morris,

            I was already including stdlib.h, but your suggestion did help me out.
            If I discard the cast then I get the following error:

            error C2440: '=' : cannot convert from 'void *' to 'double *'
            Conversion from 'void*' to pointer to non-'void' requires an explicit cast

            However, if I use the cast then the method works perfectly. Thanks for
            the help.

            Comment

            • Richard Heathfield

              #7
              Re: Help with malloc()

              Schizoid Man said:

              <snip>
              >
              I was already including stdlib.h, but your suggestion did help me out.
              If I discard the cast then I get the following error:
              >
              error C2440: '=' : cannot convert from 'void *' to 'double *'
              Conversion from 'void*' to pointer to non-'void' requires an explicit cast
              Then I strongly recommend that you either rename your file from foo.cpp to
              foo.c, or ask in comp.lang.c++ in future.

              C and C++ are different languages, with different rules. Asking in a C group
              about a C++ program doesn't make much sense.

              --
              Richard Heathfield
              "Usenet is a strange place" - dmr 29/7/1999

              email: rjh at above domain (but drop the www, obviously)

              Comment

              • Schizoid Man

                #8
                Re: Help with malloc()

                Default User wrote:
                Schizoid Man wrote:
                >
                >
                >>Hi,
                >>
                >>I have a function in which I am reading in an integer value, and
                >>dynamically creating an array of type double and size of the integer
                >>value:
                >>
                >>double compute(int steps, int typeopt)
                >>{
                >> double *price_array;
                >>
                >> price_array = (double *) malloc(sizeof(i nt) * steps);
                >>
                >> ...
                >>
                >>}
                >>
                >>So far so good.
                >
                >
                So far NOT so good. You have the wrong type with the sizeof operator.
                If double is a different size than int, you won't have the right amount
                allocated.
                >
                Get rid of the cast, and change the allocation to:
                >
                price_array = malloc(steps * sizeof *price_array);
                >
                >
                That way you can't screw up the type.
                Hi,

                I changed the allocation from int to double and that solved my problem.
                Removing the cast however gave me a compile error (reported above).

                Thanks.

                Comment

                • Joe Wright

                  #9
                  Re: Help with malloc()

                  Schizoid Man wrote:
                  Default User wrote:
                  >Schizoid Man wrote:
                  >>
                  >>
                  >>Hi,
                  >>>
                  >>I have a function in which I am reading in an integer value, and
                  >>dynamically creating an array of type double and size of the integer
                  >>value:
                  >>>
                  >>double compute(int steps, int typeopt)
                  >>{
                  >> double *price_array;
                  >>>
                  >> price_array = (double *) malloc(sizeof(i nt) * steps);
                  >>>
                  >> ...
                  >>>
                  >>}
                  >>>
                  >>So far so good.
                  >>
                  >>
                  >So far NOT so good. You have the wrong type with the sizeof operator.
                  >If double is a different size than int, you won't have the right amount
                  >allocated.
                  >>
                  >Get rid of the cast, and change the allocation to:
                  >>
                  >price_array = malloc(steps * sizeof *price_array);
                  >>
                  >>
                  >That way you can't screw up the type.
                  >
                  Hi,
                  >
                  I changed the allocation from int to double and that solved my problem.
                  Removing the cast however gave me a compile error (reported above).
                  >
                  Thanks.
                  Have you

                  #include <stdlib.h>

                  ?

                  --
                  Joe Wright
                  "Everything should be made as simple as possible, but not simpler."
                  --- Albert Einstein ---

                  Comment

                  • Martin Ambuhl

                    #10
                    Re: Help with malloc()

                    Schizoid Man wrote:
                    I was already including stdlib.h, but your suggestion did help me out.
                    If I discard the cast then I get the following error:
                    >
                    error C2440: '=' : cannot convert from 'void *' to 'double *'
                    Conversion from 'void*' to pointer to non-'void' requires an explicit cast
                    You are not using a C compiler. You may have a product that claims to
                    contain both a C compiler and a C++ compiler. Your documentation should
                    tell you how to invoke it as a C compiler. Should you not wish to do
                    that, then your questions should be directed to <news:comp.lang .c++>.

                    Comment

                    • Keith Thompson

                      #11
                      Re: Help with malloc()

                      Schizoid Man <schiz@sf.comwr ites:
                      [...]
                      I was already including stdlib.h, but your suggestion did help me
                      out. If I discard the cast then I get the following error:
                      >
                      error C2440: '=' : cannot convert from 'void *' to 'double *'
                      Conversion from 'void*' to pointer to non-'void' requires an explicit cast
                      >
                      However, if I use the cast then the method works perfectly. Thanks for
                      the help.
                      An implicit conversion from void* to double* (or to any
                      pointer-to-object type) is perfectly legal in C. The most likely
                      explanation for the error message is that you're using a C++ compiler.

                      If you have questions about C++, you'll need to ask in comp.lang.c++,
                      <OT>and you should probably be using new rather than malloc()</OT>.
                      If you intend your code to be valid C, use a C compiler.

                      Casting the result of malloc() is required in C++, and strongly *not*
                      recommended in C. (Some people may post followups here saying that
                      you should cast the result of malloc() in C; except in some very
                      narrow circumstances, they're wrong, for reasons that have been
                      explained here many times.)

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Schizoid Man

                        #12
                        Re: Help with malloc()

                        Richard Heathfield wrote:
                        Schizoid Man said:
                        >
                        <snip>
                        >I was already including stdlib.h, but your suggestion did help me out.
                        >If I discard the cast then I get the following error:
                        >>
                        >error C2440: '=' : cannot convert from 'void *' to 'double *'
                        >Conversion from 'void*' to pointer to non-'void' requires an explicit cast
                        >
                        Then I strongly recommend that you either rename your file from foo.cpp to
                        foo.c, or ask in comp.lang.c++ in future.
                        >
                        C and C++ are different languages, with different rules. Asking in a C group
                        about a C++ program doesn't make much sense.
                        >
                        Hi Richard, Martin, Keith,

                        You're all absolutely right - I am using Microsoft Visual C++. I did
                        know that an implicit conversion from void to double is legal, so I was
                        wondering why I get this error without the cast.

                        Renaming it to .c should do the trick.

                        Thanks for the help, and sorry for the bother.

                        Comment

                        Working...