*********************************************************************** **************** MORE ABOUT POINTERS




Is the NULL pointer same as an uninitialiseed pointer? <Yes / No> In which header file is the NULL macro defined. as '0L'


3] Why is it that for large memeory models NULL has been difined and for small memory models as just 0? 4] What is a NULL pointer?

5] What's the difference between a null pointer, a NULL macre, the ASCII NUL character and 'null string'? 6] What would be the output of the following program? #include "stdio.h" main() { int a,b = 5; a = b + NULL; printf("%d",a); } 7] 8] Is the programming style adopted in 8.6 good? What would be the output of the following program? #include "stdio.h" main() { printf("%d %d",sizeof(NULL),sizeof("")); } 9] How many bytes are occupied by near, far and huge pointers ? causes t

10] What does the error "Null Pointer Assignment" mean and what his error ? 11] 12] How do we debug a Null Pointer Assignment error ? Can anything else generate a Null Pointer Assignment error ?

and rry[][] same ? <Yes / No> 14] Can two different near pointers contain two different address r to the same loction in memory? <Yes / No> 15] Can two different far pointers contain two different address r to the same location in memeory? <Yes / No> 16] Can two different huge pointers contain two different addresses but refer to the same location in memory? 17] Would the following program give any warning on compilation ? #include"stdio. p2 = &i. void *p2. } char che but refe but refe program 20] How would you eliminate the warning generated on compiling the followin g program? main() { . p1 = p2.13] Are the three declarations char **apple. p2 = &i.i = 25. *scr = 'A'. scr = 0xB8000000. char *orange[].h" main() { int *p1.h" main() { int *p1. p1 = &i.i = 25. void *p2. p1 = &i. } 19] ? What warning would be generated on compiling the following main() { char far *scr. p2 = p1. } 18] Would the following program give any warning on compilation ? #include"stdio.

char far *b = 0x00100020. } . if(b == c) printf("\n Hello Hi"). if(a == b) printf("\n Hello"). if(a == b) printf("\n Hello"). } 26] main() { char huge *a = 0x00000120. if(a > b && a > c && b > c) printf("\n Bye"). scr = 0xB8000000. if(a == c) printf("Hi"). } 21] How would you obtain a 'far' address from the segment and ddresses of a memory location ? offset a 22] How would you obtain segment and offset addresses form a 'far' address of a memory location ? 23] In a large data model(compact.medium) all pointers are 16 bits lon g. wh ereas in a small data model(tiny. whereas the offset part is s tored in the 16-bit 'near' pointer. *scr = 'A'. large. <True / False> 25] What would be the output of the following program ? main() { char far *a = 0x00000120. < True / False> 24] A 'near' pointer uses the contents of CS register (if the pointer is pointing to data) for the segement part. char huge *b = 0x00100020. if(a > b && a > c && b > c) printf("\n Bye").char far *scr. char far *c = 0x00120000. if(a == c) printf("Hi").small. if(b == c) printf("\n Hello Hi"). char huge *c = 0x00120000. huge) all are 32 bits long.

*********************************************************************** ************************** ANSWERS ******************************* *********************************************************************** 1] 2] No In files "stdio. 5] A null pointer is a pointer which doesn't point anywhere. .h" whereas 3] Because in small memeory models the pointer is two bytes long in large memory models it is 4 bytes long. 6] 5 7] No. It has a value 0 associated with it. which ensures that the NULL will not work in non-pointer contexts. The null string is just another name for an empty string " ". 8] 2 1 pointer 9] A 'near' pointer is 2 bytes long whereas a far and a 'huge' are 4 bytes long. Only in context of pointers should NULL and 0 be consider ed equivalent. Even t hough this may work it is a bad style of programming. Usually. 4] For each pointer type (like say a char pointer) C defines a special pointer balue which is guarenteed not to point to any object or function of th at type.h" and "stddef. The ASCII NUL character has all its bits as 0 but doesn't have any rela tionaship with the null pointer. A NULL macro is used to represent the null pointer in source code. ANSI C permits deinition of the NULL macro as (( void *)0). NULL should not be used when other kind of 0 is required. the null pointer constant used for representing a null po inter is the integer 0.

