C Programming

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is $^ and $@ in a make file and what are they called?

$^ is a replacement for all of the prerequisites $@ is a replacement for the target name They are called automatic variables e.g. obj=a.o b.o c.o d.o multi: $(obj) multi.c cc multi.c $(obj) -o multi This could be rewritten as: multi: $(obj) multi.c cc $^ -o $@

How do you find the type of terminal you are using?

$echo TERM or tput longname e.g. xterm-256color

How do you find details about a process and specifically its address space?

/proc/procid/ for all details and examine "maps" for the addresses.

What would happen if we initialized a static variable?

N.B. the BSS shrank and data grew by 4 bytes.

How do you find the process ID of a process?

Run ps aux and grep it

What is the offset in maps?

The offset field is the offset into the file/whatever.

Where in the address space do the pointer and the allocated memory get stored? int *a = malloc(3*sizeof(int));

a at the top of the stack and memory on the heap.

If we had this and then output a[0], a[1] and a[2], which part of the address space would be output?

a[0], a[1] and a[2] when give is the contents of addresses 0, 4 and 8 respectively (all 4 bytes in each).

What would the full file for the implicit rule be: hello:

default: hello hello: hello.c cc hello.c -o hello

How do you add file indicators and what does each mean?

ls -F appends symbols to filenames. These symbols show useful information about files. @ means symbolic link (or that the file has extended attributes). * means executable. = means socket. | means named pipe. > means door. / means directory.

How do you dereference a ls

ls -L when showing file information for a symbolic link, show information for the file the link references rather than for the link itself

How do you ls the contents of directories?

ls -R for recursive

How do you find a files inode?

ls -i

What happens when we print out these: long *a = malloc(3*sizeof(long)); a[0] = 33; a[1] = 66; a[2] = 99; printf("&a[0] = %lu\n", a); printf("&a[1] = %lu\n", a+1); printf("&a[2] = %lu\n", a+2);

&a[0]: 0x55cbe9988260 &a[1]: 0x55cbe9988268 &a[2]: 0x55cbe9988270

What would the following output? int *a = malloc(3*sizeof(int)); a[0] = 13; a[1] = 14; printf("*a = %d\n", *a); printf("a[0] = %d\n", a[0]); printf("*(a+1) = %d\n", *(a+1)); a = a + 1; printf("*a = %d\n", *a);

*a = 13 a[0] = 13 *(a+1) = 14 *a = 14

What is the text segment of a process?

1. Text Segment: A text segment , also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions. As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.

In the following function where are a and ap stored in the address space? int main() { int a = 0; int *ap = &a; }

Because they are part of the main() frame, a would be stored at the bottom of the stack and sp would be stored in the next location. The 4 bytes at location 44 would contain 0 and the ones at 40 would contain 44 (the address of a).

What is the heap?

