![]() |
.. (לתיקייה המכילה) | |
|
|
C99 |
|||
What's the difference between
| ||||
When dealing with pointers const may mean two different things:If we declare a type with const as its first word, we create a "pointer to a const int". This means we have a non-const pointer, and it takes addresses of const int. So for example:int x = 5;In this example, *ptr is of type const int and therefore the last line raises an error, as the value of a const int cannot be changed.The other case, is when const is not the first word in the type definition. In this case, the const affects whatever is on its left. So the type int* const is a "const pointer to an int". This means such a pointer must be initialized and its value (the address it stores) cannot be changed. However, when dereferencing (i.e. reading) this pointer, we get an int which can be freely changed:int* const cptr = &x; // must be initializedWe can also combine both versions and get a "const pointer to a const int": const int* const cptr = &x; // must be initialized
|
|
|
ADT |
|||
In some cases you check the input of a function with the
| ||||
It's not necessarily a black & white answer, but the guiding rule of thumb here is this:
|
|
|
Generic ADTs |
|||
What is going on with those nasty
| ||||
Well, by now, you probably noticed that the syntax for pointers to functions is quite messy and annoying. For example, if we want to declare a pointer to a copy function of a generic element we need to write:void* (*copy)(void*) = NULL;This declares a pointer that can store the address of a function, which takes one argument of type void* and returns a void*. However, once we have a few of those pointers in our set, the syntax becomes even less readable. And the user might get lost through the collection of pointers with void* spread around though them.We therefore first give a better name to a pointer to an element in the set: typedef void* SetElement;This lets our code differentiate whether a void* pointer should point to an element or something else. (as is seen in the setFilter function)We now want to say that a void* (*copy)(void*), is not just a function taking a void* and returning one. It is a function for copying an element, so we give a new name to this type:typedef void* (*SetCopyFunction)(void*);If you compare this line to the previous declaration line, you will see that the only difference is the addition of the typedef keyword, which means we now declare a type name, and not a variable, and the fact that the name is now SetCopyFunction. This means that from now on SetCopyFunction is a shorthand for void* (*)(void*) and can be used much more conveniently when messing with such variables. |
|
|
Testing |
|||
What is going with this nasty
| ||||
|
This macro is used to initialize a set with several strings using one line only. It is very useful, and to see that simply look at the test and how much it uses ASSERT_SET_CONTENTS which depends on this macro. (Think how much more cumbersome will all the assertions look without it)To understand what it does, notice that the mission of the macro is as follows:
This macro uses a C99 feature for creating a macro which receives a variable argument list, that is, any number of arguments. To use this argument list in the macro's code, we write __VA_ARGS__.We therefore take the list of strings, such as "blue", "red", "green" and paste it inside curly braces. In addition, we explicitly cast it to char*[] so the compiler will know that is what we meant. (This for example can be the initialization of a struct as well, and since it depends on the context we must give the compiler an additional hint)Once we did that, we have our array of strings. However, to find its size, we write this array again inside a sizeof operator and divide its size by the size of one string. |
|
|
Introduction to C++ |
|||
What is the deal with those references? Why not just use pointers?
| ||||
|
|
miscellaneous |
||
|
| |||
|
|
miscellaneous |
||
|
| |||
|
|
miscellaneous |
|||||||||||
There's a huge list of tools appearing in the tutorials, I got a bit lost
Should I install Linux on my computer, or on a virtual machine?
It is written that you cannot take the address of a temporary value such as "(2 + 3)", does it apply for a
| ||||||||||||
|
No, the meaning of this slide is as follows: When you write in your code 5 or (a + b) it puts a temporary value. This value has a type (int in the first case, for example), but unlike a variable of the same type, you cannot use the & operator to take its address, as this temporary value's address has no meaning (and might not even exist after the compiler does its work).However, if you have a const int INVALID_MONTH = -1; you have a real variable in your code, and &INVALID_MONTH is just of type const int*.
|
The previous list is also not very short. Can you make it even simpler?
|
Yes. Download Eclipse and do most of your on it. Download SSH and use it to connect to the stud, copy files to it and check your code on it. Invest the time to learn Vim when you can. |
In the slide about undefined behavior, what happens there? It makes no sense!!
We're talking about the slide with the following code:#include <stdio.h>And it is being compared to the code in which int i; appears before int a[N] = {0};.First, note that this code accesses memory outside the array, this happens when i=6, in which case N-1-(i+1) is -1. This means that we are effectively writing a value outside the array and this is undefined. This means, that once we invoke such behavior, the program cannot be expected in any specific way. In practice, this is an example for how weird can undefined behavior get: In this case, writing 0 to a nearby memory address can result in rewriting the value of i to 0, thus, the loop will never end because every time i=6, 0 will be written to it. However, simply changing the declaration order, changes the location of variables in the memory and might cause a completely different behavior. The moral of the story: Once you invoke undefined behavior, everything can happen - including nothing, in which case it's hard to notice you did something wrong. |