no warnin g messages will b e generated. the four zeros and the copy right banner are checked.10. but may be a wild pointer that references these key areas in the data segm ent. com pile the following program in the small memory model and execute it: #include "dos. a null pointer points to DS: 0000. followed by the Borland copyright notice " Borland C++ . both the strcpy() and pr intf() calls using the ptr bariable will generate warnings. printf("&ptr = %Fp \n".h" #include "stdio. the copyright banner will vary depennding on your version of the Borland C/C++ compiler. and what they should display in the watch window. Note that the pointer may not truly be nul l. This error occurs in programs which attempt to chang e the bottom of tahe data segment.42MS Borland C++ . } One of the best debugging techniques for catching Null pointer assignment errors is to turn on all warinin compiler messages. Therefore. Thus assigning a value to th e memory referenced by this pointer will overwrite the first zero byte in the data segment. a null pointer will reference 0000:0000.".banner). However."the world cup saga"). For example.h" main() { char *ptr. In the small and medium memory models. or the base of system memory. If either has beenm modified. 11] In the Integrated Development Environment set two watches on the key memory locations mentioned in 8." (char *)0 Of course. you have j ust executed a statement that used a pointer that has not been properly init ialized. If the above p rogram is compiled with warnings turned off. YOu should be particularly suspicious of any warnings that a variable might be used before b eing initialzed. or of a suspicious pointer assignment. printf("banner:%s\n". banner = ( char *) MK_FP(_DS. Note that a Null Pointer Assignment error is not generate d in all midels. At program termination.(void far*) &ptr[0]). far pointers are used for data. large and huge memory models.h" #include "string. At the point where one of them changes. however. Step through your program using F8 or F7 and monitor these va lues in the watch window. Borland places four zero byt es at the bottom of the data segment.Copyright 1991 Borland Intl. These watches. are: *(char *)4. The most common cause of this error is probably declarin g a pointer and then using it before allocating memory for it. Modifying the base of system memory usually causes a system crah. Athough it would be possible that . then the Null Po inter Assignment error is generated. In Borland C and C++ compilers. if all warnings are turned on. printf("banner:%s \n".10] The Null Poiner Assignment error is generated only in small and medium memory models.Copyright 1991 Borland Intl. and using it will ot cause a corruption of the k ey balues at the base of the data segement. In the compact.4).banner). strcpy(ptr.*banner.

Suspicious pointer conversion in function main. p = MK_FP(seg. Non-portable pointer assignment in function main. No. DS = CS = SS. 13] 14] 15] 16] 17] 18] 19] 20] 21] No. place a wat ch on that pointer. # include "dos.*off.h" main() { char *seg = (char *) 0xb000. seg = (char*) FP_SEG(scr). 12} Yes. char *off = (char *) 0x8000. No. } 22] .h" main() { char far *scr = (char *) 0xb8000000. Yes. using a wild pointer that happens to regerence the base area of the data segment may cause the same error since this would chang e the zeros or the copyright banner. Therefore. Use the typecast scr = (char far *) 0xb8000000.off). char far *p. In the tiny memory model. any memory corruption could result in this err or being generated. Yes. No. off = (char *) FP_OFF(scr). Step through your program again and watch for its value (a ddress) to change. Since data corruption or stack corr uption could cause an otherwise-valid pointer to be corrupted and point to the base of the data segment. } # include "dos. char *seg. using a null pointyer will overwrite the beginning of the code segment.a wild pointer would ovrewrote the key walues. If the pointer used in the program statement which co rrupts the key values appears to have been properly initialized. it would not indicate a null pointer.

b and c are such that the last condition is satis fied. because while comparing using > (and >=. And the offset values of a.23] 24] 25] True. huge pointer are 'normalized' to avoid th e strange behaviour as in the 25th above. True. < . Bye Here a. Huge pointers are always kept normalized. . The last if however gets satisfied. 26] Hello Hi Hello Hi Unlike far pointers. this means that the offset will only hav e a value for 0 to F.b and c reger to same location in memory still the firs t three ifs fail because while comparing the are pointers using ==( and ! =) the full 32-bit value is used and since the 32-bit balues are different th e ifs fail. As a result for any given memory address there is only one possible huge address segment: offset pair for it. <= ) only the offset value is used for comparsion. Since a segment can start every 16 bytes. A normalized pointer is a 32-bit poin ter which has as much of its value in the segment address as possible.

Sign up to vote on this title
UsefulNot useful