Full path folder on linux console

When you log on to linux console, usually you see something like

web@w22 log $

comp-web : user who log in;  w22  : host name ; log : current folder.

But you dont know full folder path, to show that use this command

$ export PS1="\[33[01;32m\]\u@\h \[33[01;34m\]\w \$ \[33[00m\]"

Thank to this link

VC++6 : Mix C and C++ header rule

Its just my hypotesis . C++ need  “filename.h”  in header file while  C  can accept <filename.h>

How can we mix code that contain C and C++

My best solution until now is having 2 include file. One with <filename.h>  format and one with  “filename.h ” although

the content is exactly same

Any better idea?

Simple C++ template char* and int, different strcmp output ?

#include <stdio.h>
#include <string.h>
//max returns the maximum of the two elements
template <class T> T max(T a, T b)
{
    return a > b ? a : b ;
}

// Specialization of max for char*
template <> char* max(char* a, char* b)
{
    return strcmp(a, b) > 0 ? a : b ;
}

int main()
{
    printf( "%d ", max(10, 15) );
    printf( "%s\n", max("Aladdin", "Jasmine") );
    return 0 ;
}

Output :
Windows: 15 Jasmine
Linux : 15 Aladdin

Reference:
http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm

Very simple C++ string class in 31 lines

Bismillah

Its 2 things what I learnt

 1: #include <stdio.h>
 2: #include <stdlib.h>

 3: class cstring {
 4: private :
 5:  short int i;
 6:  short int maxl;
 7:  char * data;
 8: public:
 9:  short int len;
10:  char *str;

11:  cstring()  { data = (char*) malloc( 1024); str = data; maxl = 1024; };
12:  cstring( char * c ) {  data = (char*) malloc( 1024); str = data; maxl = 1024;  strcopy( str, c ); };  // lesson 1
13:  ~cstring() { free( data );  };

14:  cstring& operator = ( char * c ) { strcopy( str, c ); return *this; };

15:  void strcopy( char * dest, char * src ) {
16:    i = 0;
17:    while( (*src != '') && (i++ < maxl) )
18:      *(dest++) = *(src++);
19:    *(dest++) = *(src++);
20:    len = i;
21:  };
22: };

23: void main() {
24:   cstring b;
25:   b = " world";                               // b.operator=( "world" );   // lesson 2
26:   printf( "%d %s\n", b.len, b.str );
27:   cstring c[] = { "hi", "there" } ;
30:   printf( "%s %d\n", c[0].str, c[1].len );
31: }

Lessons learn:

  1. You can NOT call constuctor line 11 in line 12 , ie: cstring(); because its not what you mean. Compiler will interpret it as Object creation. My friend told me that he ever do similar code but forgot how to avoid duplicate code in line 11 and line 12. Anyone have idea ?
  2. b.operator= is function name

References:

  1. http://www.cplusplus.com/doc/tutorial/classes2.html
  2. http://csl.sublevel3.org/programming/my_string/

Can someone point out what wrong with my understanding. Thanks

error: asm/socket.h: No such file or directory and solution

Someday i want to compile this very simple socket program on RHEL5 Linux when get error like that.

#include <sys/types.h>
#include <sys/socket.h>main() {
  int svr_sd = socket(AF_INET, SOCK_STREAM, 0 );
  return 0;
}:

Diagnose:

#rpm -q kernel-headers
#rpm -V kernel-headers // to verify that everytingk is OK, print nothing on OK, else here what will you get :

# rpm -V kernel-headers

missing     /usr/include/asm/socket.h

Solution :

#rpm -e –nodeps kernel-headers
#yum install kernel-headers

Note: on RHEL4 “kernel-headers” name is “glibc-kernheaders”

Moral of story:

Program error message already point to solution. Because we make an assumption that gcc/g++ can compile other program  we dont go to right direction. We ask , “how can possible asm/socket.h not found, i dont touch the system?”

Silakan minum, berani?

Terbayang di jaman dulu, ketika kebanyakan orang adalah petani.
Banyak orang menyuguhkan kendi di depan rumah, secuil sedekah bagi yang haus.

Tapi kalo hal itu ada di jaman ini, beranikah anda meminumnya ? 🙂

Berani minum ?

pthread_create valgrind memory leak solved

Its look like pthread_detach() and pthread_create()   is couple like free() to  malloc() . So … dont forget !

#include <stdio.h>
#include <pthread.h>

// unmark pthread_detach(), memory leak will be gone

void* process( void* data ) {
printf( “hello world\n” );
}

main() {
pthread_t th;
pthread_create( &th, NULL, process, NULL );
sleep( 1 );
// pthread_detach( th );
return 0;
}

/*

$ valgrind –tool=memcheck –leak-check=full –show-reachable=yes ./a.out

==23782== 272 bytes in 1 blocks are possibly lost in loss record 1 of 1
==23782== at 0x4C1DCBF: calloc (vg_replace_malloc.c:279)
==23782== by 0x400FDF2: _dl_allocate_tls (in /lib64/ld-2.5.so)
==23782== by 0x4E28A33: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.5.so)
==23782== by 0x4005BF: main (thsimple.c:11)
==23782==
==23782== LEAK SUMMARY:
==23782== definitely lost: 0 bytes in 0 blocks.
==23782== possibly lost: 272 bytes in 1 blocks.
==23782== still reachable: 0 bytes in 0 blocks.
==23782== suppressed: 0 bytes in 0 blocks.

*/

Links:

http://sourceware.org/ml/glibc-bugs/2007-04/msg00036.html

http://www.linuxquestions.org/questions/programming-9/memory-leak-in-pthread-591263/

http://sources.redhat.com/bugzilla/show_bug.cgi?id=5171