answersLogoWhite

0


Best Answer

All references must be non-null, therefore they will always have memory allocated to them. A reference is simply a non-null memory location that stores a specific type of variable, as determined by the reference's type. The reference name is an alias (a token) for the memory location, in much the same way that an array name is an alias for the starting address of the array.

A pointer variable is different in that memory must be set aside for the pointer variable itself, in addition to the memory it actually points to. On a 32-bit system, 4 bytes must be allocated to every pointer variable, to store the memory address that they point to, which could be null. But references only occupy the memory they actually refer to, which can never be null (a null reference will in fact render the program invalid).

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Do reference variables occupy memory in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the differences between C and Java reference variables?

Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.


Are reference variables are NOT available in C?

Yes, reference variables are permitted in C. However a reference variable in C is simply another name for a pointer variable, because pointers can be dereferenced to allow indirect access to the memory they point at. However, in C++, references are neither pointers nor variables, they serve another purpose altogether, one which is not available in C. So the real answer is that C++ references are not available in C, but reference variables are. Although it is common practice to avoid using the term reference lest there be any confusion with a C++ reference, it doesn't alter the fact that a C pointer variable is also a C reference variable. They are one and the same thing. In C++, a reference is entirely different from a C reference. More specifically, it is an alias, an alternate name, for a memory address. Unlike pointer variables, a reference consumes no memory of its own. That is, you cannot obtain the address of a reference since there is none to obtain. Hence the 'address of' operator (&) is used during instantiation to signify that the instance name is actually a reference of the specified type, and not a variable of the type. The address being referred to must also be assigned to the reference when the reference is instantiated. That is, you cannot declare a reference and then assign a memory address to it elsewhere in your code. By the same token, you cannot subsequently reassign a reference while it remains in scope. Remember, references are not variables. What they refer to may be variable, but not the reference itself. In that respect it is not unlike like a constant pointer to a variable. References allow you to refer to the same memory address using one or more aliases. Since they consume no memory of their own, there is no practical limit to the number of aliases you can have in your C++ program. When compiled, all references to the same memory are effectively replaced with the actual memory address, or the address of a static location containing the actual memory address if the address was allocated dynamically. So while references are often implemented just as if they were pointers, this is only of concern to developers of C++ compilers. In more general C++ source code, the two concepts must be kept distinct. C++ references are also much easier to work with than pointers. Whereas pointers to allocated memory must be manually released when no longer required, references do not. When a referenced object falls from scope, the object's destructor is called automatically. More importantly, references can never be NULL so, if you have a reference, a valid object of the specified type is guaranteed to exist at the referred memory location. The only time you will encounter problems with references is when you point at them, and then delete the pointer! If you subsequently access the referenced memory or the reference falls from scope, your program will exhibit undefined behaviour. Obviously this is a programming error, but these type of problems exist with pointers in general, hence pointers are best avoided as much as is possible. If an object is guaranteed to exist, then refer to it, don't point at it. Only use a pointer when an object is not guaranteed to exist, or if you need to change the address being referred to. And if a pointer is non-NULL, refer to the object.


Can local variables be passed between subroutines?

If this is a homework assignment, please consider trying it yourself first, otherwise the value of the reinforcement to the lesson offered by the homework will be lost on you. Since C and C++ pass parameters by value, local variables can be passed between functions (subroutines). If you intend to pass them by reference, i.e. by passing their address, you must ensure that they do not go out of scope while the called function runs, otherwise the called function might reference memory that is no longer valid.


What is a pointer variable in C?

Pointer variables point to data variables. They are mostly used to point to dynamically allocated data variables, but can actually point to anything (e.g. statically allocated variables, array elements, anywhere inside a variable, program machine code, I/O device descriptors, nonexistent memory). Misuse of pointer variables, either unintentionally or intentionally, is a major cause of nearly impossible to debug software problems in programs written in C (and C++).


What is the difference between reference variable and painter variable?

Reference VariableIt does not have separate memory other than variableIt is just an alias created to existing variableIt is not present C languagePointer VariableIt has separate memory other than variableIt actually stores address of the variableIt is present in C as well as C++.

Related questions

What are the differences between C and Java reference variables?

Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.


Explain reference variable and how it is different from pointer variable?

