Why are pointers modified in a function ? - c++

As a beginner in C++, first thing I came accross for functions is that they use a copy of the argument, for example if we execute this :
void add (double x){
x=x+3;
}
int main(){
double x=2 ;
add(x) ;
}
Then x would actually be equal to 2, not 5. Later on, learning about pointers, I found the following :
void fill (int *t){
for (int j=0, j<10, j++){
t[j]=j;
}
int main(){
int* p;
p = new int[10];
fill(p);
}
Now if we print what p contains, we find that it's indeed filled by the integers. I have some trouble understanding why it does that as I feel like it should have been the same as the first function ?
Thanks.

The reason it isn't the same as the first function is because you are passing the pointer by value. This means that if you modify the actual pointer, e.g by assigning to it, then it would only be in that state inside the function. The value that the pointer points to is still the original value, which will get modified since both copied pointers point to that same original value (don't forget notation of the form a[i] is equivalent to *(a + i), which does a dereference and is modifying the pointed value, not the pointer itself).
A small example that illustrates this would be the following (not accounting for memory leaks):
#include <iostream>
int test(int* x)
{
int* y = new int{10};
x = y;
std::cout << "Inside function: " << *x << "\n";
}
int main()
{
int* t = new int{5};
std::cout << "Before function: " << *t << "\n";
test(t);
std::cout << "After function: " << *t << "\n";
}

In first example you are using ordinary variable. When passing normal variable to function, like this, the function creates its own copy of the variable (it has same value as it has when you passed it, but it was copied to different place in memory). That is standard behaviour.
In second example you are using pointer: we can say that it points to the place in memory, where values are stored. Few of the advantages of them:
1) If you want to spare memory, but need to use same value in different functions -> mostly appplies to bigger objects than double, like array in your example
2) If you need to change value of variable/array/object in different functions
But careful, in the second function you still created copy, but not of value, but pointer. So basically, the "new" pointer is different object, but it is pointing to the same place in memory, so when accessing the value (which you are doing with [j]), you are editing the same place in memory.
It is not that easy concept to grasp, especially with more dimentional arrays, but hope this helped a little. You can learn some more in tutorials or c++ docs, for example this is a good one: https://www.geeksforgeeks.org/pointers-in-c-and-c-set-1-introduction-arithmetic-and-array/

Related

Use the new operator to allocate an array of integers using the parameter as the size of the array within a function

