CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: strcat problem

  1. #1
    Join Date
    Aug 2004
    Posts
    15

    strcat problem

    First of all, somewhat newbie to C++. I know the basics, just not all the "fine detail". My question...

    I get an error - "The instruction at "mem location" referenced memory at "mem location". The memory could not be "read".

    I have searched the archives, and couldn't find an answer to why I am getting this. Here is a snippet of my code. The problem seems to be with the strcat function. I have debugged it to here.

    int buno = 0;
    char filepath[64] = "C:\\some path here\\";
    char filesuffix[32] ="_flt_test.csv";
    char bunotostring[5] = "123456";

    // Convert buno number (int) to string
    sprintf(bunotostring, "%d", buno);

    char *fp = strcat(filepath,strcat(filesuffix,bunotostring));

    for some reason, when I try to use strcat, I get this error. Any thoughts, and sorry for the newbie post....

  2. #2
    Join Date
    Aug 2001
    Location
    Indiana
    Posts
    117
    It may be because you are only allocating 5 bytes for a string that takes up 7, in the statement char bunotostring[5] = "123456"; There is also a null character at the end of the string that is not shown, but tells you where the string ends. I would bet that strcat is not seeing the null character at the end of bunotostring and going off into the weeds
    "If it seems that I have seen further than others, it is because I stand on the shoulders of giants" --Newton

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    First, the code you posted is just C. It doesn't have anything C++ specific (except for the declaration of fp).

    Second, when I run your code it dosn't give me any error and it prints:
    C:\some path here\_flt_test.csv0
    which is the corect result of your code.

    The only mistake is here:
    Code:
    char bunotostring[5] = "123456";
    You cannot initialize with 7 chars a buffer of only 5 elements.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Aug 2004
    Posts
    15

    Sorry...

    My bad... that line should have been ... ...

    char bunotostring[6];

    But I still get the error. Again, sorry for posting in the wrong discussion group. I am using VisualC++, and some of my code is in C++. Trying to make the switch!
    Last edited by hebes_99; August 19th, 2004 at 08:50 AM. Reason: more info

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    Wrong again. You forget about the null terminating character. You're array is actually {'1','2','3','4','5','6','\0'}, so it has 7 chars, not 6. So it should be
    Code:
    char bunotostring[7] = "123456";
    Of course that goes for dinamically memory allocation. If your string has N chars, you need N+1 chars.
    Code:
    int N = 6;
    char *p = new char[N+1];
    ...
    delete [] p;
    What compiler are you using? Because VisualC++ tells you stuff like that, and don't need to look in the dark for such mistakes. Anyway pay attention to the null terminating char.
    Last edited by cilu; August 19th, 2004 at 08:54 AM.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Aug 2004
    Posts
    15

    compiler

    I am using the built in Visual C++ compiler. The error I get is a runtime error. The code compiles fine, and my code works to this point. Once it hits the strcat function, it crashes. It seems straight forward, but I know I am missing something easy. For clearity, let me repost the correct code snippet....

    int buno;
    char filepath[64] = "C:\\some path\\";
    char filesuffix[32] ="_flt_test.csv";
    char bunotostring[6];

    // Convert buno number (int) to string
    sprintf(bunotostring, "%d", buno);

    char *fp = strcat(filepath,strcat(filesuffix,bunotostring));

    Note: buno is a number that is 6 digits in size. I am sure there are much easier ways to do this... just new!

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    You have to initialize your variables, and since buno is not initialized of course it crashes. When you don't do it it starts with an undefined value, for instance: -858993460. When you run this
    Code:
    sprintf(bunotostring, "%d", buno);
    It puts more than 6 chars in that buffer, overriding your null terminating char. So your bunotostring would have another lenght, like 21 in the below example.

    0012FF14 2D 38 35 38 -858
    0012FF18 39 39 33 34 9934
    0012FF1C 36 30 00 74 60.t
    0012FF20 5F 74 65 73 _tes
    0012FF24 74 2E 63 73 t.cs
    0012FF28 76 00 00 00 v...
    0012FF2C 00 00 00 00 ....

    When you try to concatenate this to filesuffix which has only 32 chars it chrashes.

    So just initialize your variables before using them.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Aug 2004
    Posts
    15
    a value for buno is passed in from another part of the prorgam. I posted it as an int for clarity. In actuality, it is passed in.

    float myfunction(int buno)
    {

    char filepath[64] = "C:\\some path\\";
    char filesuffix[32] ="_flt_test.csv";
    char bunotostring[6];

    // Convert buno number (int) to string
    sprintf(bunotostring, "%d", buno);

    char *fp = strcat(filepath,strcat(filesuffix,bunotostring));

    ...
    //more code....
    }

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    Quote Originally Posted by hebes_99
    a value for buno is passed in from another part of the prorgam. I posted it as an int for clarity. In actuality, it is passed in.

    float myfunction(int buno)
    {

    char filepath[64] = "C:\\some path\\";
    char filesuffix[32] ="_flt_test.csv";
    char bunotostring[6];

    // Convert buno number (int) to string
    sprintf(bunotostring, "%d", buno);

    char *fp = strcat(filepath,strcat(filesuffix,bunotostring));

    ...
    //more code....
    }
    Your code stays WRONG!
    Why don't you want to correct it as cilu and stickben already suggested?
    Keep in mind that the length of int may be up to 11 characters (for negativ decimal number) in Win-32.
    In this case you MUST declare your char bunotostring array with the size at least 12:
    Code:
    char bunotostring[12];
    
    // Convert buno number (int) to string
    sprintf(bunotostring, "%d", buno);
    .............

  10. #10
    Join Date
    Aug 2004
    Posts
    15

    Unhappy

    tried that, didn't work. And sorry, I am new to the idiosyncrasies of C++. I have played with strcat enough to see that the error only occurs when filepath is the initial argument. ie:

    strcat(filepath, filesuffix)
    or
    strcat(filepath, bunotostring)
    etc..etc..

    other combinations work, so I am guessing this is some weird thing with my compiler....

    again, sorry for being a pain..

  11. #11
    Join Date
    Aug 2004
    Posts
    15

    Smile

    Well... got it to work, but don't really know why...

    If I changed the size of filepath to char filepath[128], then it compiles. Strange, since strlen of filepath is only 57 (in my code).... oh well.. it works...

    Thanks to all for your help!!!
    Last edited by hebes_99; August 19th, 2004 at 10:30 AM.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    Quote Originally Posted by hebes_99
    tried that, didn't work. And sorry, I am new to the idiosyncrasies of C++. I have played with strcat enough to see that the error only occurs when filepath is the initial argument. ie:

    strcat(filepath, filesuffix)
    or
    strcat(filepath, bunotostring)
    etc..etc..

    other combinations work, so I am guessing this is some weird thing with my compiler....

    again, sorry for being a pain..
    1. Once again: please, read carefully the ryplies to your question! As cilu already wrote, there is no any c++ specific at your code snippet posted to the forum!
    2. No need to guess there could be "some weird thing with my compiler...." if your compiler is not from youself!
    3. Please, debug your code and see/control all your variables (their values/string length,...).
    4. Note, that max length of the file path in Windows is MAX_PATH (260 characters), it is some more than 64!

  13. #13
    Join Date
    Aug 2004
    Posts
    15
    wow...sorry...

    I guess this isn't a forum for new programmers. I am sorry for violating the forum rules.
    Last edited by hebes_99; August 19th, 2004 at 10:48 AM.

  14. #14
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58
    Quote Originally Posted by hebes_99
    wow...sorry...

    I guess this isn't a forum for new programmers. I am sorry for violating the forum rules.
    This forum has many new programmers, and NO you are NOT violating the forum rules.
    Quote Originally Posted by VictorN
    1. Once again: please, read carefully the ryplies to your question! As cilu already wrote, there is no any c++ specific at your code snippet
    VictorN is wrong is saying there is no specific C++ code as all the code you presented was valid C++ code, perhaps it should have been posted in the C++ (Non Visual C++ Issues) forum.

    What the replies to your question were trying to convey was that the line:
    Code:
    sprintf(bunotostring, "%d", buno);
    If you want to limit the maximum size of the string, then changed the above code to
    Code:
    sprintf(bunotostring, "%.5d", buno);
    The period before the 5 means the maximum size will be 5 (the 6th character is for the null at the end of the string.
    then change the strcat to strncat as follows
    Code:
    char *fp = strcat(filepath,strcat(filesuffix,bunotostring, 5), 32);
    or you could combine them into one call to sprintf as follows
    Code:
    sprintf(bunotostring, "C:\\some path\\%d_flt_test.csv", buno);
    Don't get discouraged, C++ is a difficult language to learn and it is easy to get into trouble with this language.

    For new programmers, if possible, it is better to start programming in another language, for programs that will run on Windows PCs, C# would be a better starting language. You could never have the above problem in C#.
    Last edited by CJ1; August 19th, 2004 at 12:23 PM.

  15. #15
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Quote Originally Posted by CJ1
    If you want to limit the maximum size of the string, then changed the above code to
    Code:
    sprintf(bunotostring, "%.5d", buno);
    The period before the 5 means the maximum size will be 5 (the 6th character is for the null at the end of the string.
    Well, this is just wrong! The "%.5d" does NOT limit the output - go check your manuals. It is responsibility of the programmer to provide enough room in the destination buffer (that is why printf's are not safe).

    Also, even if this code compiles in C++ compiler, it still has nothing to do with C++. But I do think that it was OK to post the question here.

    I am surprised that nobody pointed out an obvious OP's misunderstanding:
    Code:
    char *fp = strcat(filepath,strcat(filesuffix,bunotostring));
    strcat appends (concatenates) second string to the first string, returning pointer to the target (first string). It does NOT allocate any new memory for char* fp! In that example, fp is equal to filepath. And filepath buffer is not capable to accept all other strings at its end.

    Here comes C++, that provides string classes that take care of the memory management (like std::string and MFC's CString), so you no longer need to guess if 64 bytes will be enough for the file path (you know, it won't).
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured