CMSC 216: Quiz 3

¡Supera tus tareas y exámenes ahora con Quizwiz!

Draw a memory map for the following program up to the point indicated by the comment /*HERE */. In addition, provide the output generated by the program.

parameters are new boxes

When do you want to use the const modifier?

• Indicates that a variable can't be changed (enforced by compiler) • These constant declarations are helpful when passing arguments int i = 4, j = 5; const int *p = &i; /* pointer to constant int */ int * const q = &j; /* constant pointer to int */ p = &j; *p += 5; q = &i; *q+=23; /*OK*/ /* OK */ /* ERROR */ /* ERROR */

Define a function that receives two integer arrays as parameters and determines whether the arrays have the same elements. For this function: a. The function returns 0 if the arrays are different and 1 otherwise. b. The arrays may have different lengths. c. Feel free to add any parameter you might need.

#include <stdio.h> int have_same_elements(int array1[], int array2[], int size1, int size2); int main(void) { int array1[4] = {2, 3, 8, 11}; int array2[5] = {2, 3, 8, 11, 14}; printf("%d", have_same_elements(array1, array2, 4, 5)); return 0; } int have_same_elements(int array1[], int array2[], int size1, int size2) { if (size1 != size2) { return 0; } else { int count = 0, i; for (i = 0; i < size1; i++) { if (array1[i] == array2[i]) { count++; } } if (count == size1) { return 1; } else { return 0; } } }

Write a code fragment that shows that NULL is considered false in C.

#include <stdio.h> int main(void) { if (NULL == 0) { printf("NULL is considered false in C"); } else { printf("NULL is not considered false in C"); } return 0; }

Define a function that receives an integer array as parameter and shifts all the array elements one position to the left (the first element will become the last one). You may not define/create a new array in the function.

#include <stdio.h> void shift_array(int array1[], int size); int main(void) { int i; int array1[4] = {2, 3, 8, 11}; shift_array(array1, 4); for (i = 0; i < 4; i++) { printf("%d\n", array1[i]); } return 0; } void shift_array(int array1[], int size) { int i, firstelement= array1[0]; for (i = 0; i < size - 1; i++) { array1[i] = array1[i + 1]; } array1[size - 1] = firstelement; }

What are the values associated with the following array? int a[5] = {2, 8, -3};

2, 8, -3

What is the difference between a pointer and a pointer variable?

A pointer is a memory address/reference A pointer variable is a variable whose value is an address

What does the name of an array represent?

A pointer to first element in array

Which of the following pointer variables occupies the largest number of bytes? int *x; float *y; double *m;

All the same (pointers have the same amount of bytes)

When will a segmentation fault occur when we dereference NULL? For example, int *ptr = NULL; printf("%d\n", *ptr);

Anytime you deference it

You need to be familiar with the debugging guide available at: http://www.cs.umd.edu/class/spring2015/cmsc216/content/resources/debugging.html

Incremental Code Development Tools - Make sure you have run splint (e.g., splint my_code.c) and valgrind (e.g., valgrind a.out) on your code. Segmentation Fault? - If your code is generating a segmentation fault, run the debugger (gdb) and use where to identify where the fault is taking place. Simplify Input - Simplify the input that is causing the code to fail. This will help you find the problem and if you need to see us, help us locate the problem with your code. Incremental code development - Write a little bit of code and verify it works before moving forward. How much is a little bit depends on your coding experience. Good variable names and indentation from the beginning - Do not wait until submission time to fix indentation and variable names. Keep backups - You can use the submit server as a backup mechanism. Do not make assumptions - If you are not sure how a language construct works, look up information about it or write a small program that allows you to understand it. Use Tools Often - Check your code with splint, valgrind, and the additional gcc flags mentioned below often. Do not wait until you have a lot of code to use the tools. Do not wait to have a bug in order to use them. 1. Use splint to identify any problems. 2. Make sure your gcc alias has been set correctly. 3. Compile your code with gcc flags -fmudflapth -lmudflap as they can provide additional information. gcc flags -fmudflapth -lmudflap my_code.c 4. Run valgrind on your code (see usage information below). 5. if (segmentation fault) { Use gdb and the where command. } else { if (no results) { a. Program may have an infinite loop (check your loops). b. Program is waiting for input. c. Output statements failed. } else { /* incorrect results or different in the submit server */ a. Use the diff command to identify the differences. b. If using diff does not help, simplify the input as much as possible to see if you can identify the problem. c. Step through the code using the debugger. d. If all the above fails, use printf statements to see values, and the execution path leading to the problem. } } The valgrind utility is mainly use to detect dynamic memory problems, but it can also be used to detect invalid memory accesses. To use the utility: valgrind a.out You get the correct results in grace, but not in the submit server - This may happen because: You have uninitialized variables. Grace seems to set uninitialized variables to 0. Your strings are missing a null character ('\0'). You are not declaring an array that is large enough. You are passing to a function a pointer to a character rather than a pointer to an array of characters.

Could you assign NULL to the name of the array?

No you could not assign NULL to the name of the array int NULL[5] = {1, 1, 2, 3, 5};

How are pointer arguments passed in C? By value?

Pass by value

Why do we need to specify the type of a pointer variable?

Points to the beginning, p only has the value where it starts, does not know how many bytes to grab. Type tells you how many to grab.

How many pointer variables can be associated with a particular variable?

Unlimited

What is a NULL pointer?

• This is a pointer that points to the address 0, where nothing is allowed to be accessed • NULL is considered false • Defined in stddef.h, which is included by many other header files • Analogue to Java's null - What happens when you try to call a method of an object which is null? - Very similar thing happens in C when trying to dereference a NULL pointer; it's usually a segfault • Just like in Java, you have to check pointers to see if they're NULL before dereferencing them:


Conjuntos de estudio relacionados

Chapter 1 - Introduction - Connect Questions

View Set

Biol 252L lab 4 grqWhich bone of the axial skeleton is the only one to form a joint with the upper limb?

View Set

Human Services Final Exam Chapters 9-15

View Set