answersLogoWhite

0

What is meant by passing an argument as a pointer to an object?

Updated: 10/26/2022
User Avatar

RichardIsaacsgp7883

Lvl 1
9y ago

Best Answer

In C++ we can pass arguments to functions in only two ways: by value or by reference. The method is decided by the signature of the function we call. The following examples demonstrate these signatures:

void byval (object val) {}

void byref (object& ref) {}

When we pass arguments to the byval function, we are passing the value of those arguments. Thus the formal argument, val, is always a copy of the actual argument we pass and any changes made to the formal argument are not reflected in the actual argument.

When we pass arguments to the byref function, we are passing the argument itself. Thus the formal argument, ref, is an alias for the actual argument we pass and any changes made to the formal argument are reflected in the actual argument.

To pass an argument by pointer, the formal argument must be a pointer of the same type as the actual argument (or any derivative of that type):

void byptr(object* ptr) {}

If we don't have an actual pointer variable to pass to the function, we must use the address-of operator to obtain an automatic pointer variable that we can pass:

object something;

byptr (&something);

If we don't do this, the compiler will search for a function signature that accepts a value or a reference and will completely ignore the pass by pointer signature., resulting in a compile-time error.

Passing by pointer is essentially an alternative method of passing by reference. However, when we pass by pointer we introduce an extra layer of indirection that we don't get when we pass by reference. This is because we are not actually passing by reference at all, we're passing by value. Pointers are variables that store memory addresses, thus whatever address we pass as the actual argument, that address becomes the value that is copied to the formal argument, ptr. However, because the formal argument and the actual argument both refer to the same object (because they both point to the same memory address), we are effectively passing that object by reference.

Passing by reference is always the preferred method of passing complex objects into functions, because copying complex objects is expensive. Imagine if the object represented a list containing 1 million nodes. That's 1 million copies for the nodes alone. However, we cannot pass by reference if there's even the slightest possibility there may be no object to pass. References cannot be NULL (or if they are, the program is deemed invalid) but pointers can be NULL. So in these cases we can pass by pointer rather than by reference.

Programmers try very hard never to pass function arguments more than 4 bytes in length (or 8 bytes on 64-bit systems). Thus passing primitive variables by value is fine unless you want changes reflected in the actual arguments. But complex objects must always be passed by reference, or by pointer variable, which is also a primitive type in itself because memory addresses are always the same length, depending on the architecture (32/64-bit).

Pointers can also be passed by reference as well as by value. Being able to pass a pointer by value means we can operate upon the object being pointed to, but we cannot change the value of the actual argument itself -- the pointer that was originally passed to the function. To do that we must pass the pointer by reference and we achieve this by introducing yet another layer of indirection, by passing a pointer to pointer variable:

void bypptr(object** pptr) {}

When we do this, pptr now holds the memory address of the pointer we actually passed, not the memory address of the object it pointed to. Thus we can change the memory address stored in that pointer thereby pointing it to another object of the same type, or indeed NULL, just as if we'd passed that pointer by reference.

Remember that pointers are just variables -- they simply store memory addresses. The pointer's type tells us what type of data resides at that address, thus allowing us to access that data via indirection. If that data happens to be another pointer, we have an extra layer of indirection between the pointer to pointer variable and the object being pointed at.

We cannot do this with references. References are easier to work with (provided we can be sure the reference will always be non-NULL) but, once assigned, we cannot reassign them like we can a pointer variable. That is, references must always refer to whatever object we initially assigned to them. But pointers are variables, thus we can reassign them any valid address, including NULL, at any time. So although pointers are slightly more complex than references and consume more memory than references, they are more flexible than references.

New programmers are often confused by the terms reference and pointer. This is understandable, particularly for C programmers moving to C++ because C has no concept of a reference (A C reference is the same as a C pointer). References are perhaps best understood using the following example:

class person {};

person Robert;

Here we've instantiated a person object named Robert. Whenever we refer to Robert we're actually referring to the memory address allocated to Robert. This memory address is not physically stored anywhere (as it would be if we assigned Robert to a pointer variable), we simply identify the address by the name Robert (or rather, &Robert).

person& Bob = Robert;

Here we've assigned Robert to the reference named Bob. Thus Bob and Robert both refer to the same memory address (Robert's address). In other words, Bob is an alternate name, an alias, for the memory allocated to Robert. We don't need the address-of operator to perform the assignment because we're assigning an object to a reference (&), not a pointer (*). The compiler knows the difference.

Note that Bob is neither a variable nor a constant. We haven't actually stored the memory address of Robert as we would were Bob a pointer variable. Since it is not a variable, we cannot reassign Bob to another person object and thus treat the two as being separate people (we can with pointers, but not with references). Indeed, if we did assign another person to Bob, we'd actually be invoking Robert's assignment operator (because Bob refers to Robert), thus Robert will end up copying the member data of that other person object and assigning that data to its own members. Robert then becomes a different person but a person who can still be referred to as either Bob or Robert. They are still one and the same person.

We can take this concept to extremes:

person& Bobby = Bob;

person& Rab = Robert;

person& Rabbie = Bobby;

person& Rob = Bob;

person& Robby = Rab;

Now we can refer to Robert as Rob, Bobby, Rab, Rabbie, Rob or Robby. Note how the reference we assign need not be the name we gave the original person object. They all refer to the same person object so any existing reference will do for assignment. And since references do not store memory addresses like pointers do, we can have as many different references to the same object as we like without consuming any additional memory in order to store the addresses, as we would with multiple pointers to the same object.

We would not normally use multiple references to the same object within the same code block, but passing references to different code blocks (functions) means those code blocks can refer to the same object by alternate names. This is important because the code block will not know the original name assigned to an object, but must refer to it in some way -- thus we use aliases.

void func (person& someone) {}

Thus if we pass Robert (or Bob, etc) into this function, the function will refer to Robert by the alias named someone. In turn, the function might pass someone into another function that perhaps uses the alias anyone. Thus that function refers to Robert by the alias anyone.

With pointers we do exactly the same thing except we pass a physical copy of the address of the person object:

void func2 (person* anyone) {}

Thus the anyone variable will store the address of Robert regardless of which alias (Bob, Rob, etc) we actually pass to the function, like so:

func2 (&Bobby);

Note that although Bobby is a reference (and therefore a memory address), we must use the address-of operator in order to pass by pointer. What we're actually doing is creating an automatic pointer variable whose value can be copied to the function's formal argument, anyone. If we don't use the address-of operator, the compiler will look for a signature that accepts Bobby by reference or by value, but will completely ignore any signature that accepts a pointer, because we didn't actually pass a pointer, we passed a reference.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is meant by passing an argument as a pointer to an object?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How structure is passed to function using structure pointer?

When you pass an object to a function you are not actually passing the object, you are only passing the object's value. This is what is meant by the term pass by value.When passing a value to a function there are actually two objects involved in the exchange: the actual argument (the object that is being passed) and the formal argument (the object used by the function). When we call a function that accepts one or more arguments (also known as parameters), the value of the actual argument is assigned to the corresponding formal argument. Thus the formal argument is a copy of the actual argument and any changes made to the formal argument will have no effect upon the actual argument.When the formal argument is a pointer, however, the value we pass is a memory address. The actual argument can either be a pointer of the same type or we can take the address of an object of the same type using the address-of operator and pass that. Either way, the value we pass is a memory address. We call this pass by reference even though the address is actually being passed by value. Passing by reference means that the formal argument and the actual argument both refer to the same object and offers an efficient means of passing large objects that are too expensive to copy. This includes most structures and unions and all arrays. Note that arrays implicitly decay to pointers and therefore cannot be passed by value. Structures and unions can be passed by value, but if they are larger than a pointer (in bytes) passing by reference is more efficient.There are four different ways to declare a formal argument as a pointer:mutable pointer to mutable typemutable pointer to constant typeconstant pointer to mutable typeconstant pointer to constant typeIdeally, functions should declare formal pointer arguments using methods 2 or 4. Both point to constant types so this gives the caller an assurance that the function has no side effects upon the object being passed by reference. Functions that use methods 1 or 3 should be regarded as having side-effects upon the object being referred to. This can be desirable for efficiency reasons, however returning values via arguments (also known as output parameters) should be avoided whenever possible.Note that it makes no difference if the formal pointer argument is mutable or constant because the formal and actual arguments are still separate objects. Constant formal arguments are only of relevance to the function designer, they are of no importance to the caller. This is true of all values passed to functions whether the value is a memory address or not. What is important to the caller of a by reference function is whether or not the object being pointed at is declared constant or not.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


What is the different between header file and header pointer?

Header file is a file which is meant to be included into another file during compilation. Examples: string.h, stdio.h, inttypes.h. Header pointer is a pointer to an object called header (for example header of a linked list).


What is the Operator c plus plus?

c++ is not an operator, it is a programming language (the successor to the c programming language upon which it was based). However, if c were really an object or variable name, then ++ would be the postfix increment operator. c++ returns the value of c before incrementing c. Its counterpart, ++c, invokes the the prefix increment operator, which returns the value of c after incrementing. ++c is a convenient shorthand notation for c=c+1, which can also be written c+=1, all of which return the value of c after incrementing. c++ is also a convenient shorthand for c=c+1 or c+=1, but the return value is the original value of c, not the incremented value.


