Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, Prog-1 works fine. But not Prog-2. It gives core dump. Can you pls help. Prog-1 char str1[]= "To be or not to be"; char str2[6]; strncpy (str2,str1,5); str2[5]='

Thread: strncpy core dump.

'; Printf(XYZ, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    6

    strncpy core dump.


    Hi,
    Prog-1 works fine. But not Prog-2. It gives core dump. Can you pls help.
    Prog-1
    char str1[]= "To be or not to be";
    char str2[6];
    strncpy (str2,str1,5);
    str2[5]='\0';
    Printf(XYZ, "%s(): strncpy str2 = %s \n", __FUNCTION__, str2);

    Output: To be
    Fine.
    Prog-2
    main()
    {
    char scl_ofile[] = "/mnt/ttt.mp4";
    abc(scl_ofile);
    }

    int abc(char * filename)
    {
    char *args1[] = {"abc", "25", "ttt.h264", ""}; //I need this frame work.

    Printf(XYZ, "%s(): strncpy args1[3] = %s \n", __FUNCTION__, args1[3]); //It prints fine.
    strncpy(args1[3], filename, 50); //Looks like core dump is happening here.Changing it to the exact size of 12 also did not work.
    Printf(XYZ, "%s(): strncpy args1[3] = %s \n", __FUNCTION__, args1[3]); //Nothig is printed
    // pqr() //to send as main(argc,argv) type
    }
    Last edited by sdsjohnny; 09-16-2010 at 01:32 AM. Reason: Instead of strncpy, I used strn1 = strn2 like in CPP. It works.

  2. #2
    Just Joined!
    Join Date
    Jul 2010
    Posts
    6
    When I used strn1=strn2 like in CPP, it works fine. But I did not understand, why strncpy won't work line in regular C.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,533
    In this line:
    Code:
    strncpy(args1[3], filename, 50);
    you have a couple of problems that wlil cause a core dump.

    1. args1[3] is the empty terminating member "". It is only 1 char in size (for the NUL byte), yet you are copying up to 50 chars there.
    2. args1[] is an array of char pointers, yet is initialized with string literal values, which, depending upon the system and compiler flags, may not be writeable.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •