Why does the strcat function give me a segmentation fault? [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • How do I concatenate two strings in C?

    10 answers



  • How do I concatenate const/literal strings in C?

    17 answers




I want to concatenate "/bin/" and "touch" so that I will have "/bin/touch".



In my program, I have



        char* filePath = malloc((strlen("/bin/") + strlen(rv[0]))* sizeof(char));
filePath = strcat("/bin/",rv[0])


First of all, rv[0] contains a string, "touch". I allocate 10 bytes in memory by using malloc function, and filePath will be the pointer to those 10 bytes of memory. Because, the total length of the string concatenated ("/bin/touch") will be 10.



The program executes normally until the second line which gives me a segmentation fault. Did I make any mistake on the strcat function?










share|improve this question













marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You're appending to a string literal. strcat does not work like that. You'd strcpy to filePath and then strcat
    – Antti Haapala
    Nov 11 at 10:04






  • 1




    Also you're not allocating space for the null terminator (+ 1), and sizeof(char) is 1 by definition.
    – Antti Haapala
    Nov 11 at 10:05












  • When copying strings, strcat() copies the input string up to and including the zero terminator. Your malloc() size does not allow for the presence of that zero terminator, so is one character shorter than needed. strcat() therefore writes past the allocated end of filePath - which gives undefined behaviour. A call of strcat() should not even compile when given two string literals. If it is, then either your code is forcing it in some way you haven't shown, or your compiler/library is flawed. I'll bet on the former.
    – Peter
    Nov 11 at 10:07

















up vote
0
down vote

favorite













This question already has an answer here:




  • How do I concatenate two strings in C?

    10 answers



  • How do I concatenate const/literal strings in C?

    17 answers




I want to concatenate "/bin/" and "touch" so that I will have "/bin/touch".



In my program, I have



        char* filePath = malloc((strlen("/bin/") + strlen(rv[0]))* sizeof(char));
filePath = strcat("/bin/",rv[0])


First of all, rv[0] contains a string, "touch". I allocate 10 bytes in memory by using malloc function, and filePath will be the pointer to those 10 bytes of memory. Because, the total length of the string concatenated ("/bin/touch") will be 10.



The program executes normally until the second line which gives me a segmentation fault. Did I make any mistake on the strcat function?










share|improve this question













marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You're appending to a string literal. strcat does not work like that. You'd strcpy to filePath and then strcat
    – Antti Haapala
    Nov 11 at 10:04






  • 1




    Also you're not allocating space for the null terminator (+ 1), and sizeof(char) is 1 by definition.
    – Antti Haapala
    Nov 11 at 10:05












  • When copying strings, strcat() copies the input string up to and including the zero terminator. Your malloc() size does not allow for the presence of that zero terminator, so is one character shorter than needed. strcat() therefore writes past the allocated end of filePath - which gives undefined behaviour. A call of strcat() should not even compile when given two string literals. If it is, then either your code is forcing it in some way you haven't shown, or your compiler/library is flawed. I'll bet on the former.
    – Peter
    Nov 11 at 10:07















up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • How do I concatenate two strings in C?

    10 answers



  • How do I concatenate const/literal strings in C?

    17 answers




I want to concatenate "/bin/" and "touch" so that I will have "/bin/touch".



In my program, I have



        char* filePath = malloc((strlen("/bin/") + strlen(rv[0]))* sizeof(char));
filePath = strcat("/bin/",rv[0])


First of all, rv[0] contains a string, "touch". I allocate 10 bytes in memory by using malloc function, and filePath will be the pointer to those 10 bytes of memory. Because, the total length of the string concatenated ("/bin/touch") will be 10.



The program executes normally until the second line which gives me a segmentation fault. Did I make any mistake on the strcat function?










share|improve this question














This question already has an answer here:




  • How do I concatenate two strings in C?

    10 answers



  • How do I concatenate const/literal strings in C?

    17 answers




I want to concatenate "/bin/" and "touch" so that I will have "/bin/touch".



In my program, I have



        char* filePath = malloc((strlen("/bin/") + strlen(rv[0]))* sizeof(char));
filePath = strcat("/bin/",rv[0])


First of all, rv[0] contains a string, "touch". I allocate 10 bytes in memory by using malloc function, and filePath will be the pointer to those 10 bytes of memory. Because, the total length of the string concatenated ("/bin/touch") will be 10.



The program executes normally until the second line which gives me a segmentation fault. Did I make any mistake on the strcat function?





This question already has an answer here:




  • How do I concatenate two strings in C?

    10 answers



  • How do I concatenate const/literal strings in C?

    17 answers








c segmentation-fault dynamic-memory-allocation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 9:56









ronn Lee

876




876




marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You're appending to a string literal. strcat does not work like that. You'd strcpy to filePath and then strcat
    – Antti Haapala
    Nov 11 at 10:04






  • 1




    Also you're not allocating space for the null terminator (+ 1), and sizeof(char) is 1 by definition.
    – Antti Haapala
    Nov 11 at 10:05












  • When copying strings, strcat() copies the input string up to and including the zero terminator. Your malloc() size does not allow for the presence of that zero terminator, so is one character shorter than needed. strcat() therefore writes past the allocated end of filePath - which gives undefined behaviour. A call of strcat() should not even compile when given two string literals. If it is, then either your code is forcing it in some way you haven't shown, or your compiler/library is flawed. I'll bet on the former.
    – Peter
    Nov 11 at 10:07




















  • You're appending to a string literal. strcat does not work like that. You'd strcpy to filePath and then strcat
    – Antti Haapala
    Nov 11 at 10:04






  • 1




    Also you're not allocating space for the null terminator (+ 1), and sizeof(char) is 1 by definition.
    – Antti Haapala
    Nov 11 at 10:05












  • When copying strings, strcat() copies the input string up to and including the zero terminator. Your malloc() size does not allow for the presence of that zero terminator, so is one character shorter than needed. strcat() therefore writes past the allocated end of filePath - which gives undefined behaviour. A call of strcat() should not even compile when given two string literals. If it is, then either your code is forcing it in some way you haven't shown, or your compiler/library is flawed. I'll bet on the former.
    – Peter
    Nov 11 at 10:07


















You're appending to a string literal. strcat does not work like that. You'd strcpy to filePath and then strcat
– Antti Haapala
Nov 11 at 10:04




You're appending to a string literal. strcat does not work like that. You'd strcpy to filePath and then strcat
– Antti Haapala
Nov 11 at 10:04




1




1




Also you're not allocating space for the null terminator (+ 1), and sizeof(char) is 1 by definition.
– Antti Haapala
Nov 11 at 10:05






Also you're not allocating space for the null terminator (+ 1), and sizeof(char) is 1 by definition.
– Antti Haapala
Nov 11 at 10:05














When copying strings, strcat() copies the input string up to and including the zero terminator. Your malloc() size does not allow for the presence of that zero terminator, so is one character shorter than needed. strcat() therefore writes past the allocated end of filePath - which gives undefined behaviour. A call of strcat() should not even compile when given two string literals. If it is, then either your code is forcing it in some way you haven't shown, or your compiler/library is flawed. I'll bet on the former.
– Peter
Nov 11 at 10:07






When copying strings, strcat() copies the input string up to and including the zero terminator. Your malloc() size does not allow for the presence of that zero terminator, so is one character shorter than needed. strcat() therefore writes past the allocated end of filePath - which gives undefined behaviour. A call of strcat() should not even compile when given two string literals. If it is, then either your code is forcing it in some way you haven't shown, or your compiler/library is flawed. I'll bet on the former.
– Peter
Nov 11 at 10:07














1 Answer
1






active

oldest

votes

















up vote
0
down vote













Take a look at the reference for how to use strcat:



char *strcat( char *dest, const char *src );



Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest.




The first parameter must thus be a pointer to a memory location large enough to hold the bytes of both the C string already there and the C string pointed to by src.



You call strcat("/bin/",rv[0]) and thus try to write into memory where the string literal "/bin/" is stored .. which is normally in readonly memory, thus You get a segmentation fault.



You need to copy "/bin/" first into the allocated memory pointed to by filePath and then append rv[0] there.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Take a look at the reference for how to use strcat:



    char *strcat( char *dest, const char *src );



    Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest.




    The first parameter must thus be a pointer to a memory location large enough to hold the bytes of both the C string already there and the C string pointed to by src.



    You call strcat("/bin/",rv[0]) and thus try to write into memory where the string literal "/bin/" is stored .. which is normally in readonly memory, thus You get a segmentation fault.



    You need to copy "/bin/" first into the allocated memory pointed to by filePath and then append rv[0] there.






    share|improve this answer

























      up vote
      0
      down vote













      Take a look at the reference for how to use strcat:



      char *strcat( char *dest, const char *src );



      Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest.




      The first parameter must thus be a pointer to a memory location large enough to hold the bytes of both the C string already there and the C string pointed to by src.



      You call strcat("/bin/",rv[0]) and thus try to write into memory where the string literal "/bin/" is stored .. which is normally in readonly memory, thus You get a segmentation fault.



      You need to copy "/bin/" first into the allocated memory pointed to by filePath and then append rv[0] there.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Take a look at the reference for how to use strcat:



        char *strcat( char *dest, const char *src );



        Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest.




        The first parameter must thus be a pointer to a memory location large enough to hold the bytes of both the C string already there and the C string pointed to by src.



        You call strcat("/bin/",rv[0]) and thus try to write into memory where the string literal "/bin/" is stored .. which is normally in readonly memory, thus You get a segmentation fault.



        You need to copy "/bin/" first into the allocated memory pointed to by filePath and then append rv[0] there.






        share|improve this answer












        Take a look at the reference for how to use strcat:



        char *strcat( char *dest, const char *src );



        Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest.




        The first parameter must thus be a pointer to a memory location large enough to hold the bytes of both the C string already there and the C string pointed to by src.



        You call strcat("/bin/",rv[0]) and thus try to write into memory where the string literal "/bin/" is stored .. which is normally in readonly memory, thus You get a segmentation fault.



        You need to copy "/bin/" first into the allocated memory pointed to by filePath and then append rv[0] there.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 10:06









        Daniel Jour

        11.8k12046




        11.8k12046















            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine