strcat not working

This is a discussion on strcat not working within the C Programming forums, part of the General Programming Boards category; Hi , I have a problem with strcat... The code is: Code: char *str="/home/bargi/"; strcat(str,"temp.txt"); This is not working and ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    strcat not working

    Hi ,

    I have a problem with strcat...
    The code is:
    Code:
    char *str="/home/bargi/";
    
    strcat(str,"temp.txt");
    This is not working and giving segmentation fault.

    Can anybody suggest me the correct way of doing it and also the mistake I am making

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's because the string literal that str points at is a constant value - it can not be modified. Nor is there any space to store the extra string that you are concatenating onto it, but that's not why it's crashing.

    You need to make it an initialized char array, and make sure it's big enough to hold the full string.

    char * variables that are initialized to a string shoudl really be declared "const char *" - that way, the compiler will tell you when you are doing silly things like trying to write to the string that should not be modified.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    The problem is that str is a pointer that points to the first char of a string literal. Attempts to modify the string literal results in undefined behaviour, and a segmentation fault is one example of that. Rather, you need to make sure that str points to the first char of a char array whose contents can be modified, and that there are enough chars in the char array to accomodate the result of strcat, including the null terminator, e.g.,
    Code:
    char str[21] = "/home/bargi/";
    
    strcat(str, "temp.txt");
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Thanks for help....

    It mean that I have to declare the size of str....

    char str[] = "/home/bargi/";

    strcat(str, "temp.txt");
    But it printing something this:

    *** stack smashing detected ***: ./str terminated
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)[0xb7e35138]
    /lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x0)[0xb7e350f0]
    ./str(__gxx_personality_v0+0x11e)[0x804859a]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7d5e450]
    ./str(__gxx_personality_v0+0x35)[0x80484b1]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:05 2064596 /home/bhagwat/malloc/str
    08049000-0804a000 rw-p 00000000 08:05 2064596 /home/bhagwat/malloc/str
    0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]
    b7d47000-b7d48000 rw-p b7d47000 00:00 0
    b7d48000-b7e91000 r-xp 00000000 08:05 3899630 /lib/tls/i686/cmov/libc-2.7.so
    b7e91000-b7e92000 r--p 00149000 08:05 3899630 /lib/tls/i686/cmov/libc-2.7.so
    b7e92000-b7e94000 rw-p 0014a000 08:05 3899630 /lib/tls/i686/cmov/libc-2.7.so
    b7e94000-b7e97000 rw-p b7e94000 00:00 0
    b7e97000-b7ea1000 r-xp 00000000 08:05 3850252 /lib/libgcc_s.so.1
    b7ea1000-b7ea2000 rw-p 0000a000 08:05 3850252 /lib/libgcc_s.so.1
    b7ea2000-b7ea3000 rw-p b7ea2000 00:00 0
    b7ea3000-b7ec6000 r-xp 00000000 08:05 3899639 /lib/tls/i686/cmov/libm-2.7.so
    b7ec6000-b7ec8000 rw-p 00023000 08:05 3899639 /lib/tls/i686/cmov/libm-2.7.so
    b7ec8000-b7fb0000 r-xp 00000000 08:05 5226835 /usr/lib/libstdc++.so.6.0.9
    b7fb0000-b7fb3000 r--p 000e8000 08:05 5226835 /usr/lib/libstdc++.so.6.0.9
    b7fb3000-b7fb5000 rw-p 000eb000 08:05 5226835 /usr/lib/libstdc++.so.6.0.9
    b7fb5000-b7fbb000 rw-p b7fb5000 00:00 0
    b7fc9000-b7fcc000 rw-p b7fc9000 00:00 0
    b7fcc000-b7fcd000 r-xp b7fcc000 00:00 0 [vdso]
    b7fcd000-b7fe7000 r-xp 00000000 08:05 3850309 /lib/ld-2.7.so
    b7fe7000-b7fe9000 rw-p 00019000 08:05 3850309 /lib/ld-2.7.so
    bf8a4000-bf8b9000 rw-p bffeb000 00:00 0 [stack]
    bargiAAAAAborted
    Can you explain me the reason....??

    Also is this rule apply for strcpy() function and what are the best ways to implement them...

    I will be thank ful to you


    Thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Bargi
    It mean that I have to declare the size of str....
    Yes, if you initialise str that way (without explicitly stating the size of the char array) it will have only enough space for the original string. You would not be able to correctly concatenate anything.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    strcat(str, "temp.txt");

    Now you are adding 8 more bytes to str, which is already ONLY 13 bytes. You need to start with a bigger str:
    Code:
    char str[32]="/home/bargi";
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    thanks laser for your help.......

    I want some help from regarding C programming....

    Actually I have just started working on C specific project and there are lots of pointer memory intialization in this....

    Can you suggest me the best ways in initialization of memory and ways to handle and debug memory faults....as I am new to C programming...

    Thanks for your help...

    Regards
    Bargi

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bargi View Post
    I want some help from regarding C programming....

    Actually I have just started working on C specific project and there are lots of pointer memory intialization in this....
    A great way to "get comfortable" with pointers and memory management is to do a simple linked list exercise using a tutorial (find one you like); just write a basic linked list program where you can add and remove nodes, and print the list properly to the screen.

    By the time you're done, you'll have a bunch more experience with malloc() and free() and using pointers to access blocks of data in memory.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 02:49 AM
  3. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  4. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21