Heap is the segment where dynamic memory allocation usually takes place. The heap area begins at the end of the BSS segment and grows to larger addresses from there.The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size (note that the use of brk/sbrk and a single "heap area" is not required to fulfill the contract of malloc/realloc/free; they may also be implemented using mmap to reserve potentially non-contiguous regions of virtual memory into the process' virtual address space). The Heap area is shared by all shared libraries and dynamically loaded modules in a process.

If we do the following: int **a = malloc(3 * sizeof(int*)); How is address space set out?

Here, the malloc adds three integer pointers to the heap, so each of these spaces (size of address each on your machine) will contain an address to an integer. The [int *](*a) will contain an integer pointer (a*) to an integer pointer [int*] which is the 1st pointer stored on the heap.

What does the following mean? TARGET: PRE-REQUISITE RULE

Here, we are building the target. It will be built of it doesn't exist or if any of the prerequisites have changed. In order to build the target, we follow the rules.

How would you include a .o dependency and how can it include implicit rules?

If hello also depended on an object file that contains lots of functions it needs, we would need to add that as a target to be built: default: hello hello: hello.c func.o cc hello.c func.o -o hello func.o: func.c cc -c func.c Here, -c will create an object file. This is another implicit rule. If there is a target will a .o name and there is a .c file with the same name, then make will build the .o file using the -c option

How can this address space layout be used with 2D arrays? int **a = malloc(3 * sizeof(int*)); what happens when we do the following: for (int i=0; i<3; i++) { a[i] = malloc(3 * sizeof(int)); }

If we have a[0][0] This will jump to the first pointer for the first [0], which points to the 1st int* on the heap. This then points to the 1st 3 ints malloc in the for loop. When we get there, we look at the second [0] which will point to the first int in this group of 3.

What does tee do?

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. e.g. % ls -la | tee file-directory.txt

After int **a = malloc(3 * sizeof(int*)); what happens when we do the following: for (int i=0; i<3; i++) { a[i] = malloc(3 * sizeof(int)); }

It will add three set of 3 integers to the heap and then it will add pointers to each of these areas to each of the elements of the original array.

What is the data segment also called and what is it used for?

It's also called Initialized Data Segment Initialized data segment, usually called simply the Data Segment. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. Note that, data segment is not read-only, since the values of the variables can be altered at run time. This segment can be further classified into initialized read-only area and initialized read-write area. For instance the global string defined by char s[] = "hello world" in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global C statement like const char* string = "hello world" makes the string literal "hello world" to be stored in initialized read-only area and the character pointer variable string in initialized read-write area. Ex: static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment.

How does make know when to remake a prerequisite?

N.B. If there are no pre-requisites, the target will just be built if it doesn't exist. If there are prerequisites, make will store the dates of the files and if those dates change, then the pre-requisite will be rebuilt.

What would happen to the original pointer (outside the func).in the following function: void f(int *p){ p = realloc(....); }

Nothing would happen to the original. It would point to where is pointed before. This is because *p is a local variable so only p would get changed (not the contents of p). You could use this to permanently change what p (and the original) point to, but not the original pointer value itself. If you wanted to do that you would need to change it to the following: void f(int **p){ *p = realloc(....); } You would then pass in &original (i.e. the address of the original).

What is in the high memory of a process?

The command line arguments and environment variables

What is the inode pointer structure?

The inode pointer structure is the data structure universally used in the inode to list the blocks (or data blocks) associated with the file. Most modern file systems have 15. From wikipedia, for the case where there are 12 pointers in the data structure, the pointers are: Twelve points that directly point to blocks containing the data for the file. These are called direct pointers. One single indirect pointer. This pointer points to a block of pointers that point to blocks containing the data for the file. One doubly indirect pointer. This pointer points to a block of pointers that point to other blocks of pointers that point to blocks containing the data for the file. One triply indirect pointer. This pointer points to a block of pointers that point other blocks of pointers that point to other blocks of pointers that point to blocks containing the data for the file.

What is a c object file?

The object files are used as a linker for creating executable files which are mandatory for running C programs. ... An Object file (.o extension) is the compiled file itself. It is created when a C program (.c file) is compiled using a compiler. An executable file (.exe) is formed by linking the Object files.

In maps, what is the pathname?

The pathname field will usually be the file that is backing the mapping. For ELF files, you can easily coordinate with the offset field by looking at the Offset field in the ELF program headers (readelf -l). Otherwise it can be a pseudo path.

What 9 things does POSIX store about your file in its inode? S DUG MAT LP

The size of the file in bytes Device ID User ID of the file Group ID of the file The file mode that determines the file type and how the owner, group, and others (world) can access the file Additional system and user flags to further protect the file (note: this can be used limit the files use and modification) Timestamps telling when the inode itself was last change (ctime, changing time), the file content was last modified (mtime or modification time), and when the file was last accessed (atime or access time) A link counter that lists how many hard links point to the inode Pointers to the disk blocks that store the file's contents (more on that later)

What is the stack?

The stack area traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory was exhausted. (With modern large address spaces and virtual memory techniques they may be placed almost anywhere, but they still typically grow opposite directions.) The stack area contains the program stack, a LIFO structure, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction. A "stack pointer" register tracks the top of the stack; it is adjusted each time a value is "pushed" onto the stack. The set of values pushed for one function call is termed a "stack frame"; A stack frame consists at minimum of a return address. Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller's environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn't interfere with the variables from another instance of the function.

What are the other names for BSS and what is it used for?

Uninitialized data segment, often called the "bss" segment, named after an ancient assembler operator that stood for "block started by symbol." Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code. For instance a variable declared static int i; would be contained in the BSS segment. For instance a global variable declared int j; would be contained in the BSS segment.

int main() { int a = 0; int *ap = &a; int **app = &ap; } In this situation, how do we find the contents of app?

We dereference app. Dereferencing also uses the * character. To dereference we would use: int *appd = *app; What is happening here? First we take the value of the variable. app is a pointer, which means its value if a memory address. This value is 40. We then go to this memory address (the memory address in the pointer). To find what this is we need to look at the variables type. It is a pointer to a pointer, so the value app points to is another pointer. i.e. the bit is brackets here: [int *]* app = &ap; This is the value of ap, which is another memory address, which points to the value of a. If that is dererenced again: *appdd = *appd; So this a whatever appd is pointing at which is a [int] *appd (i.e. that which is left of the deferenced variable. i.e. an int. That is the actual value of a.

How could we include a function in our main file and then build it with a make file?

func.c #include "func.h" void hello_func() { printf("Hello!!!"); } func.h #include <stdio.h> void hello_func(); We can now add the .h and therefore the hello_func() to our main hello.c file: #include <stdio.h> #include "func.h" int main() { printf("Test program\n"); hello_func(); return 0; } We then need to create a make file. There are a couple of ways we could do this: hello.c hello: hello.c func.o Now, because .c and .o have implicit rules, this should be enough to build the programs.

What is an implicit target?

make doesn't need to be told all of this if the name of the target is the same as a .c file (like hello and hello.c). This is called an implicit target. The file will be compiled with cc without needing to be told.

What is the perms in maps and what are the 5 different values it can contain?

maps is 'A file containing the currently mapped memory regions and their access permissions.' It contains the following flags: r = read w = write x = execute s = shared p = private (copy on write)

What do you get for p in the following function: void f(int *p){ }

p would simply contain the address.

How does the following work: default: hello hello: hello.c cc hello.c -o hello

Here it needs hello to create default. To create hello it uses hello.c. Here cc is almost always aliased to the defaults compiler on your system (gcc or clang). This is saying compile hello.c and create the output file hello.

What does the size command do?

It outputs the sizes of the text, data and bss segments as well as the total size in dec and hex.

If we had this: long *a = malloc(3*sizeof(int)); on a 32 bit machine, which part of the address space would a[1] (probably) access?

Probably byte 8

What would happen if the global was initialized?

Same thing...

What is the mmu and what does it do?

The MMU is the Memory Managament Unit and it translates addresses between virtual and physical memory.

If we had the following: int *a = malloc(3*sizeof(int)); a[0] = 12; What would the following output? printf("*a = %d\n", *a); printf("a[0] = %d\n", a[0]);

They would both output 12.

What are map pathnames pseudo path?

[stack] - The initial process's (also known as the main thread's) stack. [stack:<tid>] (since Linux 3.4) - A thread's stack (where the <tid> is a thread ID). It corresponds to the /proc/[pid]/task/[tid]/ path. [vdso] The virtual dynamically linked shared object. [heap] The process's heap.

What is address or pointer arithmetic? How would it work on: int *a = malloc(3*sizeof(int));

a would initially point to the fo

How can we redirect an error to nothing e.g. cat sdfsdf?

cat slfds 2> /dev/null

What would the explicit of the following be: hello: hello.c func.o

hello: hello.c func.o gcc hello.c func.o -o my_prog func.o: func.c func.h gcc -c func.c -o func.o Here, hello is dependent on: hello.c which is built explicitly using the 2nd line. If the 2nd line wasn't there, it would do it implicitly func.o which is dependent on func.c and func. If any of these is changed, then it is rebuilt using the last line.

In the following, how could we create a pointer to ap and what would the address space look like? int main() { int a = 0; int *ap = &a; }

int **app = &ap; (a pointer to a pointer)

If we have a variable: int a; How can we make a pointer to this?

int *ap = &a;


Set pelajaran terkait

PHYS_1404: Solar System; CHP 2 HW

View Set

Property and Allied / Insurance Regulation

View Set

Chapter 9: Lifespan Development (questions)

View Set

ECON compilation of all tests- FINAL

View Set

Government Principles of Government Sec. 1-1

View Set

Chapter 16 PrepU: Assessing Eyes

View Set