Thread: two strcpy-ies crashes my program

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Question two strcpy-ies crashes my program

    Hi.

    I have a strange problem with the following code. It compiles without errors, but when i start it, it crashes when the second strcpy-command should be done. When I delete one strcpy-command (no matter which one) it works without problems. Why can't I use two strcpys in one code here?

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
        char *stra[10];
        char *strb[10];
        
        strcpy(*stra, "hallo");
        strcpy(*strb, "hallo");
        return(0);
    }

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Jakob
    When I delete one strcpy-command (no matter which one) it works without problems.
    Does it really works? I don't thinks so. Anyway maybe you need this:
    Code:
    #include <iostream>
    #include <cstring>
    
    
    using namespace std;
    
    int main()
    {
        char stra[10];
        char strb[10];
        
        strcpy(stra, "hallo");
        strcpy(strb, "hallo");
        
        return(0);
    }
    With this line
    Code:
    char *stra[10];
    you defined array of 10 pointers to type char.
    If your intetnion was to do this then you must first allocate memory otherwise you'll get a seg. fault
    So correct version would be:

    Code:
    char *stra[10];
    stra[0] = new char[10]; //allocate enough memory to store 10 character
     strcpy(*stra, "hallo");
    //or  strcpy(stra[0], "hallo");
    when using *stra that means you use stra[0] in this case.
    I repeat I don't know if your intetion was to define array of pointers to type char in the first place. Did you want to define array of C strings?
    Last edited by Micko; 03-28-2005 at 07:13 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why can't I use two strcpys in one code here?
    They're both wrong, but it takes a while for programs to crash usually.

    Code can simultaneously be tolerant and sensitive of programming errors, so it's not sufficient to say that it "compiles without warnings" or "runs without errors" to determine if it is bug free or not.

    > strcpy(*stra, "hallo");
    Which is strcpy(stra[0], "hallo");
    But this doesn't point anywhere, so you just spray 6 bytes of data over some random memory location. Depending on luck, this may or may not crash your program.
    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
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    Oops, thank you. I mixed up this pointer-stuff. It works without the *s. Interesting how long one can work on an easy problem like this...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funcion runs 14 times, then program crashes
    By happyclown in forum C Programming
    Replies: 9
    Last Post: 03-03-2009, 11:58 PM
  2. Replies: 3
    Last Post: 02-29-2008, 01:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  5. Program crashes...help Needed!!
    By butterfly in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 11:22 AM