Thread: strncat..strange behaviour

  1. #1
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78

    Question strncat..strange behaviour

    Take a look at this piece of code:

    Code:
    int main (int argc, char *argv[])
    {
    
        char *str1 = "Foxbat";
        char *str2 = "Fulcrum";
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);
    
        strncat(str1, "ABC", 2);
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);
    
        return 0;
    }
    and here is the output:
    Code:
    str1 = Foxbat   str2 = Fulcrum
    str1 = FoxbatAB str2 = B
    why is str2 being spoiled?? it is not supposed to contain value B, right? Any ideas?...i tried to declare str2 as const...but same result..

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes its true infact in my case it does not print the second line

    It says unhandled exception writing at some location . But this is strange.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    a) str1 is pointing at a string constant, so you're already relying on luck to be able to modify it at all.
    (edit - the more usual case is as roaan notes, and the OS kills your program with a memory fault)

    b) string constants are only allocated the space they need. You can't arbitrarily extend that without trashing something else.

    You (your specific case have)
    Code:
    Foxbat\0Fulcrum
    ^       ^
    The two ^ indicate where str1 and str2 point.

    You change str1, so it now looks like this
    Code:
    FoxbatAB\0ulcrum
    ^      ^
    Both pointers still point at the same place.
    Only str2 is now just the tail-end of some much shorter string!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm surprised that worked at all without a run-time crash. But you don't have any "extra" letters to play with in your str1 and str2, and so any attempt to add letters to them are doomed to failure.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps it's your compiler, mine core dumps (as expected).

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The code is broke - there ain't any other way to slice it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78

    Question

    Ok, could this be a solution to the problem?

    Code:
        char *str1 = "Foxbat";
        char *str2;
    
        strcpy(str2, "Fulcrum");
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);
    
        strncat(str1, "ABC", 2);
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);
    Now it prints as i want..
    Code:
    str1 = Foxbat   str2 = Fulcrum
    str1 = FoxbatAB str2 = Fulcrum
    any other solutions?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    NO! This is not a solution! What compiler do you use? This should not work one bit.
    First and foremost, read SourceForge.net: Common mistakes and errors - cpwiki
    And secondly, listen to what people say! It's a string literal! It cannot be modified, nor does it have the space for the extra string you're adding!
    And thirdly, read all the links in the thread!

    Solutions?
    Code:
    char str1[20] = "Foxbat";
    char str2[20];
    // The rest as above.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    If what i understood from the wiki page is that having a const before a string literal will prevent any memory fault and will give compilation error if we try to modify a string literal appended with the keyword const. So to prevent abnormal termination of program this should be fine

    [insert]
    Code:
    int main (int argc, char *argv[])
    {
    
        const char *str1 = "Foxbat";
        const char *str2 = "Fulcrum";
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);
    
        strncat(str1, "ABC", 2);
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);
    
        return 0;
    }
    // This will give error at compilation tiem and prevent abnormal termination as what i experienced.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Oh please, it just gets worse and worse!

    str2 isn't even pointing at ANY memory (just some random location).
    Please stop guessing, and learn about some things before trying again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    i got the point..

    so this is the safest method.. this way i just add two elements to the char array..which already contains 6 elements... So, the point is... its better to know how many elements there if u want to do such kind of operations am i right? i read it.

    Code:
        char str1[20] = "Foxbat";
        char str2[20] = "Fulcrum";
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);
    
        strncat(str1, "ABC", 2);
    
        printf("str1 = %s\tstr2 = %s\n", str1, str2);

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't get your question, but either you make sure you have enough space beforehand (figure it out!), or you expand your storage as necessary using dynamic memory (malloc/realloc/free).
    And this is not the safest method, it's the ONLY method (unless you want to use malloc/free). Your "methods" don't work at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    hmm...by the way, i am working now on some image processing system..and there i am going to output strings in the image memory... well in other words before i tried to make things work like that:

    char *letter = "";

    and then i used a lot of strcpy() on the letter.

    and then i used a lot of things like *(letter+ SomOffset) inside for loops..

    and i found that strings dont print correctly...

    what i did now is:

    char *letter = (char *)malloc(14*12*sizeof(char));

    and now everything works just fine...

    i could also do this char letter[168]; but in my code i have specific element accessing method...cuz actually my array is 2dimensional... cuz when u have this: char letter[14][12] and then u do strcpy(letter, "something"); it gives warning..


    so...in other words, it now seems to me that using malloc is one of the good ways to solve such issues... ok

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This works with regular arrays, too.
    1D arrays:
    char foo[n];
    foo[n]; // Access element n
    2D arrays:
    char foo[x][y];
    foo[x][y]; // Access element x, y.

    If you use malloc, you must also use free.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange behaviour of Ternary
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2009, 02:17 AM
  2. strange behaviour of sscanf
    By BeBu in forum C Programming
    Replies: 7
    Last Post: 12-17-2004, 10:12 AM
  3. strange behaviour using OpenGL types
    By Hubas in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2003, 04:02 PM
  4. GetClientRect strange behaviour
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 10-02-2002, 02:13 PM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM