strcat() and segmentation faults. But malloc() already done.

This is a discussion on strcat() and segmentation faults. But malloc() already done. within the C Programming forums, part of the General Programming Boards category; Hi, I'm puzzled. Why does the following cause a seg fault? Notwithstanding that I've already malloc() a certain space for ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    strcat() and segmentation faults. But malloc() already done.

    Hi,

    I'm puzzled. Why does the following cause a seg fault? Notwithstanding
    that I've already malloc() a certain space for "Hello".

    I do understand that using a fixed length array will work very well.
    But I wish to find out how can this be achieved using pointers.

    Thank you.

    Stan

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
            char *Hello = NULL;
            char *World = NULL;
            Hello = malloc(100);
            Hello = "hello";
            World = "world";
            strcat(Hello, World);
            printf("%s\n",Hello);
    
            return 0; 
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > Hello = malloc(100);
    Ok, you allocated space

    > Hello = "hello";
    Oops, you just lost it again (and you've just leaked some memory)
    You probably wanted strcpy(Hello,"hello");

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Oh. Silly me. I must copy <somethings> into the pointed location.

    Thank you Salem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
            char *Hello = NULL;
            char *World = NULL;
            Hello = malloc(100);
            strcpy(Hello, "hello");
            World = "world";
            strcat(Hello, World);
            printf("%s\n",Hello);
    
            return 0; 
    }

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    In this case,

    Do I have to malloc() every single char pointers? For eg:

    Code:
    World = "world";
    If I do not malloc() "World", am I right to say that "World" is just writing somewhere in the memory? (Having a possibility of overwriting some other app's memory space?)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    No, the compiler allocates space for "strings" automatically.

    So when you do
    World = "world";
    You're just assigning a pointer to the start of that memory the compiler set up for you.

    The reason for the segfault is these "strings" really are constant, and the OS will stop your program if you try and modify them (as your first strcat tried to do).

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    printf("%s\n",Hello);
    ->
    Code:
    puts(Hello);
    And don't forget to use free().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault on strcat
    By paxmanchris in forum C Programming
    Replies: 9
    Last Post: 04-24-2007, 10:04 PM
  2. strcat causing segmentation fault?
    By jbsloan in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 09:42 AM
  3. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM
  4. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 06:56 AM
  5. segmentation fault - pointers/functions
    By p1c1078 in forum C Programming
    Replies: 15
    Last Post: 04-22-2003, 05:46 PM

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