1

I'm trying to read an unknown number of elements into the array, when my size reaches the current capacity, I call a function to double the size and copy the contents of the old array into a new array. I got my '&' but it seems it's still passing the array by value.

#include <iostream> #include <fstream> using namespace std; void resize(int*&, int&); int main() { ifstream in("numbers.input"); int cap = 10; double avg; int total = 0; int size = 0; int *arr = new int [cap]; int temp; for(int i =0; i <cap; i++){ in >> temp; if(size >= cap) { resize(arr,cap); } arr[i]=temp; total += arr[i]; size++; } avg = (double) total/cap; cout << cap <<endl; cout << size <<endl; cout << total <<endl; cout << avg; return 0; } void resize(int *&arr,int &cap) { cap*=2; int* newArr = new int[cap]; for(int i = 0; i < cap; i++){ newArr[i] = arr[i]; } delete [] arr; arr = newArr; } 
8
  • 1
    Why is it so hard to format code before posting... Commented Mar 1, 2015 at 22:25
  • 4
    Use vector and avoid the problem? Commented Mar 1, 2015 at 22:26
  • What evidence do you have that the "array" (actually the pointer) is not being passed by reference? It doesn't take an example this complicated to verify that statement. Commented Mar 1, 2015 at 22:27
  • 4
    Arrays can't be passed by value, so your observation is incorrect. Commented Mar 1, 2015 at 22:27
  • 1
    You have a bug in the for loop that does the copying in the call to resize. Can you spot it? Commented Mar 1, 2015 at 23:32

2 Answers 2

1

Everything you try to implement 'by hand' is already in the standard library. Use std::vector<> which implements a doubling/reallocation strategy very similar to what you're proposing.

#include <fstream> #include <vector> int main() { std::ifstream in("numbers.input"); std::vector<int> arr; int temp; while (in >> temp) { arr.push_back(temp); } // process your data ... } 

See http://www.cplusplus.com/reference/vector/vector/

To answer the question more literally: Arrays are always passed by reference, typically by passing a pointer to the first element.

Sign up to request clarification or add additional context in comments.

2 Comments

Consider editing the title of your question? It's really about dynamic resizing of an array.
I'd use vector if I could, but the assignment calls for an array. But thanks, anyhow.
0

Your resize function is taking the pointer by reference and will modify the value of the variable it is called with. However you have a number of bugs:

  1. You copy cap items out of the old array, but you have already doubled cap, leading to an out of bound access and a possible crash.
  2. Your resize function never gets called, due to a bug in your input loop. You should step through in a debugger (or at least add some trace cout calls) to work out what is going on. Try to figure this one out, if you can't let me know.
  3. Your average is using cap as the divisor, that is not correct.

Note: You should add in your question that you can't use vector, because that would be the normal way to do this.

Note 2: In your question, you should also say exactly what is going wrong with your program - "seems to be passing the array by value" is a bit vague - why do you think it isn't passing by value?

4 Comments

There is another issue. Changing the value of cap prematurely in the resize function before new[] is called. If new[] throws an exception, cap now has a wrong value.
Well, I think it is passing it by value because the array remains unchanged, but as you pointed out, the function to modify it, isn't even being called. Nonetheless, you've pointed me in the right direction and I really appreciate it.
I got it to call resize now. However, I think an exception is being thrown for running out of memory(std::bad_alloc), but I'm confused. Shouldn't new and delete protect me from that?
It depends on how you changed your loop. If you changed the condition to something like i < cap+1, then it will never stop because i will never exceed cap. You will then keep doubling cap until it gets too big to allocate. Use a loop like in the other answer, where the loop will stop when the input finishes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.