468,504 Members | 2,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

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(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

Jul 30 '06 #1
11 5124
Schizoid Man <sc***@sf.comwrites:
>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)

?

--
Chris.
Jul 30 '06 #2
Schizoid Man (in ea**********@geraldo.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(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.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 30 '06 #3
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(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.
Jul 30 '06 #4
On 2006-07-30, Schizoid Man <sc***@sf.comwrote:
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)?

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
Jul 30 '06 #5
Morris Dovey wrote:
Schizoid Man (in ea**********@geraldo.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(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.
Jul 30 '06 #6
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
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 30 '06 #7
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(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.
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.
Jul 30 '06 #8
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(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.

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 ---
Jul 30 '06 #9
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++>.

Jul 30 '06 #10
Schizoid Man <sc***@sf.comwrites:
[...]
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_Keith) ks***@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.
Jul 30 '06 #11
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.
Jul 30 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by na1paj | last post: by
4 posts views Thread by Tarique Jawed | last post: by
3 posts views Thread by Robert Rota | last post: by
6 posts views Thread by TC | last post: by
2 posts views Thread by leo2100 | last post: by
5 posts views Thread by totoro2468 | last post: by
4 posts views Thread by Paul David Buchan | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.