What does int* resizeArray do that int resizeArray doesn't, what' the deference?

And why do I delete nArray, and not newArray?

#include <iostream>


using namespace std;

void printArray(int* nArray,int size)
{
	// code printing array
}

int* resizeArray(int* nArray, int oldsize, int newsize )
{
	int* newArray = new int[newsize];
    
    // code copying old to new...

    delete[] nArray; // <--- Why not new array
    return newArray;


}

int main()

{
	int* nArray = 0;

	//code...

	delete[] nArray; 
	nArray =0;

}

int* resizeArray is a declaration that states "hey, I want space for a pointer to an int"

int resizeArray is a declaration that states "hey, I want space for an int"

int* resizeArray is a pointer to an int that can be treated as an array, whereas int resizeArray can only be treated as an integer.

That's the short story of it.

What does int* resizeArray do that int resizeArray doesn't, what' the deference?

And why do I delete nArray, and not newArray?

#include <iostream>


using namespace std;

void printArray(int* nArray,int size)
{
	// code printing array
}

int* resizeArray(int* nArray, int oldsize, int newsize )
{
	int* newArray = new int[newsize];
    
    // code copying old to new...

    delete[] nArray; // <--- Why not new array
    return newArray;


}

int main()

{
	int* nArray = 0;

	//code...

	delete[] nArray; 
	nArray =0;

}

Resizing an array is actually copying the array to a different (larger or smaller) block of memory. Internally, a pointer to an array is the memory address of the beginning of a block of memory. So for example, if you say "int q = NewArray[4]" what you are actually telling the computer is to first look to see where NewArray starts by examining the NewArray pointer, and then go 4 memory spaces to the right, and set q to whatever is contained at that location.

Here's the breakdown:
1) To resize an array, you first have to make a completely new array with the new size. That is what NewArray is. Create an array with a size of newsize. (which is done for you already I see).
2) Next you would copy the contents of the array you passed into the function. Remember that *nArray is just a pointer to the front of your old array. So you just do something like "for (int i = 0; i < oldsize; ++i) NewArray = nArray;"
3) But now you have two arrays and you have to delete one of them or every time you call the function you will have another extra array taking up memory.
4) So why delete nArray and not NewArray? Deleting is not what you think--you don't delete the NAME of the pointer, but the contents of memory itself. So if you delete NewArray, you would delete everything you just copied. So what you have to do is delete the old block of memory (via delete [] nArray), and then since the name nArray is still around, have it repoint to the NewArray.

5) so why "int* ResizeArray" and not "int ResizeArray". int* is the return value. After you make the copy, you have to return it (at least in this example you do). The reasoning for this is because, in your main(), you will say "oldarray = ResizeArray(oldarray, 10, 5)" for example.

You are perhaps wondering why you can't just say "nArray = NewArray" inside the function itself...and this is a common thing that teachers fail to explain well. The problem is that it wouldn't work. To understand this you have to understand that functions only get a copy of information. You may have heard "pass by value, pass by reference". Where value just gives a copy of the integer or whatever, and reference gets the address. Well start thinking "everything is passed by copy". When you pass by value, you are passing a copy of the integer, when you pass by reference, you are passing a COPY of that memory address, so you can access stuff at that memory address and make permanent changes at those memory locations. When you are passed a copy of an address (i.e. when you pass a pointer), well it's just a copy. It's not the real deal so if you say "nArray = NewArray" inside the function, what you are really saying is "the copy of the address of nArray = NewArray". So you can't change where a pointer points inside a function permanently unless you make modifications (i.e. pass it by int **nArray). Doing this would pass a copy of the reference location of the pointer, so you could make changes to where the pointer points.
Hope this helps!
-Greywolf

So basically when I return a pointer from a function I must always put int* or float* etc except when void and bool? Or does it have to for boolean as well.

The thing with deleting got me confused because first they say:
when you use new you have to delete
int* newArray = new int[newsize];

but I never used new for nArray

From what you say, if I understand correctly, I'm storing the address of newArray in nArray is that right, so it's basically all I have to do is delete the address and set to null.

Invisi,

nArray is passed as a function parameter.

What you are doing is making another array, using memcpy to move the contents of the old array to the new one, and then deleting the old one.

You return a pointer (address of 0th element of array) to the new array from your function, this becomes nArray, which in this case is just unclear naming since you use that name in main().

You in fact, -are- deleting the array which contained your data, you obviously would not want to delete the freshly doubled in size array you made just nanoseconds before.

int* resizeArray(int* OLDArray, int oldsize, int newsize )
{
int* newArray = new int[newsize]; 
memcpy(newArray,OLDArray,oldsize*sizeof(int)); // I don't know why you bothered to omit this
delete[] OLDArray; // <--- Why not new array <--- why newArray?
OLDArray = newArray; // this way you don't have the modify the pointer later
return newArray; // <-- because newArray is what you are using in your code from now on
}
 
int main()
 
{
int sz(32);
int* nArray = new int[sz];
resizeArray( nArray, sz, (sz<<1) );
//code...

 
delete[] nArray;
nArray =0; // <-- Why are you doing this?
 
}

So basically when I return a pointer from a function I must always put int* or float* etc except when void and bool? Or does it have to for boolean as well.

The thing with deleting got me confused because first they say:
when you use new you have to delete
int* newArray = new int[newsize];

but I never used new for nArray

From what you say, if I understand correctly, I'm storing the address of newArray in nArray is that right, so it's basically all I have to do is delete the address and set to null.

nArray is just the name given to the copy of the pointer to the array declared in your main() function, sometime before you call that function. Presumably, the array that you pass into the function resizeArray would be at some point declared using "new". Else it's not an array because "new" is the keyword that allocates a block of memory. As for return values... you should think of functions that return a value as being just a mathematical equation. For example, if you have A = (B * C)/D. Well this is just a function that takes parameters B, C, and D, and returns the value A. Now whether A is an integer a float or a pointer that's up to you. This makes it possible for you to do something like int A = fun(int B, int C, int D) and so on. void just says that you don't want it to be an equation--for example, you don't need to have an equation if all you want to do is print out a message. bool can be a pointer, though. The only thing that separates bool from an integer is that it can only be 0 or 1. And, of course, bool uses less memory because of that fact. If you return a pointer, it's no different. You're still returning a number, only that number just happens to be a memory address instead of an integer.

You are correct if I'm understanding you. If you say, for example: nArray = ResizeArray(nArray, 10, 20); Then you are saying that nArray now points to the NewArray declared in your function.

commented: good post, but I think he doesn't understand the concept of scope +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.