In JAVA, all variables are reference variables, and there are no pointer variables. Even though the platform may implement them as pointers, they are not available as such. In C, no variables are reference variables. They are a C++ enhancement. In C++ a reference variable is syntactically the same as a pointer variable, except that the use of the indirection operator (*) is implicit. You do declare reference variables slightly differently than pointer variables but, once you do so, they can be treated as non-pointer variables. Reference variables also cannot be redefined once they have been initialized to point to some object. They are const. Structurally, there is no difference between a pointer variable and a reference variable. They are both still pointers. The compiler just makes it easier to treat reference variables and non-pointer variables the same way.


WHAT IS POINTER TO POINTER IN C POINTER?

Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.


Are reference variables are NOT available in C?

Yes, reference variables are permitted in C. However a reference variable in C is simply another name for a pointer variable, because pointers can be dereferenced to allow indirect access to the memory they point at. However, in C++, references are neither pointers nor variables, they serve another purpose altogether, one which is not available in C. So the real answer is that C++ references are not available in C, but reference variables are. Although it is common practice to avoid using the term reference lest there be any confusion with a C++ reference, it doesn't alter the fact that a C pointer variable is also a C reference variable. They are one and the same thing. In C++, a reference is entirely different from a C reference. More specifically, it is an alias, an alternate name, for a memory address. Unlike pointer variables, a reference consumes no memory of its own. That is, you cannot obtain the address of a reference since there is none to obtain. Hence the 'address of' operator (&) is used during instantiation to signify that the instance name is actually a reference of the specified type, and not a variable of the type. The address being referred to must also be assigned to the reference when the reference is instantiated. That is, you cannot declare a reference and then assign a memory address to it elsewhere in your code. By the same token, you cannot subsequently reassign a reference while it remains in scope. Remember, references are not variables. What they refer to may be variable, but not the reference itself. In that respect it is not unlike like a constant pointer to a variable. References allow you to refer to the same memory address using one or more aliases. Since they consume no memory of their own, there is no practical limit to the number of aliases you can have in your C++ program. When compiled, all references to the same memory are effectively replaced with the actual memory address, or the address of a static location containing the actual memory address if the address was allocated dynamically. So while references are often implemented just as if they were pointers, this is only of concern to developers of C++ compilers. In more general C++ source code, the two concepts must be kept distinct. C++ references are also much easier to work with than pointers. Whereas pointers to allocated memory must be manually released when no longer required, references do not. When a referenced object falls from scope, the object's destructor is called automatically. More importantly, references can never be NULL so, if you have a reference, a valid object of the specified type is guaranteed to exist at the referred memory location. The only time you will encounter problems with references is when you point at them, and then delete the pointer! If you subsequently access the referenced memory or the reference falls from scope, your program will exhibit undefined behaviour. Obviously this is a programming error, but these type of problems exist with pointers in general, hence pointers are best avoided as much as is possible. If an object is guaranteed to exist, then refer to it, don't point at it. Only use a pointer when an object is not guaranteed to exist, or if you need to change the address being referred to. And if a pointer is non-NULL, refer to the object.


What is turbo c variables?

Turbo C variables are memory place holders for storage of data during the execution of a Turbo C program. Types of variables include integer, real and char.


When does the local variables gets memory in C?

When entering the function; they are on the stack.


What mean by reference function in c?

Reference function has no meaning. Variables are passed to functions by reference or by value, depending on the function signature.


What is union in c program?

It's a fragment of memory shared by multiple variables.


What is the Conceptual Memory Map of a C Program?

C++ has 4 distinct regions for memory distribution Stack : This region is used for function calls' return addresses , arguments and local variables Heap : This region is for dynamic allocation of memory (dynamic variables created on run time use this memory , aka RAM) Global Variables : This is used for global variables defined by the programmer Program Code : This region is for the program code.


What is a pointerin c?

Pointer in C are fun to programming.Pointers in C are variables which doesn't stores the value but points to the address where the value is stored.They are used for reference.


Sizeof operator to determine the memory occupied by structure variables and pointers?

C and C++ both include the built-in sizeof() operator to do just that.


Can local variables be passed between subroutines?

If this is a homework assignment, please consider trying it yourself first, otherwise the value of the reinforcement to the lesson offered by the homework will be lost on you. Since C and C++ pass parameters by value, local variables can be passed between functions (subroutines). If you intend to pass them by reference, i.e. by passing their address, you must ensure that they do not go out of scope while the called function runs, otherwise the called function might reference memory that is no longer valid.