Showing posts with label useful tips. Show all posts
Showing posts with label useful tips. Show all posts

Tuesday, July 15, 2008

Tips'n'Tricks: GCC useful macros for debugging

Continuing my series for newbies/beginners I want to told about gcc macros useful for debugging.
Everytime on C you have deal with memory, and very big part of bugs are memory managing related.
You must know differents ways to decide question with it, wrappers to general mmap/malloc/munmap/free functions, implement your own allocator (if you really know what you're doing).
Anyway, it's better to know without debugger (just use stdout to make this deal) when you are allocate and freed memory. GCC has a several really useful macros for this.
For example let's write a simple example with free() function:

void *__my_free(char *file,int line,char *function,void *p)
{
if(p) free(p);
else {
fprintf(stderr,"Trying to free nil pointer.\n");
fprintf(stderr,"At `%s:%d' via '%s'\n",file,line,function);
}

return NULL;
}

It's really a big shit deal to point everywhere info about function, line and file ;)
Just use this macro:

#define my_free(p) __my_free(__FILE__,__LINE__,(char *)__FUNCTION__,p)

This is all, for more detailed documentation see gcc manual.

Monday, June 23, 2008

Emacs #1 - saving your state after exit , and tabbar use

Often you need to restore your emacs session, but it's not a good way to load all buffer per start, just keep it.
Also, there are useful addition to emacs called tabbar.el it can keeps your buffers without its creating.
I don't want to write where you can find this file - if you are using debian you can find it out in emacs-goodies-el package.
So, the first step is to enable tabbar mode by default:

(tabbar-mode t)

After this I recommend to bind a keys for switch between tabs:

(global-set-key [(control shift w)] 'tabbar-forward)
(global-set-key [(control shift q)] 'tabbar-backward)
(global-set-key [(control shift e)] 'tabbar-forward-group)
(global-set-key [(control shift d)] 'tabbar-backward-group)

You can use your own key bindings depending on your choice and feel.
The second step is to use a desktop addition (that comes with emacs22):

(when (fboundp 'desktop-load-default)
(desktop-load-default)
(mapcar
(lambda (symbol)
(add-to-list 'desktop-globals-to-save symbol))
'((buffer-name-history . 100)
(dired-regexp-history . 20)
(extended-command-history . 100)
(file-name-history . 500)
(grep-history . 50)
(minibuffer-history . 100)
(query-replace-history . 60)
(read-expression-history . 60)
(regexp-history . 60)
(regexp-search-ring . 20)
(search-ring . 20)
(shell-command-history . 50)))
;;; (desktop-read)
)

(add-to-list 'desktop-locals-to-save 'buffer-file-coding-system)
(add-to-list 'desktop-locals-to-save 'tab-width)

(setq-default desktop-missing-file-warning nil)
(setq-default desktop-path (quote ("~")))
(setq-default desktop-save t)
(setq-default desktop-save-mode t)

(setq-default save-place t)

And this all, this code snippets from my ~/.emacs file.