week 2 c programming code Flashcards
to learn more about c programming
what is a string in c#
string of characters is a 1D array of characters
how much memory does ASCII store
(1 byte) of each character element is stored in
consecutive memory locations
what is string terminated by
String is terminated by the null character ‘\0’ (ASCII value 0)
what is the length of the null string
The null string (length zero) is the null character only
what is use to take input from user in c#
scanf()
what is used to output info to user
printf()
what is the format for string in c
%s
what is the 4 safe string functions
strcpy()
strcat()
strlen()
strcmp
what is strcpy()
copies the string from one destination to another
what do you need to make sure that you do in strcpy()
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;
what does strlen do
gets the length of the string;
does strlen take into account the null value
no
what does strcat do
concatenates 2 strings together
in strcat(str1,str2) which string is modified and which stays the same
str2 stays the same and str1 changes as str2 is appended to teh end of str1
what does strcmp do
compares 2 strings and see if they are the same
if strcmp conpares 2 strings and finds them to be equal then what does it return
it returns 0
if strcmp conpares 2 strings and finds them to not be equal then what does it return
returns the numeric difference between the first
non matching characters
or -1 i need to check
what are string problems
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
what is a partial solution to string problems
- Partial solution is to use “safe” functions
what are safe string functions
strcpy() strcmp , strlen , strcat
what is wrong with this
strncpy(char *dst, char * src, size_t num)
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
what is the definition of a pointer
A pointer is a variable that contains the address of a
variable
pointer is also stored in memory
what is the unary operator*
- 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
what is unary operator &
‘address-of’ operator
points to address of object
p =&c
now p now has address of c so p points to c
how to declare a pointer in C#
T *p
T is any data type like int or char
does string exists in c#
no string does not exists we use char[] instead
do & and * have a higher precedence then arithmetic operations
yes
use of pointers in expressions (1)
- Pointer variables can appear in expressions
- If ‘p’ points to the object ‘x’, then *p can occur in any context
Use of pointer in expressions (3)
A pointer variable can be assigned to another pointer variable of the same type
scale factors in point expressions
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
what is the syntax to finding out the scale factor in C#
sizeof(data_type)
what is the scale factor in C
number of bytes used by a data-type
what can i use to find scale factor in C
sizeof()
how many bytes in
int
char
float
*int
*char
*float
int - 4
char - 1
float - 4
*int - 8
*char - 8
*float -8
what is an array in C
sequential collection of elements of the same type
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
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]
what does *(p + i) refer to
refers to the content of a[i] in array
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);
what is array - name
synonym for the location of the initial element
what can int *p = &a[0]; also be written as using array name
int *p = a;
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);
}
What is the memory layout of a C program
1) Text or Code segment
2) Data segment
3) stack segment
4) heap segment
what is the Text / code segment
Text segment contains the the program
i.e. the executable instructions
what is the data segment
Two data segments contain initialized and
uninitialized global and static variables
respectively
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
what is the heap segment
Heap segment is used to store dynamically
allocated variables are stored
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.
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
why is using an array not good for a large volume of data
insufficient
wasteful
tiny computers can not hold large amounts of data
what is the solution for dynamci memory allocation
allocate memory at runtime as per demand
why is allocating memory at runtime as per demand a good solution
optimal usage of memory depending on current case of applications
how can we acces memory aloocation in C
stdlib.h
what syntax can we use for memory allocation in C
malloc(c)
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
what is free(p)
releases dynamically allocated blocks of memory
block of memory returns to heap
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()
how to avoid memory leaks in C
always use a free() after you have used a malloc()
what happens when there is a memory leak
blocks of memory not returned to heap so program grows larger over time
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
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
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
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
what do nodes consist of in C
data
reference to the next node ( pointer -> )
what data type is node in C
composite data type
what is Null in a linked list
end of a list
instead of class , what do we use in c
we use structure in C
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
what is syntax of structure
struct{
T a;
T b;
}
can you write struct{} in code
struct{
int x;
int y;
}
int main() {
struct point p1;
}
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
how are structure members accessed in different functions
using the dot (.) operator
e.g
Point p1;
p1.x = 1;
give example of using typedef for co - ordinates x and Y
typedef struct Point {
int x;
int y;
} point;
how are pointers created in structure members
using pointer ( -> ) operator
what is “pass by value”
pass data objects to functions as arguments
what is “pass by reference “
pass pointers to a function as arguments (no side effects)
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
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
in a function can pointers be returned
yes they can pointers of the same data type can be returned
what one thing you should not return pointers to
do not return pointers to local var
do not return address of a variable too