Thursday, 6 March 2014

error: stray ‘\302’ in program


Make and meet this error!

$ gcc -g -Wall -o test callback_simple.c
callback_simple.c:7:1: error: stray ‘\302’ in program
callback_simple.c:7:1: error: stray ‘\240’ in program
callback_simple.c:9:1: error: stray ‘\302’ in program
callback_simple.c:9:1: error: stray ‘\240’ in program
callback_simple.c: In function ‘sig_handler’:
callback_simple.c:13:5: error: stray ‘\302’ in program
callback_simple.c:13:5: error: stray ‘\240’ in program

\302 is the octal representation of the UTF-8 sequence 0xC2

You have invalid chars in the source. If you don't have any valid non ascii chars in your source, maybe in a double quoted string literal, you can simply convert your file back to ascii with the following command:
tr -cd '\11\12\15\40-\176' < old.c > new.c
It works!

Sunday, 19 January 2014

Address out of bounds 错误分析

遇到Address out of bounds 错误,原来是数组的大小超过了栈的大小!


栈大小是有限的

VC默认的分配给栈的空间是1MB~2MB,体积比较大的对象不适合在栈中分配.特别
要注意递归函数中最好不要使用栈对象,因为随着递归调用深度的增加,所需的栈
空间也会线性增加,当所需栈空间不够时,便会导致栈溢出,就会产生运行时错误.

如果真的需要的话,在数组定义前面加上static,将数组分配在静态内存中,静态内存相比起堆栈要大很多的.
或者直接把这个数组定义成全局的,这样也会将其分配在静态内存.

vc6/7 默认1M
不过这个可以修改的Project->Setting->Link,在Category   中选中Output,然后在Reserve中设定堆栈的最大值和commit   


看一下stack是否被限制,如果是unlimited,那么栈的大小只受内存大小限制,当然还有一个2G的上限。至于是否能达到2G上限,答案是否定的,因为除了栈,程序还有别的地方会用到内存。不过linux在某种方式下可以用到最高64G的内存,不知道栈是否也可以突破2G的限制?

Linux下也是可以修改的:
先用命令 ulimit -a 看看默认的stack size
user@ubuntu-64bit:~/mysql_study$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15821
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 15821
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

然后再用命令将其修改,这里,改大一点做测试。
user@ubuntu-64bit:~/mysql_study$ ulimit -s 9999999

改了后发现‘Address out of bounds’ 的错误消失, oh yeah.

用动态分配, malloc free 要好一些~~~~

Thursday, 16 January 2014

the vim cscope easy tutorial

The following steps to help you to set up cscope easily.

1. install cscope:
user@ubuntu-64bit:~$ sudo apt-get install cscope

2. Download the cscope_maps.vim file, put it to $HOME/.vim/plugin directory:
user@ubuntu-64bit:~$ mv Desktop/cscope_maps.vim .vim/plugin/  

3. Try setting the $CSCOPE_DB environment variable to point to a Cscope database you create:
    find /my/project/dir -name '*.c' -o -name '*.h' > /foo/cscope.files
   Then run Cscope in the same directory as the cscope.files file (or use 'cscope -i /foo/cscope.files'), then set and export the $CSCOPE_DB variable, pointing it to            the cscope.out file that results):
    cd /foo
    cscope -b 
    CSCOPE_DB=/foo/cscope.out; export CSCOPE_DB    


add the commands:
    " The following maps all invoke one of the following cscope search types:
    "
    " 's' symbol: find all references to the token under cursor
    " 'g' global: find global definition(s) of the token under cursor
    " 'c' calls: find all calls to the function name under cursor
    " 't' text: find all instances of the text under cursor
    " 'e' egrep: egrep search for the word under cursor
    " 'f' file: open the filename under cursor
    " 'i' includes: find files that include the filename under cursor
    " 'd' called: find functions that function under cursor calls


    " To do the first type of search, hit 'CTRL-\', followed by one of the
    " cscope search types above (s,g,c,t,e,f,i,d).