Академический Документы
Профессиональный Документы
Культура Документы
By FCG Team
&X
*p
10 0x22CCE4
0x22CCE4 0x22CCE8
&p
Wipro confidential
Wipro confidential
But, need not be always initialized as an address of a variable. Examples: char *p = Hello; int *ip = malloc(2 *sizeof(int)); *ip = 1; *(ip+1)=2;
p
&p
H e l l o \0
*(p+1)
*(p+5)
*ip
0x22EE00 0x22EE04
0x22CC00 0x22DD00
0x22DD00 0x22DD04
1 2
&ip
*(ip+1)
ip
Wipro confidential 4
Pointers Basics
Important points to remember
Constant Data
Hello string in this example will be in constant data segment Memory pointed by ip will be in heap p and ip will be present in stack if they are local variables. If they are global or static local variables, they will be present in data segment. To differentiate the various segments, each segment is shown in different color.
Stack if p and ip are local variables
*p *(p+1)
H e l l o \0
*(p+5)
0x22DD00
p
&p
0x22DD04
1 2
*ip
*(ip+1)
&ip
0x22EE00 0x22EE04
0x22CC00 0x22DD00
Heap
ip
Wipro confidential 5
Pointers Basics
Wipro confidential
Pointers Basics
Size of a pointer variable Since a pointer stores an address, the size of the pointer
variable is the same as the size required to store an address (typically 4-bytes for all 32-bit architectures) Size of a pointer variable is the same, irrespective of the type it is pointing to.
Wipro confidential
Pointers Frequently done mistakes with pointers Frequent mistakes with pointers
p is not pointing to any legal address. This will cause segmentation fault.
In some architectures, it is illegal to write into const data segment and can cause memory fault. Even if writing to const area is acceptable in some architectures, p is pointing to memory that has only 6 bytes. Adding World to it, would corrupt other data.
Wipro confidential
Wipro confidential
Pointers Basics
Output: 10 4 5
Wipro confidential
10
Output:
50 4 20 4 0x22cca0 0x22cca0 0x22ccaa 0x22cca4 0x22ccab 0x1 0x22cc60 0x22cc60 0x22cc64 0x22cc64 0x403005 0x403005
Wipro confidential
11
Pointers Basics
Double Dimension arrays and double pointers
a[1]: Second dimension size of a is 10 and hence 10 is added.
main() {
Str points to a pointer. &str[1] is str+1 and since size of a pointer is 4 bytes, str + 1 translates to 0x22cca0 + 4
char a[5][10]={"One", "Two", "Three", "Four", "Five" }; char **str = a; Add 1 to a[1] char *b[5]={"One", "Two", "Three", "Four", "Five" }; char **str1 = b; printf("%d %d %d %d\n", sizeof(a), sizeof(str), sizeof(b), sizeof(str1)); printf("%p %p %p %p %p %p\n", a, str, &a[1], &str[1], &a[1][1], &str[1][1]); printf("%p %p %p %p %p %p\n", b, str1, &b[1], &str1[1], &b[1][1], &str1[1][1]); &str[1][1] translates to
&(*(*(str+4) + 1)), i.e &(*(*(0x22cca4) + 1))). This is equal to &(*(0 + 1)) and is equal to 1 and str[1][1] will be a junk value
Output:
50 4 20 4 0x22cca0 0x22cca0 0x22ccaa 0x22cca4 0x22ccab 0x1 0x22cc60 0x22cc60 0x22cc64 0x22cc64 0x403005 0x403005
Wipro confidential
12
Pointer Basics
&str[0][0]
a &str1[0][0] b
0 1 2 3 4 5
0x22cca0 0x22ccaa . . .
6 7 8 9
0x22cc60 0x22cc64 . . .
&str[1][1] translates to &(*(*(str+4byte) + 1)), i.e &(*(*(0x22cca4) + 1))). This is equal to &(*(0 + 1)) and is equal to 1. So str[1][1] will be a junk value
13
Pointer Basics
Junk\0 0 1 2 3 4 5 6 7 8 9
ef be ad de ee ff
c0
00
d e
1f ca
af
de
de
ca
all
cp+1 sp+1 ip+1cp+5 sp+3 ip+2 dp+1 ip + 3 dp+2 ip + 6 sp + 16 cp + 24
int abc=0xdeadbeef; int def=0xc0ffee; char ghi[6]=abcde; int jkl = 0xdeafca1f; short mno = 0xcade; Date pqr = { 1, 1, 2006 }; main() { int *ip = &abc; short *sp = (short *)&abc; char *cp = (char *) &abc; Date *dp = (Date *) &abc; .. } Note: Little endian architecture assumed
sp + 8 cp + 12
When a pointer is incremented by 1, depending upon the type, number of bytes moved will differ. When casting from one type to another Take care of alignment issues (SIGBUS) Keep in mind that number of bytes that will move for every increment/decrement will change.
Wipro confidential 15
Pointers Function pointers Pointers can point to functions as well in addition to variables.
Declaration example: int (*f1)(int,char **) -> Pointer to a function that returns int and takes one integer and one char double pointer as arguments. Assignment example: f1=main;
A pointer variable that points to a function contains the address of the first executable instruction of the function.
Wipro confidential
16
Volatile pointers
Volatile keyword informs compiler that optimizations must be disabled for that variable. The following code has a problem. Compiler will not generate code for disabling interrupt.
#define BASE_ADDRESS 0x80808000 #define INTERRUPT_REGISTER_OFFSET 0x10 CriticalSection() { int *intr_reg = (BASE_ADDRESS + INTERRUPT_REGISTER_OFFSET); *(intr_reg) = 1; /* disables interrupt */ /* critical code, interrupt must be disabled here */ . . /* critical code is executed, interrupt can be enabled here */ *(intr_reg) = 0; /* enables interrupt */ } The problem can be solved simply by defining intr_reg as volatile: volatile int *intr_reg = (BASE_ADDRESS + INTERRUPT_REGISTER_OFFSET);
Wipro confidential
18
Wipro confidential
19
Thank you.
Information contained and transmitted by this presentation is proprietary to Wipro Limited and is intended for use only by the individual or entity to which it is addressed, and contains information that is privileged, confidential or exempt from disclosure under applicable law.
Wipro confidential
20