Strtok function (C++) : Problem with a constructor that crashes

Code with C | Programming: Projects & Source Codes Forums C and C++ Strtok function (C++) : Problem with a constructor that crashes

This topic contains 4 replies, has 1 voice, and was last updated by  SapnaVishwas 2 years, 5 months ago.

  • Author
    Posts
  • #11039

    HeatherKort
    Member

    This is the code:

    I give a string something like “12/12/2011″ as parameter and the constructor splits it but the rule string = strtok(date,”/”); gives a infinite loop.

    I’ve seen on
    http://www.cplusplus.com/reference/clibrary/cstring/strtok/
    for more information but I don’t get it why it doesn’t work.

  • #11040

    ShikhaTan
    Member

    I tried your code in an easy program:

    And i do not see any problem.
    The piece of code looks just fine to me.

    (What variables are member of the private part of your class, what is the point of making a constructor when you do not use the information made in a the function.)

    You should show more code... give an example when it crashes... get into the loop.

  • #11042

    Abhey
    Member

    Try

    Tested example

    And working for me

  • #11045

    SapnaVishwas
    Member

    Dear friend,
    i tried your code in visual studio 2010 express.It passses the build test but gives this warning.The warning is self explanatory.

    The said code is deprecated and can produce dangerous results;which in your case seems to be crashing.

    Hence follow the solution given with the warning itself.

  • #11046

    SapnaVishwas
    Member

    strtok() will modify the original string by replacing a deliminator with ‘\0’.

    The code above will give you an access violation because your compiler will make dateString immutable and “1/1/1” will be treated as a constant which will also be immutable.

    The code above will work because dateString can be modified as long as it’s within its boundaries. You can also make a copy of the original string and use the copy with strtok().

You must be logged in to reply to this topic.