I've been stuck on this problem and I'm hoping someone can explain where I'm wrong on this. I'm working on an assignment where I need to:
1) Allocate an array that can contain 100 int values by calling allocIntArray and assign the returned pointer to ptr1.
2) Use the new operator to allocate an array of integers using the parameter as the size of the array.
3) Return the pointer that is returned by the new operator.
4) Print out the new array.
I'm trying to print out the array after passing the size I want through the function.
int main() {
int *ptr = NULL;
ptr1 = *allocIntArray(100);
cout << ptr1 << endl;
return 0;
}
//The function I want to call
int *allocIntArray(int size) {
int *newarr = nullptr;
newarr = new int[size];
return newarr;
}
However when I call the function, the output comes out as 00F011E8.
I'm currently trying to understand why this is the output and not the first value in the array. (Just the number 1)
I've been having a lot of trouble grasping pointers any help understanding would be greatly appreciated.
Thanks to everyone who took the time to respond.
From what I understand from assignment directions, I shouldn't need to use vectors. I'm trying to modify my current code to display the array output and this is what currently comes up when I run it.[enter image description hereMy current results
At first, if you're using C++, you should use std::vector/std::array. This avoids a huge amount of possible problems.
It would look like:
#include <iostream>
#include <vector>
int main()
{
// create array of size 10 and initialize it with 0's
std::vector<int> vec(10, 0);
// print array
for(auto a : vec)
std::cout << a << '\t';
std::cout << std::endl;
}
If it's some kind of exercise, you have done four big mistakes:
you dereference the returned pointer to the array. So you get the value of the first element in the array and not the array itself. Simply remove the *.
you print out the address of the first element of the array. To print the array, you have to iterate over each element of the array. This can be done in a for loop:
for(int i = 0; i < 10; ++i)
std::cout << ptr1[i] << '\t';
you want to print out the array uninitialized. In fact, you try to print out some random values which are there in the memory. At first, you have to assign the elements values.
you forget to delete the array by using
delete[] ptr1;
I just have to answer as you seem to be missing some important fundamentals. Either the instructor should be dismissed or you have not paid enough attention in the class. So...
ptr1 = *allocIntArray(100);
You could not have pasted code that compiles, ptr1 is not declared.
You need to understand what the * operator does. What ever value to the right of * must be a pointer. What a pointer is should be fundamental in your understanding. If you had:
int* ptr1 = *allocIntArray(100);
You should have gotten a compiler error, so you must have:
int ptr1;
Somewhere along the line. As allocIntArray(...) returns a pointer, then *allocIntArray(...) gives you an integer.
You would have wanted to:
int* ptr1 = allocIntArray(100);
To get a pointer to the new array. Then you:
std::cout << ptr1 << std::endl;
So, what is ptr1? If it is a pointer then all you are doing is printing the pointer value. Per your stated problem, I'd say ptr1 is in fact a pointer. std::cout has not facility to work with a pointer as you expect. At that, it would have no way of determining the size of your array.
You would want to (And it hurts my fingers to write like this):
for(size_t i= 0; i < 100; ++i)
std::cout << ptr1[i] <<" ";
But!!!
4) Print out the new array.
So what will it print? There was never an instruction to initialize the array. It will print out what ever garbage is sitting in the array when it was created.
Side note, that the instructor has you doing a:
using namespace std;
Says much, as he/she should never have allowed it.

C++ array as parameter reference by default?

