Basic Knowledge
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
Problem 1. Basic knowledge
- Read the following code. Which of the following statements is/are true?
#ifndef NULL
#ifdef __cplusplus
#ifndef _WIN64
#define NULL 0
#else
#define NULL 0LL
#endif /* W64 */
#else
#define NULL ((void *)0)
#endif
#endif
{{ multiselect(1) }}
- The value of
NULLis undetermined until running time. - If we defined
NULLsomewhere else, this code would report a Compile Error. NULLis a pointer.NULLwill be defined multiple times in this code.- In
C++,NULLis0, which is not a pointer.
- Read the following header file. Suppose this file's name is
cs100_hw3.hunder current work directory.
#ifndef CS100_HW3_H
#define CS100_HW3_H
#include <stdio.h>
...
#endif //CS100_HW3_H
{{ multiselect(2) }}
- If we include
stdio.htwice, we would have Compile Error. - To include this header, write
#include <cs100_hw3.h>in our main C file. - Before we include
cs100_hw3.h, we could defineCS100_HW3_Hin our main C file (with no affects to including header file). - After we include
cs100_hw3.h, we have definedCS100_HW3_Hin our main C file.
-
Read the following code. Fill the blank with output corresponding to
printfs, omitting the newline\ncharacter.#include <stdio.h> int main(void) { char str[] = "Hello_CS100"; printf("%s\n", str); // (1) printf("%s\n", str + 7); // (2) str[5] = 0; printf("%s\n", str); // (3) const char *fstr = "%s/\\\n"; printf(fstr, str+1); // (4) }
| Code | (1) | (2) | (3) | (4) |
|---|---|---|---|---|
| Output | {{ input(301) }} | {{ input(302) }} | {{ input(303) }} | {{ input(304) }} |
- Read the following code. Assume memory address is 64-bit. Which of the following statements is/are true?
#include <stdlib.h>
char *str = "Hello_World";
char arr[] = "abcdef";
int main(void) {
int *ptr = malloc(10*sizeof(int));
static int b[] = {1, 2, 3};
free(ptr);
}
{{ multiselect(4) }}
- The value of
sizeof(str)is11. - The value of
sizeof(arr)is6. - Before free, the memory
ptrpoints to is in heap memory. &ptris a memory address located in stack memory.- The element of static array
bis located in stack memory. - The type of
"abcdef"ischar [7]. - We can use code such as
str[2]='b';to change the content ofstr.
- Read the following code. Which of the following statements is/are true?
int main(void) {
int x = 2, y = 2;
const int N = 100;
const int *p = &x;
int* const q = &y;
}
{{ multiselect(5) }}
- If we write
*p = 10, this would lead to Compile-Error. - If we write
p = &y, this would lead to Compile-Error. - If we write
*q = 10, this would lead to Compile-Error. - If we write
q = &x, this would lead to Compile-Error. const int *p = &x;is equivalent toint const *p = &x;.int *ptr = &N;would generate warning. Furthermore,*ptr = 100would lead to undefined behavior.
- Which of the following statements is/are true?
{{ multiselect(6) }}
malloc()would return an address where value initialized to 0.- If we want to free half memory of
int *ptr = malloc(n * sizeof(int));, we could writefree(ptr + n/2); - If we forget to free memory of malloc, this memory would be freed by C Memory Manager after pointer lifetime ends.
- The freed memory may be allocated by next
malloc()call.
- Read the following code. Which of the following statements is/are true?
struct Account{
char username[10];
int ID;
};
{{ multiselect(7) }}
sizeof(struct Account)is equal to10*sizeof(char) + sizeof(int).- We could use
struct Account A = {.username = "Alice", .ID = 0xbadbeef};to initialize a struct variable. - If
AandBare bothstruct Account, we could writeb=a;to copya's value tob. C's struct is equivalent toC++'s struct.
- Which of the following statements is/are true?
{{ multiselect(8) }}
#define TABLE(a, b, n) a*n + b, the value ofTABLE(1, 2, 3)is5.#define TABLE(a, b, n) a*n + b, the value ofTABLE(1, 2, 3) * 3is9.#define TABLE(a, b, n) (a*n + b), the value ofTABLE(1 + 1, 2, 3)is8.#define TABLE(a, b, n) ((a)*(n) + (b), the value ofTABLE(1 + 1, 2, 3)is8.
- Suppose we have allocated some memory as follows.
int *mem = malloc(sizeof(int) * 10);
Which of the following code snippets correctly deallocate(s) the memory?
{{ multiselect(9) }}
-
for (int i = 0; i < 10; ++i) free(mem[i]); -
for (int i = 0; i < 10; ++i) free(mem + i); -
free(mem); -
free(mem + 5); free(mem); -
free(mem, mem + 10); -
free(mem, mem + 10 * sizeof(int));
- The standard library function
mallocis declared as follows.
void *malloc(size_t size);
Which of the following statements is/are true?
{{ multiselect(10) }}
- The asterisk (
*) here is the dereference operator. - The return type of
mallocisvoid *. - The return type of
mallocis determined at runtime. For example, the return type ofmalloc(sizeof(int) * n)isint *. - If
size > 0, it will try to allocate at leastsizebytes of memory. - If
sizeis too large,mallocwill fail and the program will crash.