week 2 c programming code Flashcards

to learn more about c programming

1
Q

what is a string in c#

A

string of characters is a 1D array of characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how much memory does ASCII store

A

(1 byte) of each character element is stored in
consecutive memory locations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is string terminated by

A

String is terminated by the null character ‘\0’ (ASCII value 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is the length of the null string

A

The null string (length zero) is the null character only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is use to take input from user in c#

A

scanf()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is used to output info to user

A

printf()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the format for string in c

A

%s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the 4 safe string functions

A

strcpy()
strcat()
strlen()
strcmp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is strcpy()

A

copies the string from one destination to another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what do you need to make sure that you do in strcpy()

A

need to allocate memory to the destination where you want to put your string
e.g
char[10] destination; -> where i will put the string in;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what does strlen do

A

gets the length of the string;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

does strlen take into account the null value

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does strcat do

A

concatenates 2 strings together

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

in strcat(str1,str2) which string is modified and which stays the same

A

str2 stays the same and str1 changes as str2 is appended to teh end of str1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does strcmp do

A

compares 2 strings and see if they are the same

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

if strcmp conpares 2 strings and finds them to be equal then what does it return

A

it returns 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

if strcmp conpares 2 strings and finds them to not be equal then what does it return

A

returns the numeric difference between the first
non matching characters
or -1 i need to check

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what are string problems

A

Need to ensure that sufficient memory is allocated at run-
time for storing the string - not automatically done by the
compiler

  • No checks at run-time (and compile-time), hence common
    source of errors
  • Such “buffer overflows” have been exploited to achieve
    execution of code supplied by attackers
  • Partial solution is to use “safe” functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what is a partial solution to string problems

A
  • Partial solution is to use “safe” functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what are safe string functions

A

strcpy() strcmp , strlen , strcat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

what is wrong with this
strncpy(char *dst, char * src, size_t num)

A

Copies the first num characters of src to dst. If the end of src
(signaled by a null char) is found before num characters have been
copied, dst is padded with zeros until a total of num characters
have been written to it.

No null-character is implicitly appended at the end of dst if src is
longer than num. Thus, in this case, dst shall not be considered a
null terminated C string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

what is the definition of a pointer

A

A pointer is a variable that contains the address of a
variable
pointer is also stored in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

what is the unary operator*

A
  • is also calld indirection or dereferencing operator
    applied to a pointer to accesses the object the pointer points to
    e.g c = 5 , *p = &c , d = p now d = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

what is unary operator &

A

‘address-of’ operator
points to address of object
p =&c
now p now has address of c so p points to c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
how to declare a pointer in C#
T *p T is any data type like int or char
26
does string exists in c#
no string does not exists we use char[] instead
27
do & and * have a higher precedence then arithmetic operations
yes
28
can pointers appear in expressions?
* Pointer variables can appear in expressions * If ‘p’ points to the object ‘x’, then *p can occur in any context
29
can a pointer variable be assigned to another pointer variable of the same type?
yes , a pointer variable can be assigned to another pointer variable of the same type
30
scale factors in point expressions remind me to change this card
If p is a pointer then p++ or p+1 ➢ points to the next object of the same type. So, ➢ value of p increments by 4 when p is an int pointer ➢ value of p increments by 4 when p is a float pointer ➢ value of p increments by 1 when p is a char pointe
31
what is the syntax to finding out the scale factor in C#
sizeof(data_type)
32
what is the scale factor in C
number of bytes used by a data-type
33
what can i use to find scale factor in C
sizeof()
34
how many bytes in int char float *int *char *float
int - 4 char - 1 float - 4 *int - 8 *char - 8 *float -8
35
what is an array in C
sequential collection of elements of the same type
36
how does pointer work in an array in C
int a[10]; int *p = &a[0]; p is a pointer to the first element of array a int b = *p; will copy value of a[0] into b
37
accessing array elements using pointers
If p points to the first element a[0], then * p+1 points to a[1] * p+i points to a[i] So, *(p+i) refers to the content of a[i]
38
what does *(p + i) refer to
refers to the content of a[i] in array
39
write for loop to compute sum of array of integers using pointers
int *p = &a[0]; int sum=0, i; for(i=0; i<5; i++) sum = sum + *(p+i);
40
what is array - name a synonymn of
synonym for the location of the initial element
41
what can int *p = &a[0]; also be written as using array name
int *p = a;
42
write for loop for sum of array of integers using array name and pointers
int sum = 0; for (int i = 0 ; i < a.length ; i ++) { sum = sum + *(a + i); }
43
What is the memory layout of a C program
1) Text or Code segment 2) Data segment 3) stack segment 4) heap segment
44
what is the Text / code segment
Text segment contains the the program i.e. the executable instructions
45
what is the data segment
Two data segments contain initialized and uninitialized global and static variables respectively
46
what is the stack segment
Stack segment is used to store all local or automatic variables. When we pass arguments to a function, they are kept in stack
47
what is the heap segment
Heap segment is used to store dynamically allocated variables are stored
48
what is a static variable
* Static variables are stored in the data segment, not in the stack. * The data segment is active during the entire life-time of program * So, static variables preserve their values even after they are out of their scope.
49
what is a global variable
A global variable is declared outside all functions. * It can be read or updated by all functions. Careful: Do not name any local var in the name of a global var
50
why is using an array not good for a large volume of data
insufficient wasteful tiny computers can not hold large amounts of data
51
what is the solution for dynamci memory allocation
allocate memory at runtime as per demand
52
why is allocating memory at runtime as per demand a good solution
optimal usage of memory depending on current case of applications
53
how can we acces memory aloocation in C
#stdlib.h
54
what syntax can we use for memory allocation in C
malloc(c)
55
what is malloc(c)
allocates requested amount of bytes of contiguous memory from heap returns a pointer of first byte in allocated space or returns null pointer if memory fails
56
what is free(p)
releases dynamically allocated blocks of memory block of memory returns to heap
57
what is something you make sure you do with free() when doing a function
use free() before finishing requests all malloc() need to use free()
58
how to avoid memory leaks in C
always use a free() after you have used a malloc()
59
what happens when there is a memory leak
blocks of memory not returned to heap so program grows larger over time
60
what are the consequences of a memory leak in C
- leaks slow down system preformance as they reduce the amount of available memory - modern applications ystems -> releases memory used by applications when application terminates - memory leaks causes serious issues on resource constrained embedded devices
61
what happens when free() is called twice for the same address and what is the consequence.
if called twice for same address program's memory management becomes corrupt con: prog malfunctions including security vuneralbilities
62
what is a linked list in C
- collection of data elements called nodes, -> search nodes points to next nodes in a list -> not stored in contiguous locations like arrays -> linked using pointers
63
implementations of singly linked list in C
1) append elements at end of the list 2) search for elements in list 3) print , gives start of list 4) free memory is occupied by list
64
what do nodes consist of in C
data reference to the next node ( pointer -> )
65
what data type is node in C
composite data type
66
what is Null in a linked list
end of a list
67
instead of class , what do we use in c
we use structure in C
68
what is Structure in C
User defined composite data type in C Use to deinfed items of possibly different data type into one single data type no member functions
69
what is syntax of structure
struct{ T a; T b; }
70
can you write struct{} in code
struct{ int x; int y; } int main() { struct point p1; }
71
what is an alternative version to using struct{}
typedef struct var{ T a; T b; } v; v -> variable name that we call in other functions
72
how are structure members accessed in different functions
using the dot (.) operator e.g Point p1; p1.x = 1;
73
give example of using typedef for co - ordinates x and Y
typedef struct Point { int x; int y; } point;
74
how are pointers created in structure members
using pointer ( -> ) operator
75
what is "pass by value"
pass data objects to functions as arguments
76
what is "pass by reference "
pass pointers to a function as arguments (no side effects)
77
consequences of using pass - by - value for functions
function gets a local copy of variable fumction only happens within the function and will not be printed out in the main or another function
78
consequences of using pass - by - reference for functions
function gets a local copy of pointer ehich has address of variable pointer update so you get results of the function thst was executed memory locstion is where the variable is stored both function and main can see the result of the variable
79
in a function can pointers be returned
yes they can pointers of the same data type can be returned
80
what one thing you should not return pointers to
do not return pointers to local var do not return address of a variable too