Help with malloc()

S

Schizoid Man

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(int) * steps);

...

}

So far so good.

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

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

value = compute(steps, 1);
printf("\n%5.5lf", 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_array);
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
 
C

Chris McDonald

Schizoid Man said:
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(int) * steps);

^^^^^^^^^^^
Possibly: (sizeof *price_array)

?
 
M

Morris Dovey

Schizoid Man (in [email protected]) 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(int) * steps);
|
| ...
|
| }
|
| So far so good.
|
| Now in my main method, I invoke this function twice:
|
| value = compute(steps, 0);
| printf("\n%5.5lf", value);
|
| value = compute(steps, 1);
| printf("\n%5.5lf", value);

<snipperectomy>

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(unsigned,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.
 
D

Default User

Schizoid 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(int) * 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.
 
A

Andrew Poelstra

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(int) * 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)?
 
S

Schizoid Man

Morris said:
Schizoid Man (in [email protected]) 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(int) * steps);
|
| ...
|
| }
|
| So far so good.
|
| Now in my main method, I invoke this function twice:
|
| value = compute(steps, 0);
| printf("\n%5.5lf", value);
|
| value = compute(steps, 1);
| printf("\n%5.5lf", value);

<snipperectomy>

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(unsigned,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.
 
R

Richard Heathfield

Schizoid Man said:

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.
 
S

Schizoid Man

Default said:
Schizoid Man wrote:




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.
 
J

Joe Wright

Schizoid said:
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>

?
 
M

Martin Ambuhl

Schizoid said:
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++>.
 
K

Keith Thompson

Schizoid Man said:
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.)
 
S

Schizoid Man

Richard said:
Schizoid Man said:



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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top