C Programming lang
About C - copied from note.
References
- Change array as pointers: https://www.dummies.com/programming/c/how-to-build-an-array-of-pointers-in-c-programming/
- Memory : https://www.youtube.com/watch?v=NKTfNv2T0FE&list=PLhQjrBD2T382_R182iC2gNZI9HzWFMC_8&index=5
Why learn C ?
Learning C programming has lot of benefits, but the foremost thing in which it helps is to understand the underlying architecture of how things works?
Consider a situation where a person learns to drive a Car. In this modern era with advancement in technology, we have many options when it comes to buying car. There are cars with auto-driving mode, auto gear change features etc which reduces the manual overheads and makes driving the car easier. Suppose the person learns driving on a auto-gear change enabled car. After learning driving, the person applied for a driving licence for which he need to pass a driving test. The driving test is now on a manual car with no auto-gear change feature. The person was even not able to answer some basic questions related to gears as he was not even aware of it and eventually ended up getting disqualified.
Why Pointers
Biar memahami concept, actually, di java pointer ada cuma implicit: reference.
Why pointers need data type declaration ?
The Data type is needed when dereferencing the pointer so it knows how much data it should read. For example dereferencing a char pointer should read the next byte from the adress it is pointing to while an int pointer should read 2 bytes.
What actually size of pointers ?
It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed. (It's common as another data type)
What if pointers not initialized any value (uninitialized value) ?
If the pointer contains an uninitialized value, then the value might not point to a valid memory location. This could cause the program to read from or write to unexpected memory locations, leading to a denial of service. If the uninitialized pointer is used as a function call, then arbitrary functions could be invoked. If an attacker can influence the portion of uninitialized memory that is contained in the pointer, this weakness could be leveraged to execute code or perform other attacks. Unless a value is assigned, a pointer will point to some garbage address by default.
In most cases, indirection through garbage pointer will crash your application.
What is dangling pointer ?
Dangling pointers arise during object destruction, when an object that has an incoming reference is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.
Subtract pointer from another to get int ?
yes, we could. Another, subtract int from a pointer to get pointer.
Array out of bounds ?
expression vs
What is we assign pointer with number while the pointer doesn't initialized
the memory should be allocated first, so that
About const ? What they could do actually ?
- const int nilaiKu = 89; you could not change value of 89.
- const int *pKu = &nilaiKu; you could not change 89 via *pKu.
- int *const pKu = &nilaiKu; you could not change address they're pointing.
- Number 2 and 3 could be combine together.
What do void pointer?
it's a nice referencing/assign multiple data type variable. Example : void *ptr = NULL, int d = 100, float f = 200, Assign it
- ptr = &d, after that
- ptr = &f.
It could. If you wanna dereferencing it, we need use explicit cast.
- *(int *) ptr
Very nice stuff.
All these arrays are pointer underneath the hood.
int newYork[100] = ...
int *pHello;
so u can pHello = newYork;
Memory leaks?
when you allocate memory dynamically and do not retain the reference to it, so unable to release the memory.
Malloc vs Calloc
It would be better use malloc over calloc, unless we want the zero-initialization because malloc is faster than calloc. So if we just want to copy some stuff or do something that doesn't require filling of the blocks with zeros, malloc betteer choice.
Array can't assign ?
The number of elements of an array must be determined either at compile time or since C99 can be evaluated at runtime at the point of creation. Once the array is created, its size is fixed and cannot be changed.
int a[] = { 1, 2, 3 }; // equivalent to int a[3] = { 1, 2, 3 };
int m[][2] = {{ 1, 2 }, { 3, 4 }}; // equivalent to int m[2][2] = {{ 1, 2 }, { 3, 4 }};
char s[] = "Hello world\\n"; // equivalent to char s[13] = "Hello world\\n";
Note how the compiler adds the implicit null terminator in the string case.
Struct?
Grouping elements together.
Example : date
Defining (telling this is the structure and data inside it) → Declaring (i'm gonna start using that type) → Accessing
initiate
struct date Now = {.year = 2015};
Expression vs Statement
expression resolve to value, ex: return function.
statement just perform action and control action but don't become values.
Declaration and Intialization
usage of pointers
- Pointer mostly used in dynamic memory allocation. Using the memory management function we can get the memory during the execution of a program.
- Pointers can be used to access the array elements.
- We can access the memory location with the help of C Pointers.
- Pointers are used in “call by reference”. In which we can pass the address of a variable ( function, array, ..etc) into a function.
- Pointers are used in complex data structures like linked lists, trees, etc.
Why learn data structure?
Data structures are collections of values, the relationships among them, and the functions or operations that can be applied to the data
misalnya: list di js, ada operasi/function push
why many of them
Different data structures excel at (be good at) different things. Some are highly specialized, while others (like arrays) are more generally used.
why care
The more time you spend as a developer, the more likely you'll need to use one of these data structures. You've already worked with many of them unknowingly!
example
- Working with map/geolocation data : use graph
- Need ordered list with with fast inserts/removals at the beginning and end : use linked list
- Web scrapping nested html : use tree
- Need to write scheduler : use binary heap
Why so much of algorithm using searching and sorting ?
because actually we really live with data. What best thing you could do with data ? lot
0 Response to "C Programming lang"
Post a Comment