How are pointer addresses assigned in malloc
C programming: pointer
Up to now, a variable was always addressed directly by name. For example, to add two numbers, the value was assigned to a variable name:
A variable is always addressed internally in the computer via an address (unless the variable is already in a processor register). All memory cells within the main memory are given a unique address. Whenever the processor reads or writes a value from the RAM, it sends this to the main memory via the system bus.
A variable can also be addressed indirectly in C via the address. The (start) address of a variable or more generally - a memory area - is supplied by the & operator (also known as the address operator). You already know this address operator from the statement:
An instruction can be used to find out where this variable was stored:
The value can differ depending on the operating system, platform, and even from call to call. The placeholder stands for the word pointer (English: pointer).
A pointer variable is used to address an object (e.g. a variable) via its address. A pointer variable behaves exactly like a "normal" variable, but its value is interpreted as an address. As a result, each pointer variable also has an address.
The pointer variable a is defined in the following program:
In line 5 the pointer variable is defined and a variable b of type int.
After the definition, the pointer variable has an undefined content. The instruction on line 8 therefore assigns a new value, namely the address of. The variable now points to the variable. The statement prints the value of the variable pointed to by the pointer. Since it was assigned the address of, the number 17 is displayed.
Whether you are accessing the content of the address referenced by the pointer variable or the address referenced by the pointer variable depends on the operator (the content or dereferencing operator): accesses the content of the pointer variable. But if you want to have the address of the pointer variable itself, you have to choose the operator, that is.
A pointer may only refer to a variable that has the same or a compatible data type. A pointer of the type cannot refer to a variable with the type. You will get to know the reason for this in the next chapter. Just so much in advance: The variable type has nothing to do with the width of the address. Depending on the system, this is always the same. With a 16 bit CPU the address is 2 bytes, with a 32 bit CPU 4 bytes and with a 64 bit CPU 8 bytes - regardless of whether the pointer variable was declared as char, int, float or double.
Pointer arithmetic 
It is possible to increase the pointer and thus to address another memory area, e.g. B .:
increases here Not the content, but those address of the pointer. The output also makes it easy to see how big an int is on the system on which the program was compiled. The following is a 32-bit system (difference between the two memory addresses 4 bytes = 32 bits):Memory address 134524936 contains 5 Memory address 134524940 contains 0
Now to increase the value in the memory, not the pointer, will be of no use. This is because the dereferencing operator has lower priority than the post-increment (). To achieve the intended effect, one writes, or else. When in doubt and also to increase readability and maintainability, brackets are a good choice.
Pointer to functions 
Pointers can refer not only to variables, but also to functions, since functions are nothing more than code in memory. A pointer to a function therefore receives the address of the code.
The following expression defines a pointer to a function:
This notation seems a bit unusual at first. On closer inspection, however, there is only one difference between a normal function definition and the pointer notation: The pointer appears instead of the name of the function. The variable type is the return type and the parameter passed to the function. The bracket around the pointer must not be removed because the bracket operator has a higher priority than the dereferencing operator.
As with a pointer variable, a pointer to a function can only hold one address. So we still have to assign an address to the pointer:
This notation is the same because the address of the function is in the function name. For the sake of readability, you should generally not do without the address operator.
We can now call the function using the pointer as usual:
Here is a complete sample program:
void pointer 
The pointer is compatible with every other data pointer (attention, different than in C ++), i.e. in C programs no Cast in the target pointer type is required, in C ++ it is mandatory. One speaks here of an untyped or generic pointer. This goes so far that you can convert a void pointer to any other pointer and back without the pointer's representation losing any properties. Such a pointer is used, for example, with the library function malloc. This function is used to provide a certain amount of memory; the start address of the allocated area is returned. Then the programmer can write and read data of any type there. Therefore pointer typing is irrelevant. The prototype of malloc is the following:
The return type is necessary here because it is not known which pointer type (etc.) should be returned.
The only difference to a typed ("normal") pointer is that the pointer arithmetic is difficult to master because the compiler does not know the memory consumption per variable (we will come to this in the next chapter) and in this case you has to take care of the fact that the pointer comes to rest at the correct address. For example with the help of the operator.
Call by Value 
A function is used to perform a specific task. Variables can be passed to it for this purpose or it can return a value. The compiler does not pass this variable directly to the function, but makes a copy of it. This way of passing variables is called Call by Value designated.
Since only a copy is made, the transferred values are only valid within the function itself. As soon as the function is exited, all these values are lost. The following example illustrates this:
After compilation, the program produces the following output on the screen:10 15 10
This is because the function only receives a copy of the variable. The function then adds the number 5 to this copy. After exiting the function, the content of the variable is lost. The last statement in therefore outputs the number 10 again.
One solution has already been addressed in the functions chapter: The return via the instruction. However, this has the disadvantage that only one value can be returned at a time.
A good example of this is the function. Its purpose is to swap two variables. The function should look something like this:
The function is basically correct, but cannot return the result to the main function, since it only works with copies of the variables and.
The problem can be solved by not passing the variable directly, but - as you probably guessed it - a pointer to the variable is passed to the function. The correct program then looks like this:
In this case the result is correct:Variable x: 2, variable y: 5, variable x: 5, variable y: 2
The program is now correct because the function does not work with the copies of the variables x and y, but with the originals. In many books, such a call is also called Call By Reference designated. However, this designation is not without its problems. In fact, there is also one here Call by Value before, but not the value of the variable but its address is transferred. C ++ and some other languages also support a real one Call By Reference, C however not.
The question you may be asking yourself is what use is made of pointers. It seems that apart from calling a function with Call by Reference, we have so far got along quite well without pointers. Other programming languages even seem to be able to do without pointers entirely. But this is a fallacy: often pointers are only well hidden so that it is not immediately apparent that they are being used. For example, in the case of strings, the computer works internally with pointers, as we shall see later. It is also not possible to copy, search or change data fields without a pointer. With type-safe programming languages, there are usually no pointers that can be used at will.
There are areas of application that cannot do without pointers at all: An example of this are data structures such as linked lists, which we will get to know briefly later. In the case of linked lists, the data is stored in a so-called node. These nodes are connected to each other with pointers. This has the advantage that the number of nodes and thus the number of elements to be saved can grow dynamically. If a new element is to be added to the list, a new node is simply created and connected to the rest of the linked list with a pointer. It would be possible to implement a pointerless variant for linked lists, but this would lose a lot of flexibility. Even with many other data structures and algorithms, you cannot do without pointers. Some algorithms can also be implemented more efficiently using pointers, so that their execution time is faster than implementing the same algorithm without a pointer.
Errors can occur with pointers, for example:
In this program, a pointer points to a variable that is out of range. The value to which the pointer points is then not defined. For example, if the program was compiled with gcc without optimization, the output looks like this:pointa points to 0x7fffdfe01d44 with the value 0 pointa points to 0x7fffdfe01d48 with the value 1 pointa points to 0x7fffdfe01d48 with the value 2 pointa points to 0x7fffdfe01d48 with the value 2 pointa points to 0x7fffdfe01d48 with the value 2
The address is different when the program is executed again.
If the program has been compiled for debugging optimization (-Og parameter), the value is 1 instead of 2.
If the program was compiled with a different optimization, the value (apart from the start value) is also different when the program is executed again, for example:
- Why is Umbridge hated more than Voldemort
- Margaret Thatcher was a cruel person
- What can Muslims learn from Hindus
- What do you say to yourself every day
- How many zeros are in a million
- Why do I always have energy
- What do you think of refugees
- PC gaming is a niche
- What is the relationship between grams liter
- Where can I get free blog templates
- Which country has the most blacks?
- Who is your current favorite film director?
- Is the Burmese language similar to Vietnamese
- Why are Sedona rocks red
- What is the deadliest disease of all time
- What makes a question silly
- Who was Sirius Black's love interest
- Is it bad to seriously hate reality?
- What do you think of money
- What is a fast food restaurant
- Donald Trump loves animals
- Why can't my husband cum inside me
- How often do the Chinese eat noodles?
- Why doesn't Quora like Polling Questions
- What is worse anorexia or obesity
- Do you think GOT has been overrated
- How many SEO clients do you have
- Smoking 4 cigarettes a day badly
- What are the boolean values in Java
- How do I sell self-published books
- How do people make money from webinars
- What does Batman use to kill Superman
- What is Public Sector Bank in Hindi
- Why is extreme pleasure actually intense pain?