forgive me for asking such a simple question, but I couldn't connect the dots from previous answers on SO or other sites. I have read that arrays are passed by reference by default and elsewhere I have read that arrays decay to pointers when passed into functions. I am trying to pass an array to a function and modify it, but cannot reconcile the previous two statements. I am not sure whether I am passing in a pointer or reference to toBin and whether it even matters. The following code is my attempt at making changes to the b array in the toBin function.
When I print the modified array, I get a whole bunch of unexpected text much bigger than the original allocated array of size 11 eg 1000000000 submarine blahblahblah. My expected output is 1000000000.
void toBin(int x,char a[]){ //passed by reference by default
std::cout << a << std::endl;
for (int i=9;i>=0;i--){
if(pow(2,i)<=x){
x=x-pow(2,i);
a[9-i]='1'; //i-1 because char b[] is zero indexed
};
}
}
int main()
{
char c[]="submarine";
double combination = pow(2,sizeof(c)-1);
char b[11]={'0','0','0','0','0','0','0','0','0','0'};
toBin(512, b);
for (int i=0;i<combination;i++){
std::cout << *(b+i) << std::endl;
}
}
Basically you pass everything to the function well. Thing that broke your output it's loop itself.
double combination = pow(2,sizeof(c)-1);
...
for (int i=0;i<combination;i++){
std::cout << *(b+i) << std::endl;
}
Your combination variable can have value like 2^8. So you point by*(b+i) to the address going far beyond the allocated array. To repair it you need change your loop to that:
for (int i=0;i< sizeof(b);i++){

Which would you choose and why? Passing/Declaring by pointer/ref combinations

I'm researching the differences between stack and heap allocation, which has its own long list of disputes on when to use which. My question is not related to that discussion though; it is about the semantics of testing these two ideas.
Consider the following situation describing the usual ways variables are passed between functions:
void passByPtr(Node* n)
{n->value = 1;}
void passByRef(Node& n)
{n.value = 2;}
void passByValue(Node n)
{n.value = 3;}
void indirectionTesting()
{
Node* heapNode = new Node();
Node stackNode= Node();
// Initialize values
heapNode->value = 0, stackNode.value = 0;
cout << heapNode->value << ", "<< stackNode.value << endl;
// Passed ptr by ptr (pass by reference)
passByPtr(heapNode);
passByPtr(&stackNode);
cout << heapNode->value << ", "<< stackNode.value << endl;
// Pass by reference
passByRef(*heapNode);
passByRef(stackNode);
cout << heapNode->value << ", "<< stackNode.value << endl;
// Pass by value
passByValue(*heapNode);
passByValue(stackNode);
cout << heapNode->value << ", "<< stackNode.value << endl;
delete heapNode;
}
int main()
{
indirectionTesting();
/* Output
0, 0
1, 1
2, 2
2, 2 */
return 0;
}
These are the two ways I was taught how to handle variable passing between methods. There are a lot of arguments for and against here. I read all of them.
But I had an idea... you see, I'd prefer for the caller to be able to easily out whether or not a function is conceptually pass-by-value or pass-by-reference. (i.e. (func(&var)) In my humble opinion, the transparency supplements the idea of encapsulation.
But there's a problem. The 'new' keyword always returns a pointer. As you can tell from the above example, if you use a mix of stack-allocated and heap-allocated variables in your program, it can quickly become confusing how to pass them by reference.
So after playing around with reference types, now consider this portion of code:
void passByPtr(Node* n)
{n->value = 1;}
void passByRef(Node& n)
{n.value = 2;}
void passByValue(Node n)
{n.value = 3;}
void indirectionTesting()
{
Node& heapNode = *new Node();
Node stackNode= Node();
// Initialize values
heapNode.value = 0, stackNode.value = 0;
cout << heapNode.value << ", "<< stackNode.value << endl;
// Passed ptr by ptr (pass by reference)
passByPtr(&heapNode);
passByPtr(&stackNode);
cout << heapNode.value << ", "<< stackNode.value << endl;
// Pass by reference
passByRef(heapNode);
passByRef(stackNode);
cout << heapNode.value << ", "<< stackNode.value << endl;
// Pass by value
passByValue(heapNode);
passByValue(stackNode);
cout << heapNode.value << ", "<< stackNode.value << endl;
delete &heapNode;
}
int main()
{
indirectionTesting();
/* Output
0, 0
1, 1
2, 2
2, 2 */
return 0;
}
Ignoring how strange the heap-allocated variable instantiation looks, does anyone see anything wrong with this?
They both function exactly the same. The way I see it, choosing to follow this type of convention allows me to freely choose whether to allocate on the stack or heap whilst allowing me to use the same calling convention for all of my functions, again regardless whether or not they are heap or stack allocated. I don't personally see any validity in the argument that "I will confuse which variables were allocated on the stack and which on the heap," but share your opinion if you think I'm wrong.
Does anyone see anything else wrong with this approach?
I recommend passing by reference to functions. A reference indicates that the object exists.
When passing by pointer, the receiving function doesn't know if the pointer is valid or not and must test the pointer.
The passing by reference is a safer and more robust style.
If you really want to do this:
Have the caller determine whether the parameter is by ref, value or ptr
Be able to swap object types between stack and heap
Then I think you might be able to do something along these lines:
auto my_node = make_object<Node>(); // factory hides value/heap creation
Then you'd make every object created be wrapped by a class which has pointer semantics. So if my_node is on the heap, it's wrapped so everything uses my_node-> or *my_node (even though the wrapper is just storing it as a member variable).
Since you are now working with wrapped types, a function might have a signature like:
value<Node> my_function( parameter<Node> );
I haven't thought through the details of how these classes might operate but I was thinking something along the lines of:
parameter can only be constructed from a reference, value or ptr. So the caller then specifies which of these it wants and some mechanics under the covers deal with getting the parameter through in the correct form.
// in the function with the my_node object
auto ret = my_function( pass_by_reference(my_node) );
I'm sure there will be some tricky bits in the implementation but on a surface level I think something along these lines would let you overload the make_object so different object types are associated with heap/stack as desired.
You wouldn't be able to work with anything like it is a reference - you'd have to deal with pointer syntax because its the lowest common denominator. However you could retain the semantics of something being a reference or a pointer (can be made null or rebound) or a value (on the stack).
Clearly there would be some inefficiency somewhere because the generic 'parameter' type would need to be able to operate in 3 different 'modes'.
But you know - you are trying to make the language do something it wasn't designed to. However the strength of C++ is that where theres a will, theres a way.
Maybe someone will tell me why the above will not work. I'd be happy to hear that but on a cursory consideration I think it could be made to work.
pass by pointer as last resort.
the best option - pass by reference as much as possible. references prevents you passing null pointers and dangling pointers. it also prevent you doing the bad bad practice of passing null as "no valid input".
there are also places you MUST pass by const reference, like in any function that gets temporary object as parameter, or copy constructors
also, every object that weight more than sizeof(void*) can benefit by passing it by reference - the reference pass will cost less byte-copying thus a faster program. pass heavy objects as references if they need to change and as const references if they do not.
if you can't pass by reference , try pass by smart pointers - std::weak_ptr and std::shared_ptr (you cant pass std::unique_ptr, or at least, you shouldn't).
you pass by raw pointer when :
you can't pass by reference, for instance like passing this
null is a legit argument
C-API's
you don't pass either by pointer nor reference if:
you do want your object to be copied
there is no performance improvement. for example, I pass a character and I don't want it to be changed, yet , passing it as const reference makes the program slower (references are usually implemented behind the scenes as pointers, and they weight many times heavier than single character)
few more things:
you CAN catch a new result with reference, just dereference it first:
int& x = *new int(4)
don't use new/delete unless you're dealing with performance. use smart pointers instead (std::shared_ptr,std::unique_ptr).
even if you do have a pointer as variable (for example, if you imlement linked list or singleton and you use raw pointers) , you can still and should return it as reference:
T& T::getInstance(){
if (!m_Instance){
m_Instance = new T();
}
return *m_Instance;
}
and again , the above code CAN AND SHOULD be used with smart pointers

