Reading some others questions about passing 2d arrays I realized that no one answered this:
void func(int **&matrix) {
matrix[2][1]=5;
...
}
int main() {
int **matrix;
matrix=new int*[10];
for(int q=0;q<10;q++)
matrix[q] = new int[5];
func(matrix);
...
}
I tested it and it works. Any problem with this code?
The answer is the same as why shouldn't you use int*& when passing 1D arrays represented via a pointer. You want to use the reference whenever you want to modify the pointer passed to your function. In your case, you are just accessing the elements that are pointed by, no need to modify the pointer itself. So, there is no real need for a reference, although it is not technically incorrect to use a reference.
Example of when it may make sense to use a reference:
f(int*& p)
{
// do something with the memory pointed by p
for(int i = 0; i < 10; ++i)
std::cout << ++p[i]; // ok, modify the memory location p+i points to
delete[] p; // deallocation
p = nullptr; // nullify p, if you want to modify what you passed, you need the reference
}
Related
I am trying to wrap my mind around arrays and pointers in C++.
I am trying to write a function that allocates memory for an int array of variable size. The array is then filled with random numbers and returned.
The problem I have is assigning values with pointers.
This is the function:
int* getArray(int length) {
int* values = new int[length];
for (int i=0; i<length; i++) {
values[i]=i;
}
return values;
}
And it works just fine.
But, if I change it to this:
int* getArray(int length) {
int* values = new int[length];
for (int i=0; i<length; i++, values ++) {
*values=i;
}
return values;
}
It doesn't work.
Can someone explain to me why I cannot do this?
This is the main method that I used to print all the values:
int main() {
int* v = getArray(100);
for(int i=0;i<100;i++, v++) {
cout << *v <<endl;
}
delete[] v;
return 0;
}
I think it is because the new operator allocates new memory in the heap and not in the stack so the pointer might actually point to the wrong direction.
But, if that is true, why is the main method working?
Side note: I do not get any kind of warning or error, but the output is just a bunch of zeros and some random numbers.
I am using the GCC compiler on Ubuntu.
values++ is modifying values, which eventually forms the function return value.
i.e. you return one past the end of the allocated array. That in itself is perfectly legal C++.
But things go very badly wrong at the function call site: you access invalid elements of the array (which very informally speaking are your "random numbers"), and calling delete[] on that is also undefined behaviour. Boom!
I am having difficulties passing dynamically allocated array to the function by reference.
"The array should be transmitted to the function by reference".
My program should take n amount of integers and find out the minimum. That minimum should be added to all the arrays that the user has entered. But the BY REFERENCE part kills me. I tried (int &x[],int &n) but failed.
PLease help, thank you very much.
void add_min(int x[], int n)
{
int add[n];
int mini = x[0];
int i;
for(i = 0; i < n; i++)
{
if(x[i] < mini)
{
mini = x[i];
}
}
for(i = 0; i < n; i++)
{
add[i] = x[i] + mini;
}
for(i = 0; i< n ; i++)
{
cout<<add[i]<<endl;
}
}
int main()
{
int *x;
int n;
cout<<"Enter the amount of integers"<<endl;
cin>>n;
x = new int[n];
cout<<"Enter the integers"<<endl;
for(unsigned i = 0; i < n; i++)
{
cin>>x[i];
}
add_min(x,n);
delete x;
return 0;
}
Dynamically allocated arrays cannot be in a named variable and you cannot have a reference to one.
When you call new[] the array is allocated somewhere in the memory, and the call returns the address of the first object in that array. You store the address in a pointer variable. The pointer is not an array, it simply points to the first element.
You can pass a pointer by reference to a function like this:
void foo(int*& ptr)
If your assignment asks you to pass an array by reference, then you may not allocate the array dynamically. In that case, this is how you declare the argument to be a reference to an array:
void foo(int (&arr)[10])
Note that the size must be known at compile time. You can use a template argument to generate functions for different sizes of arrays.
If the assignment does ask you to allocate the array dynamically, then they probably intend you to pass a pointer by reference.
The problem I see here is the following to pass in via reference a:
dynamically allocated array
The only way you can have dynamic allocation to happen is via returning a pointer. The syntax for passing in an array to a function, as you have correctly guessed is:
void add_min(int x[], int n) { ... }
Certainly you can treat the array as a pointer, and then you get:
void add_min(int* x, int n) { ... }
now comes the problem with the reference
The syntax for passing in an array via reference to the application is:
void add_min(int (&x)[3]) { ... }
but in this case you need to actually know the size of it, so the array is not dynamically allocated.
To circumvent the need to know the size of the array, we can do some template magic, like:
template<int n>
void add_min(int (&x)[n]) { ... }
but again, this will need to be called with a statically allocated array (int x[124]), not a dynamic one.
But arrays are always passed in via reference, they are never copied to the stack, so your version of the code is correct.
First of all, you're using C++ so you shouldn't use pointers but std::vector. It'll be easier and you won't create memory leaks.
Second, you're passing pointer, not array. There are not the same: https://stackoverflow.com/a/1641963/1351828
So you're pssing the pointer to the first element. If you want to change elements in 'array' you can just pass pointer by value. If you want to allocate new 'array' and change it in a function, pass the pointer by reference:
void add_min(int &x[], int n)
int** function()
{
int M[2][2] = {{1,2},{3,4}};
return (int **)M; //is this valid?
}
void anotherFn()
{
int **p = new int*[2];
for(int i = 0; i<2; i++) {
p[i] = new int[2];
}
p = function();
cout << p[0][0];
}
The above code compiled but gave runtime error. So, can I return a 2D array only if it was declared as double pointer or is there some way I can return an array as a 2D pointer?
You are representing a 2D array as a pointer to pointer to int. That is a bad idea. A better idea is to use a std::vector<std::vector<int>>. Better yet would be to use a dedicated class. But the point is that once you get rid of pointers you can return the value without any problem:
matrix_2d function() {
matrix_2d M = {{1, 2}, {3, 4}};
return M;
}
This works quite well for an appropriate definition of matrix_2d (see above).
Your code makes this whole process much more complicated by using pointers, and accesses invalid memory. In particular, you are allocating memory in your main function, but then you are discarding the pointer to that memory by reassigning it with the result of function(): inside function you aren’t using the previously-allocated memory, you are using stack-allocated memory and returning a pointer to that. Once the function exits, that stack-allocated memory is gone.
To return a 2D array, make sure you dynamically allocate it, then return the pointer. The problem with your code is that you are returning a pointer to a local variable, which will cause problems.
Basically, you'll want to do something like this (skeleton):
int** function()
{
int** M;
// Allocate M here
return M;
}
i ve created two dimensional array inside a function, i want to return that array, and pass it somewhere to other function..
char *createBoard( ){
char board[16][10];
int j =0;int i = 0;
for(i=0; i<16;i++){
for( j=0;j<10;j++){
board[i][j]=(char)201;
}
}
return board;
}
but this keeps giving me error
Yeah see what you are doing there is returning a pointer to a object (the array called board) which was created on the stack. The array is destroyed when it goes out of scope so the pointer is no longer pointing to any valid object (a dangling pointer).
You need to make sure that the array is allocated on the heap instead, using new. The sanctified method to create a dynamically allocated array in modern C++ is to use something like the std::vector class, although that's more complicated here since you are trying to create a 2D array.
char **createBoard()
{
char **board=new char*[16];
for (int i=0; i<16; i++)
{
board[i] = new char[10];
for (int j=0; j<10; j++)
board[i][j]=(char)201;
}
return board;
}
void freeBoard(char **board)
{
for (int i=0; i<16; i++)
delete [] board[i];
delete [] board;
}
The best approach is create a board class and make the ctreateBoard function its constructor:
class Board {
private:
char mSquares[16][10];
public:
Board() {
for(int i=0; i<16;i++){
for( int j=0;j<10;j++){
mSquares[i][j]=201;
}
}
// suitable member functions here
};
For information on how to use such a class, there is no substitute for reading a good book. I strongly recommend Accelerated C++ by Andrew Koenig and Barbra Moo.
This approach will not work. If you return a pointer to a local variable you'll run into undefined behaviour. Instead allocate an array on heap with new and copy data into it manually indexing it.
I would really recommend using STL vector<> or boost/multi_array containers for this.
If you must use arrays, then I would recommend using a typedef to define the array.
typedef char[16][10] TBoard;
You could also return
char**
...but then you would need to typecast it to the correct size in order to index it correctly. C++ does not support dynamic multiple dimension arrays.
Also as others have suggested you can't return an object on the stack (i.e., local variable)
Don't return pointer to a local variable, as other mentioned. If I were forced to do what you want to achieve, first I'd go for std::vector. Since you haven't learnt std::vector, here is another way:
void createBoard(char board[16][10])
{
int j =0;int i = 0;
for(i=0; i<16;i++){
for( j=0;j<10;j++){
board[i][j]=(char)201;
}
}
}
You should return char** instead of char*
The simple answer to your question is char**.
Having said that, DON'T DO IT !
Your "board" variable won't last outside createBoard().
Use boost::multi_array and pass it as a reference to createBoard() or return it directly (but if you do that, it will be copied).
You must not return a pointer to a functions local variables because this space gets overwritten as soon as the function returns.
The storage associated with board is on the function's stack.
I have two questions:
1) How can I make an array which points to objects of integers?
int* myName[5]; // is this correct?
2) If I want to return a pointer to an array, which points to objects (like (1)) how can I do this in a method? ie) I want to impliment the method:
int **getStuff() {
// what goes here?
return *(myName); // im pretty sure this is not correct
}
Thanks for the help!
How can I make an array which points
to objects?
int * myName[5]; /* correct */
If I want to return a pointer to an
array, which points to objects (like
(1)) how can I do this in a method?
Technically, you write this function:
int * (* getStuff() )[5] {
return &myName;
}
That returns a pointer to that array. However, you don't want to do that. You wanted to return a pointer to the first element of the array:
int ** getStuff() {
return myName; /* or return &myName[0]; */
}
That way, you can now access items as you want like getStuff()[0] = &someInteger;
Note that your code,
int* myName[5];
declares an array containing 5 values, each of which is a "pointer to int", which is what you asked.
However this being C++, that's all it does. As a Python scripter, that might cause you some surprises.
It does not give any of those 5 pointers sensible values, and it does not create any integers for them to point to.
If you put it in a function body, then it creates the array on the stack. This means that the array will cease to exist when the current scope ends (which, to put it simply, means when you get to the enclosing close-curly, so for example return does it). So in particular, the following code is bad:
int **myFunction() {
int *myArray[5];
return myArray;
} // <-- end of scope, and return takes us out of it
It might compile, but the function returns a pointer to something that no longer exists by the time the caller sees it. This leads to what we call "undefined behaviour".
If you want the array to exist outside the function it's created in, you could create one on the heap each time your function is called, and return a pointer, like this:
int **myFunction() {
int **myArray = new int[5];
return myArray;
}
The function returns a different array each time it's called. When the caller has finished with it, it should destroy the array, like this:
delete[] myArray;
otherwise it will never be freed, and will sit around using up memory forever (or when your program exits on most OSes).
Alternatively, you can use the keyword "static" to create an array with "global storage duration" (meaning that it exists as long as the program is running, but there's only one of it rather than a new one each time). That means the function returns the same array each time it's called. The caller could store some pointers in it, forget about it, call the function again, and see the same pointers still there:
int **myFunction() {
static int *myArray[5];
return myArray;
}
Note how similar this code is to the very bad code from earlier.
Finally, if you just want to create an array of integers, not an array of pointers to integers, you can do this:
int myArray[5] = { 1, 2, 3, 4, 5};
That actually creates 5 integers (meaning, it assigns space which can store the integer values themselves. That's different from the array of pointers, which stores the addresses of space used to store integer values).
It also stores the specified values in that space: myArray[0] is now 1, myArray[1] is 2, etc.
1) Correct - this is an array of 5 pointers to ints
2) You can return a pointer to an array of pointers to ints by returning a pointer to the first element of that array. This has two levels of indirection, so you need two asterisks. You can also return the array normally, since arrays automatically decay into pointers to their first elements.
int **getStuff() {
return myName; // 1
return &myName[0]; // 2
}
int **myName;
int **getStuff() {
int **array = new int*[5];
for (int i = 0; i < 5; i++)
{
int key = i;
array[i] = &key;
}
return array;
}
Steve Jessop, I think you meant:
int **myFunction() {
int **myArray = new int*[5];
return myArray;
}
This returns a heap array pointer (not pointer to its elements), testable and deletable. Nothing leaks.
template <class T>
T* newarray(int len)
{
T *a;
try
{
a = new T[len];
memset(a,0,len*sizeof(T));
return a;
}
catch (...)
{return 0;}
}
.
.
.
void foo()
{
float *f=0;
f=newarray<float>(1000000);
if(!f) return;
//use f
delete [] f;
}