3

I am new to C, and while reading I came across the concept of pointers. So, after references from multiple books and sites, I still cannot figure out how a pointer is used to save memory.

A pointer points to the address location of another variable, eg: int *ptr =&var;

From the above eg, ptr holds the address of var, which by itself is stored in another memory location. So my question is:

1) why do we use pointers when the pointer value is stored in a separate memory location, thus increasing memory usage?

2) Why not we directly use the variable name(var in this case) instead of using another memory space for pointers

3
  • 1
    Imagine you've got a huge array of structures that consumes 12345 MiB of RAM. Do you want to copy that entire thing, or just have a pointer to it? For any piece of data that is larger than a pointer, a pointer consumes less memory than a copy... Commented Feb 8, 2014 at 4:23
  • But suppose if we want to manipulate the data in that struct, we can directly use the struct name instead of using pointers like struct_array[x].varname=5... Instead of using struct_ptr->varname=5 Commented Feb 8, 2014 at 4:58
  • @Vijayakumar If you want you can declare everything as a global variable, and then you don't need to waste a few bytes by passing a pointer. Only a slight problem with this approach is that it leads to really hard to maintain code because of no possibility for encapsulation. Commented Feb 8, 2014 at 8:57

4 Answers 4

2

Pointers are not exactly used for the concept of memory saving. Even though certain circumstances saves a little memory like passing a parameter (like struct )with its address to function rather than variable itself . size of a struct may be more than 4 bytes where as pointer size is just fiur . it mainly used to ease of manipulation of data and avoid redundant steps. Learn about pass by value and pass reference you will know a bit why pointers are used. Your question why pass to pointer rather variable itself .

When you pass the variable it creates a new variable on stack which is local to that function of we need those changes to reflect back In the calling function then you have to pass the variable back and re assign. But in case of pointers it is using the original variable so there is no need of those extra step of passing variable back and restoring of same .

Sign up to request clarification or add additional context in comments.

Comments

1

I have never used a pointer to save memory and I have never heard of a pointer being used to save memory. As you pointed out a pointer adds to the memory used in addition to the variable that is pointed to.

The main thing that a pointer provides is a kind of handle to a memory location because it contains the address of a variable. Having a pointer to a memory location provides a kind of flexibility because when having to reference the memory area from various parts of a program you can just use the pointer.

Using pointers also allows you to use dynamic memory allocation. You can allocate a memory area for a variable and access the area by using the pointer.

For simple variables and simple programs, pointers are not really that helpful. However for complex programs with large complex variables that have various life times, pointers are very helpful.

Pointers are also useful for applications where direct memory locations/addresses are used for device interactions such as you see with embedded software. You use a pointer to access a shared memory area that is used to communicate with a device.

Pointers in C also allow you to use algorithms and complex data structures such as trees and linked lists more easily. The use of pointers reduces the amount of CPU time spent copying data from one memory location to another because rather than moving data from one place to an other, you instead change the values of pointers.

There are downsides to pointers which is why languages such as Java do not use them. Instead of pointers Java uses memory references or handles to data items. There is more overhead involved however the use of handles rather than pointers means helps the Java virtual machine to identify unused memory areas for garbage collection.

C was originally envisioned as a kind of systems programming language for operating systems and system software where efficiency and little as possible overhead is needed. Pointers is the way that C does some of the memory access that would require the use of assembler language if the pointer did not exist.

Comments

1

In C, what the pointers allow is greater flexibility to manipulate the variable. For example, if you pass an explicit parameter variable into a function as a pointer and change its value, the new variable value is saved outside the function. If it is passed into the function as a variable directly, the new values of the variable are not saved outside the function. There are also a number of byte-level operations on variables that are performed using pointers.

In Java and many other languages, we directly use the variable name and there are no pointers.

Comments

0

Certain entities known to you use pointers, behind. For eg: Arrays in C. Did you know

Arr[1] = 3;

is equivalent to:

*(Arr+1) = 3;

Or consider a String of characters

char *str = "I am a sequence of characters"

It's true that a pointer takes up the size equivalent to the word size as defined by your compiler.

Now think of a situation, pointer as an iterator. So if you would go through an array as:

  • Increase the value of the iterator by 1.
  • Evaluate each element's address
  • Get the value

It would make sense to combine the first two steps as

  • Increment the address pointer
  • Get the value

The space used get's justified here, doesn't it? since the iterator anyway might have used it.

Similarly, pointers can be used to manipulate your programs to get the most out of it.

A compiler is your friend for optimizing stuff, but even a friend could use some help & you can provide that by using pointers. Pointers allow you to tailor your program, in ways the other limited constructs could never allow you to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.