Pointer Camp Learnings
Pointer are best taught in a Q and A style.
27 flashcards · Programming Languages · by kgashok
What is the likely reason for this to happen?
The pointer has been declared but not initialized. char *abby; *abby = 'P'; // causes the warning
How do you avoid this warning?
Allocate dynamic memory and initialize it to the pointer before it is used. char *abby; abby = malloc(20); *abby = 'P';
The printf statement will generate what output? A. returning "PNG" B. returning "PNG" and 17 more characters C. Unpredictable D. None of the above
C. Unpredictable abby is pointing to some location in abbreviation whose content is not known.
The printf statement will generate what output? A. "PNG" B. "PNG" and 17 more characters C. Unpredictable D. None of the above
C. Unpredictable abbreviation contains a poorly formed NTBS string without a '\0' at the end.
The printf statement will generate what output? A. "PNG" B. "PNG" and 17 more characters C. Unpredictable D. None of the above
A. "PNG" The malloc'ed area has a properly formed NTBS string which contains PNG.
A pointer variable huge_memory points to 100,000 bytes of contiguous memory. Write a program to fill each byte of the memory pointed to by huge_memory with the letter 'a'. Write a code snippet to achieve the above without using pointer code whatsoever.
huge_memory[0] = 'a'; huge_memory[1] = 'a'; huge_memory[2] = 'a'; ... huge_memory[99999] = 'a'; Rather cumbersome, but then this demonstrates why we need pointers.
is a valid C program written to create a memory size of 100,000 bytes and fill it up with the number 1. It compiles successfully. Explain Line 3.
A chunk of memory of 100,000 bytes is dynamically allocated at runtime by using malloc function. And the memory location address is being assigned to the integer pointer variable a.
is a valid C program. It compiles successfully. Explain Line 5 and Line 6.
Line 5 The memory location pointed to by pointer variable a is dereferenced and updated with the integer value of 1. Line 6 The pointer arithmetic happens and the address stored by a is incremented.
The program will executed, will run successfully to completion. True or False?
False. The program will crash and cause segmentation fault due to memory violation.
causes a segmentation fault problem. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system). How will you fix it?
Change Line 4 to With every increment of the integer pointer, it moves by 2 or 4 memory locations and the code now is prevented by writing to illegal memory locations.
A FlexeLint static code analyzer reports two problems, one of which is Pointer a potentially could be NUL value and it is on the LHS, therefore cause errors in some situation. How will you fix this?
The function malloc is not guaranteed to return a valid memory location all the time. So, the pointer has to be checked for NULL value before using it.
The second problem reported by the static code analyzer is for Line 7 (the incrementing of the pointer) as follows: How will you fix this?
The pointer variable holding the allocated memory should not be incremented. Otherwise, the memory allocated cannot be freed eventually.
This is almost perfect. Is something is missing still? What is it?
In dynamic memory management, it is always good practice to free the memory that has been malloc'ed.
The dark green lines are examples of how pointers are initialized by other pointers
CLUES: how to provide initial values? when you use the * what are you doing? Static vs ______?
The dark green lines are examples of how pointers are initialized by other pointers de-referenced to store data in memory locations
CLUES: how to provide initial values? when you use the * what are you doing? Static vs ______?
The dark green lines are examples of how pointers are initialized by other pointers de-referenced to store data in memory locations The light green lines are use-cases for pointers and how they are required for dynamic memory allocation
CLUES: how to provide initial values? when you use the * what are you doing? Static vs ______?
The magenta high-lighting is an example of pointer arithmetic
CLUES: how to provide initial values? when you use the * what are you doing? Static vs ______?
void* malloc( size_t size ); malloc function allocates sizebytes of uninitialized storage. On success, returns the pointer to the beginning of newly allocated memory.
CLUE: which parameter specifies that? CLUE: What is returned by the malloc function?
How is dynamic memory management done in C.
The two main functions uses in dynamic memory allocation malloc function allocates sizebytes of uninitialized storage. On success, returns the pointer to the beginning of newly allocated memory. free deallocates space previously allocated by malloc(). If ptr is a null pointer, the function does nothing.
A pointer variable huge_memory points to 100,000 bytes of contiguous memory. Write a program to fill each byte of the memory starting from the location of the pointer with the letter 'a'. Write a code snippet to achieve the above.
char *p = huge_memory; if (p) for (int i = 0; i < 100000; i++) *p = 'a', p++;
What are pointer variables? And how are they useful?
they are a special type of variables that hold valid pointer values (aka memory address locations). They are powerful and cause damage if not used properly they can be used to examine and/or modify values stored in memory locations pointed to by them they are used for dynamic memory allocation they are used as arguments and parameters in function definitions (pass by reference) they are used for writing indirection code
int a = 2; // normal integer variable int *c; // c is declared as a integer pointer int **d; // d is declared as double pointer to int c = &a; d = *c; **d *= 2; printf("%d ", a); The above code output will be will not compile will compile but no output 2 4
The code will not compile. Change from d = *c; to the following: d = &c;
int a = 2; // normal integer variable int *c; // c is declared as a integer pointer int **d; // d is declared as double pointer to int c = &a; d = &c; **d *= 2; printf("%d ", a); The above code output will be will not compile will compile but no output 2 4
d ----> c ----> a (2) **d = 2 **d * 2 = 4 Answer is 4.
Assume hp is a character pointer, and it is initialized with a valid pointer value. What happens when you execute the expression hp++? The pointer now points to the next contiguous memory location. True or False? The contents of the memory location pointed to by hp is not changed. True or False?
True True
Assume int_pointer is an integer pointer and it is initialized with the address of an integer variable var_a. int *int_pointer = var_a; What happens when you execute the expression? *int_pointer && int_pointer++ The expression will cause a segmentation fault The int_pointer increments by 1 if var_a is not zero The int_pointer increment by 4 if var_a is not zero None of the above Which statement(s) are true?
Only Statement 3 is True. *int_pointer -> if it evaluates to any value other than zero, it is true and then it will increment int_pointer
int a = 10; int *ap = &a; int **pap = ≈ To update the variable a to contain the value 1000, which of the following statements will work? a = 1000; *ap = 1000; **ap = 1000; All of the above
Answer: 4
is the solution for sorting strings provided on the command line. But it does not work for the input of "aza aca aba". What changes will you make?
The code that was present only compares the first character of every string and therefore gives an erroneous output strcmp helps compare the entire string.