can somebody tell me why this program throws the error - memory cannot be read.

Posted on 2003-03-15
Medium Priority
Last Modified: 2010-04-01
If somebody finds the mistake, please tell me the reason also,so that I can learn the langauge.

void get(double * a, int &n)


cout<<"Enter: ";


 a=new double[n];

for (int i=0;i<n;i++)


cout<<"Enter item "<<i+1<<":";



void main()


int i;

double *game;


for(int j=0;j<i;j++)





Question by:anshuma
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8143865
You send a double * as a parameter to the funktion, and then assign to it. The problem with this is that the parameter is local, and the changes you make to it are going to have no effect outside the get() method.

What you need to do is either send a pointer to a pointer, double **a and then use *a = new double[n]; instead of the current code. Another alternative is to send a reference to the pointer instead of only a pointer.
LVL 12

Accepted Solution

Salte earned 80 total points
ID: 8147707
The double * a argument is modified but such a modification cannot afect the caller.

If the pointer a had been pointing to an array, then the get function could fill in that array. But this isn't what you are trying to do, you are trying to modify a itself and that doesn't work. Well, it works but when the function returns the modified value for a is lost.

void do_something()
   double * arr;
   int k;


Problem here is that arr itself isn't changed. k is changed though since it is a reference argument.

Suggest you declare a also as reference argument:

void get(double * & a, int & n)
  ...code as before...

This should do the trick.



Author Comment

ID: 8148688
Thank you both.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question