C++ Passing Static Array and Dynamic Array By Reference

To fully understand how pointers, values, and references work, I am making a basic C++ program that attempts to tamper with some static and dynamic arrays and understand exactly how they should be passed in.
First I generate a static array of 3 elements. I then pass it into a function that modifies all elements. I then pass it into another function with a slightly different signature, but can also alter the array's values.
Next I generate a dynamically sized array, pass it into a function by reference so that all of the values in this dynamically sized array can be altered.
The code is as follows:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void changeIndexStaticArrayMethod1(int* stat);
void changeIndexStaticArrayMethod2(int (&stat)[3]);
void changeIndexDynamicArrayMethod1(int* dyn, int size);
int main() {
const int MAX = 3;
int arr[MAX] = { 1,2,3 };
changeIndexStaticArrayMethod1(arr);
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << arr[2] << endl;
cout << endl;
changeIndexStaticArrayMethod2(arr);
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << arr[2] << endl;
int SIZE;
cout << "Please choose a size for the array" << endl;
cin >> SIZE;
int *ne = new int[SIZE];
//Build array
for (int i = 0; i < SIZE; i++) {
ne[i] = i;
}
changeIndexDynamicArrayMethod1(ne, SIZE);
for (int i = 0; i < SIZE; i++) {
cout << "ne[" << i << "] = " << ne[i] << endl;
}
//To hang program
cin >> SIZE;
delete[] arr;
delete[] ne;
return 0;
}
void changeIndexStaticArrayMethod1(int* stat) {
stat[0] = 10;
stat[1] = 20;
stat[2] = 30;
}
void changeIndexStaticArrayMethod2(int (&stat)[3]) {
stat[0] = 40;
stat[1] = 50;
stat[2] = 60;
}
void changeIndexDynamicArrayMethod1(int* dyn, int size) {
for (int i = 0; i < size; i++) {
dyn[i] = i * 10;
}
}
All of the above code works how I want it to, I just have a few questions as to why (some of the methods of passing arrays by reference I have found on other SO questions).
In the changeIndexStaticArrayMethod1() and changeIndexDynamicArrayMethod1() functions, why are we able to use the dereference * operator for our array as reference? My knee jerk reaction is seeing that as practically passing the array in by values since it is the dereference operator. I know that with arrays, it is much different than using variables, but also, why will the following not work for single int variables:
void changeStaticNumber(int* num){
num = 100;
}
Obviously the above will work if we use &num and not int* num, and obviously I don't fully understand the relationship between pointers and arrays, but I cannot figure out why when we pass an array by reference, int* staticArray is ok.
Any explanation for these problems I am having would be much appreciated. Thanks.
why are we able to use the dereference * operator for our array as reference?
The * in C means many things. It can mean the unary indirection ("contents of") operator, it can mean the binary multiplication operator and it can mean a pointer declaration. The int* stat is a pointer declaration.
Since you aren't using the * to dereference the contents of the pointer inside that function, I'm not quite sure what you are asking.
When you take the array name of your array in main(), it "decays" into a pointer to the first element. So what those function do, is to take a pointer by value. If you dereference the pointer by typing *stat = something; you access the actual array in main.
Should you do something weird like changing the pointer itself, for example stat++;, then it will not affect the address used in main. You passed the pointer itself by value, so the pointer is a local copy.
My knee jerk reaction is seeing that as practically passing the array in by values since it is the dereference operator.
You can't really pass arrays by value in C or C++, without resorting to dirty tricks (storing them inside structs or classes). For example, had your function been written as void changeIndexStaticArrayMethod1(int stat[3]) it would still give you a pointer to the first element. It will not pass an array by value, as the syntax might trick you into believing.
why will the following not work for single int variables:
void changeStaticNumber(int* num){ num = 100; }
Because num is the pointer itself, not its contents. In order to write code like that, you could pass the variable by reference int& num. Behind the lines this is really the same thing as passing a pointer, just with simplified syntax.
To understand the relation between pointers and arrays better, start by reading this whole chapter: http://c-faq.com/aryptr/index.html (C and C++ are identical when it comes to pointers).
Let me see if I can take a stab at this.
Pointers are simply address holders. Once you do int * ptr = myarray; --- what you are in tern doing is storing the address of the pointer my array into ptr --- array names are actually pointers to the first memory location in the array. You can use pointer arithmetic to get at everything else for example myarray +1 will point you to the next location or myarray[1].
Passing by value is not very useful when you need to modify your array. Passing in by reference is essentially making a pointer to the array and passing that. Since arrays like vectors are contiguous blocks of memory you can index through them rather easily.
As far as your example goes void changeStaticNumber(int* num){ num = 100; } will not work because what you are attempting to do is store 100 into the pointer's address. If you deference num and make it void changeStaticNumber(int* num){ *num = 100; } it will work because you are actually going one step further and accessing the data that num is pointing to. When you use &num it is essentially the same thing - & just gives you the address of something.
For example if you want to point a pointer to an int what you would do is
int num = 5;
int *ptr = &num;
at this point in time ptr has the same address in num. To print out the data in num or that ptr is pointing to you need to dereference or go one step further as I like to tell myself and dereference to so cout << *ptr;
In both changeIndexStaticArrayMethod1 and changeIndexDynamicArrayMethod1 you are not passing an array there is no pass by reference (which only happens if the parameter type is a reference type -- i.e. with &). The parameter has type int * (pointer to int). You are passing a pointer to int by value. There is no "dereference operator" in either function.
ne is already an int *, so passing it is nothing special. arr is an int [3], an array, not a pointer. In C, when an array-of-T is used in a context that expects a pointer-to-T, it is implicitly converted (without you needing to do anything) to a pointer to its first element. So when you do, changeIndexStaticArrayMethod1(arr), the compiler gets a pointer to the first element of arr, and passes that to the function.
The [] operator works on pointers. a[i] is always guaranteed to be the same as *(a + i). Inside both the changeIndexStaticArrayMethod1 and changeIndexDynamicArrayMethod1 functions, [] is used to access subsequent elements using a pointer to the first element.

C++ pointer question

still working at C++, but this came up in my book and I don't understand what it's for:
MyClass * FunctionTwo (MyClass *testClass) {
return 0;
}
My question is what is the signifigance of the first indirection operator
(MyClass *[<- this one] FunctionTwo(MyClass *testClass))?
I tried making a function like it in codeblocks, with and without the first * and I didn't see any difference in the way it ran or it's output:
int *testFunc(int *in) {
cout << in << endl;
cout << *in << endl;
return 0;
}
int testFuncTwo(int *in) {
cout << in << endl;
cout << *in << endl;
return 0;
}
I couldn't find anywhere that explained about it in my book.
Thanks,
Karl
The MyClass * means that this function returns a pointer to a MyClass instance.
Since the code is actually returning 0 (or NULL) this means that any caller to this function gets back a NULL pointer to a MyClass object.
I think the following are better examples:
int *testFunc(int *in) {
cout << "This is the pointer to in " << in << endl;
cout << "This is the value of in " << *in << endl;
return in;
}
int testFuncTwo(int *in) {
cout << "This is the pointer to in " << in << endl;
cout << "This is the value of in " << *in << endl;
return *in;
}
void test() {
int a = 1;
cout << "a = " << a;
int output = *testFunc(&a); // pass in the address of a
cout << "testFunc(a) returned " << output;
output = testFuncTwo(&a); // pass in the address of a
cout << "testFuncTwo(a) returned " << output;
}
Apologies, but I've not done C++ in years but the syntax may be a little off.
The general format for a function that takes a single value and returns a single value in C++ is:
return_type function name ( parameter_type parameter_name )
In your case, return_type is MyClass *, which means "pointer to MyClass"
In addition, in your case, parameter_type is also MyClass *.
In other words, your code could be rewritten as:
typedef MyClass *PointerToMyClass;
PointerToMyClass FunctionTwo (PointerToMyClass testClass)
{
return 0;
}
Does that look more familiar?
Given the nature of the question, I'm going to provide a somewhat crude answer.
A pointer points to something:
int x = 123; // x is a memory location (lvalue)
int* y = &x; // y points to x
int** z = &y; // z points to y
In the above code, z points to y which points to x which stores an integral, 123.
x->y->z[123] (this is not code, it's a
text diagram)
We can make y point to another integer if we want or NULL to make it point to nothing, and we can make z point to another pointer to an integer if we want or NULL to make it point to nothing.
So why do we need pointers which point to other things? Here's an example: let's say you have a simple game engine and you want to store a list of players in the game. Perhaps at some point in the game, a player can die by having a kill function called on that player:
void kill(Player* p);
We want to pass a pointer to the player, because we want to kill the original player. Had we done this instead:
void kill(Player p);
We would not kill the original player, but a copy of him. That wouldn't do anything to the original player.
Pointers can be assigned/initialized with a NULL value (either NULL or 0) which means that the pointer will not point to anything (or cease to point to anything if it was pointing to something before).
Later you will learn about references which are similar to pointers except a pointer can change what it points to during its lifetime. A reference cannot, and avoids the need to explicitly dereference the pointer to access the pointee (what it's pointing to).
Note: I kind of skirted around your original question, but the sample code you provided has no inherent meaningful behavior. To try to understand that example without first understanding pointers in a general sense is working backwards IMHO, and you'd be better to learn this general theory first.
If you had something like int * Function() this would return a pointer to an integer. MyClass * just means that the function is going to return a pointer to an object of type MyClass. In c++ user defined objects are treated as first-class-objects, so once you create your own objects they can be passed as a parameter, returned from a subroutine, or assigned into a variable just like the standard types.
In the code blocks you posted there will be no difference in the output to the console because the thing you have changed is the type that the function returns (which is not used), the contents of the functions are identical.
When you define:
int *testFunc(int *in)
You are defining a function which returns a pointer to an int variable.
int testFuncTwo(int *in)
Returns just an int variable.
Zero in this case is undergoing an implicit cast - the first function returns (int*) 0, the second just 0. You can see this implicit cast in action if you change the prototype of your example function from returning a MyClass* to just returning a MyClass - if there's no operator int method in MyClass, you'll get a nice error.