What is meant when you say you dereference a pointer variable?

When you dereference a pointer you are referring to the object being pointed at. int* ptr = new int (42); int& ref = *ptr; You must be careful when you dereference pointers like this because ptr is volatile and could be deleted at any time. If ref is still in scope after ptr is released, ref will refer to invalid memory and thus invalidates your program. Moreover, if ptr falls from scope without releasing the memory it points to and ref remains in scope, there's no way to release the memory without invalidating ref which, again, invalidates your program. In order to be valid, references to dereferenced pointer values must be placed in a separate scope from the pointer and must fall from scope before the pointer falls from scope. Even if a reference falls from scope immediately after a pointer falls from scope, your program is still deemed invalid. In some cases there is no need to hold a reference to a dereferenced pointer value, you can simply store the value by copying it to another variable. int i = *ptr; However, if the pointer is a complex object, keep in mind that this could invoke a cascade of copy constructors, which may impact performance. The following code snippet shows all the major properties of a pointer: its own address, its value (the address being pointed to) and the indirect value (the dereferenced value). std::cout << "Address: &ptr = 0x" << &ptr << std::endl; std::cout << "Value: ptr = 0x" << ptr << std::endl; std::cout << "Dereference: *ptr = " << *ptr << std::endl;

Related questions

How structure is passed to function using structure pointer?

When you pass an object to a function you are not actually passing the object, you are only passing the object's value. This is what is meant by the term pass by value.When passing a value to a function there are actually two objects involved in the exchange: the actual argument (the object that is being passed) and the formal argument (the object used by the function). When we call a function that accepts one or more arguments (also known as parameters), the value of the actual argument is assigned to the corresponding formal argument. Thus the formal argument is a copy of the actual argument and any changes made to the formal argument will have no effect upon the actual argument.When the formal argument is a pointer, however, the value we pass is a memory address. The actual argument can either be a pointer of the same type or we can take the address of an object of the same type using the address-of operator and pass that. Either way, the value we pass is a memory address. We call this pass by reference even though the address is actually being passed by value. Passing by reference means that the formal argument and the actual argument both refer to the same object and offers an efficient means of passing large objects that are too expensive to copy. This includes most structures and unions and all arrays. Note that arrays implicitly decay to pointers and therefore cannot be passed by value. Structures and unions can be passed by value, but if they are larger than a pointer (in bytes) passing by reference is more efficient.There are four different ways to declare a formal argument as a pointer:mutable pointer to mutable typemutable pointer to constant typeconstant pointer to mutable typeconstant pointer to constant typeIdeally, functions should declare formal pointer arguments using methods 2 or 4. Both point to constant types so this gives the caller an assurance that the function has no side effects upon the object being passed by reference. Functions that use methods 1 or 3 should be regarded as having side-effects upon the object being referred to. This can be desirable for efficiency reasons, however returning values via arguments (also known as output parameters) should be avoided whenever possible.Note that it makes no difference if the formal pointer argument is mutable or constant because the formal and actual arguments are still separate objects. Constant formal arguments are only of relevance to the function designer, they are of no importance to the caller. This is true of all values passed to functions whether the value is a memory address or not. What is important to the caller of a by reference function is whether or not the object being pointed at is declared constant or not.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


What is the different between header file and header pointer?

Header file is a file which is meant to be included into another file during compilation. Examples: string.h, stdio.h, inttypes.h. Header pointer is a pointer to an object called header (for example header of a linked list).


What is meant by an pointer in c program?

Pointer is like variable address the members in memory shell


What is meant by technocrats?

what argument against technocracy


What is meant by a multiplexed address and data bus?

[object Object]


What is meant by the mass of an object?

It's essentially how much of the object there is.


What is meant by unpaid claims?

When you claim for money but you lose you argument and get no money.


Is it true or false that assigning an address to a pointer variable is valid operation?

Pointers are meant to store adresses.


What is meant by an object has mass 6000 gm?

an object weighs 6000 gm


What is meant by an object has weight 80 newtons?

an object has its weigh 80 n


What is the Operator c plus plus?

c++ is not an operator, it is a programming language (the successor to the c programming language upon which it was based). However, if c were really an object or variable name, then ++ would be the postfix increment operator. c++ returns the value of c before incrementing c. Its counterpart, ++c, invokes the the prefix increment operator, which returns the value of c after incrementing. ++c is a convenient shorthand notation for c=c+1, which can also be written c+=1, all of which return the value of c after incrementing. c++ is also a convenient shorthand for c=c+1 or c+=1, but the return value is the original value of c, not the incremented value.