C/C++ Technical Notes

1. C/C++ does not support adding members to a struct dynamically, which means the member list of a struct is fixed once this struct type is defined.


2. In a console program, getchar() will obtain the first character in stored in stdin, even if there is no prompt is shown on the screen.


3. The stdin in C is line-buffered, which means the program stdin buffer will not flush the data to the receiver until a "Enter" character is encountered.


4. Pointers declared without a unary operator cannot be dereferenced.


5. In ANSI C, a character string( char * type) variable is ended up with a "\0" null character appended.


6. In ANSJ C, out of bound memory access is allowed:

char * c = "abcd";
   if(c[6] == 0)
       printf("%s", "Null Found");
   return 0;


==============>    No compilation error


7. .o file

.o file is the objective file generated by GCC after compilation, it should be used to link with c libraries to product an executable file.

.out file is the executable file compiled from source, usually .out is omitted by gcc in linux, therefore the executable file generated directly from source without linking any library has no file suffix.


8. prinf

printf modifier:

%f, floating number

%d, floating decimal

%s, string

%c, character


9. for loop iterator initialization

The iterator must be declared out of the loop beforehand due to C89/90, the initialization can be done within the for() statement


10. Environment Variable

Environment variables are actually bash shell variables (Linux) or Windows CMD variables (Linux).

The value of an environment variable is usually a file path or a constant. 


11. #pragma comment(lib, "xxxx.lib")

compiler directive in vc++

#pragma comment is a compiler directive which indicates Visual C++ to leave a comment in the generated object file. The comment can then be read by the linker when it processes object files.


12. The evaluation of 0==NULL in ANSI-C will be True


13. .a file (archive) in linux is the static library

static library: introduced by the compiler in compiler time

dynamic library (shared library): introduced by the executable in run time


14. GCC compiler predefined macros: __linux__


15. GCC 4.8.2

The default, if no C language dialect options are given, is -std=gnu90.Some features that are part of the C99 standard are accepted as extensions in C90 mode, and some features that are part of the C11 standard are accepted as extensions in C90 and C99 modes.

Use of the -std options listed above will disable these extensions where they conflict with the C standard version selected. You may also select an extended version of the C language explicitly with -std=gnu90 (for C90 with GNU extensions), -std=gnu99 (for C99 with GNU extensions) or -std=gnu11 (for C11 with GNU extensions). 


16. Void is a type and it means no value at all, the void type pointer has a value of null.


Void is no return at all, though some implementations return a null pointer on the stack for compatibility with old C, in which you didn't need prototypes or declarations.  This is less often true in C++, unless you use extern "C", which is probably the stupidest syntax this side of APL.

NULL is a pointer word of zero.  It occupies 32 bits on a 32 bit machine and 64 on a 64.  NULL does not mean that the variable has no value; it means that, as a pointer, it points to no valid value.  Technically, it points to the memory beginning at address 0, but that is usually out of the valid address space of the process.  On older computers, the word at 0 was often always set to 0 so that a dereference wouldn't blow up.  This hid many sins and made implementations of LISP a bit faster.


17. Type and Class

An objects's class defines how the object is implemented .The class defines object's internal state and the implementation of its operations.

In contrast, an objects's type only refers to its interface -the set of requests to which it can respond.

An object can have many type, and object of different classes can have the same type.

//example in c++
template<typename T> 
const T & max(T const & a,T const &b)
{
return a>b?a:b;  //> operator of the type is used for comparision
}

max function requires a type with operation > with its own type as one of it interface any class that satisfies the above requirement can be used to generate specific max function for that class.


18. The declaration of a pointer can only be achieved by using & operator or malloc(). A declaration such as int *p_x = 1 is not allowed.

The dereference operator can only be applied on non structure object, the following operation is not allowed:

struct tag_x structure;

struct tag_x *p_structure = &structure;

print("%d", *p_structure.value)


Reason: if structure.value is a pointer, then the expression *p_structure.value can have two interpretations. In this case, () should be used to indicate the order of the operation.


19. When initialized, int * p_x = &x means a int typed pointer variable is assigned to the address of variable x. (equivalent to int *p_x = &x, but express a much clearer meaning).

      When *p_x is used in dereferencing a pointer, it acts as a operator on pointer p_x to access the value that p_x points to. In this usage, *p_x is equivalent to x. 


20. Pointers are passed by value when used as a function argument when called, like the int type variables

C99 and C++ standards don't require functions to return a value. The return with no value in a value-returning function will be defined (to 0) only in the main function.

(1) This is calling convention and architecture dependent.
(2) The return value is the result of last expression evaluation, stored in the eax register.


21. A pointer is a variable, but an array name is not a variable.

Array indexing are converted to pointer arithmetic automatically.

For one dimensional array, the array name can be implicitly converted into a pointer in many cases.

Such as :

a)

        int a[5];

        int* p_x = a;

(This fails for multidimensional arrays)


b) pass array name into a function as argument


It should be avoided to use array name in cases where a pointer is available. (mix use array with pointer, use array name as "pointer")


22. char* my_str = "123x...y"; 

strlen(my_str) = sizeof(my_str)/sizeof(char) - 1


strlen would not count the ending "\0" character.


23. In C++, array name can be used as a constant array pointer.

int my_arr[2] = {1,3}

int * p = &my_arr[0]

int * p = my_arr


24. 

用g++编译程序时,-l 与-L各是什么意思?还有-I

-l

表示:编译程序到系统默认路进搜索,如果找不到,到当前目录,如果当前目录找不到,则到LD_LIBRARY_PATH等环境变量置顶的路进去查找,如果还找不到,那么编译程序提示找不到库。
-L
表示:编译程序按照-L指定的路进去寻找库文件,一般的在-L的后面可以一次用-l指定多个库文件。
-I
表示:编译程序按照-I指定的路进去搜索头文件。


25. Dynamic Linking Keywords:

/usr/local/lib

ldconfig /xxx/xxx


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值