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
Jul 30 '06
11 5491
Schizoid Man <sc***@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) 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: na1paj |
last post by:
here's a simple linked list program. the DeleteNode function is
producing an infinit loop i think, but i can't figure out where..
#include <stdio.h>
typedef struct
{
char *str; //str is a dynamic array of characters
int length; //number of characters
} String;
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working, even
the removal, I just don't know how to keep track of the parent, so
that I can set its child to the child of the node to be removed.
IE - if I had
C
/ \
B D
|
by: Robert Rota |
last post by:
Need help figuring out why my program core dumps and if I have the
structures right. If you are interested in helping me I can send you a
copy of the code. The program is supposed to mimic a 3 level
pagetable.
Thank you.
struct _PageTable {
struct _PageTable **PageArray;
unsigned int *FrameArray;
|
by: TC |
last post by:
Hi.
I write a program in c language that read a text file and extrapolate
the word. for all word the program calculate the number of the times
that word is present in the text.
The problem is the REALLOC that on my pc at times it produces error.
Someone can help me for this error?
Thank you
/*Inclusione librerie*/
|
by: leo2100 |
last post by:
Hi, I need help with this program. The program is supposed to take a
text file and identify the words in it, then it should print them and
count how many times a word is repeated. At first main called the
function wordcount, and then the function did everything including
printing out the results. That worked. Now I want to make the function
return an array of pointers to struct palabra so the calling function
can manage the data as it...
| |
by: totoro2468 |
last post by:
Here is a code I am writing. I keep geting segmentation fault.
I'm not sure what i'm doing wrong.
Can you explain it to me in PLAIN ENGLISH??
void ReadString (char *filename, int *lengthPtr, char *textFilePtr)
{
FILE *ifp;
char *text;
char txtFile;
|
by: Kevin |
last post by:
Hi all,
I clearly have an issue with some pointers, structures, and memory
allocation. Its probably pritty basic, but I'm a little stuck. Any
help would be greatly appreciated.
I'd like to instantiate an arbitrary number of arrays of arbitrary
size in function_a, copy the pointers, store the data, and free any
unused memory. My basic structure is as follows:
|
by: shrik |
last post by:
I have following error :
Total giant files in replay configuration file are :
File name : /new_file/prob1.rec
Given file /new_file/prob1.rec is successfully verified.
Splitting for giant file /new_file/prob1.rec started. Please wait....
In while loop of request searching
*** glibc detected *** ./a.out: free(): invalid next size (normal): 0x099da890 ***
======= Backtrace: =========
/lib/libc.so.6
|
by: dreiko466 |
last post by:
(sorry about my english...)
I am a newbie in C (3 month expierience)
I have wrote a simple test programm in VS2005, what i do wrong?Please...
In this programm i create a double linked list.Then pass its first block pointer
inside the structure Array to Array ->first and the last block pointer inside the structure Array to Array ->last.So i can manipulate the double linked list as a dynamic array. The cells of this dynamic array are...
|
by: Paul David Buchan |
last post by:
Hello,
I'm attempting to write a program to read in database files (.dbf).
When I do it all as a single procedure in main, everything works.
However, what I really want, is to pass the database filename to a function,
and have it pass back an array containing the database contents, and some
parameters telling me the dimensions of the array.
I've succeeded in getting my function to read in the dbf file, and it
returns the dimensions of...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |