run time Error :(

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gizmo

    run time Error :(

    hello all have been trying to write a Mid() function a bit like the one in
    vb. i come to compile it and there are no errors however when i run it an
    error accours and it says the program has to close. The odd thing is the
    error accours after the mid function has done its job. but if i take out the
    mid function the error does not accour.

    i have found that if i do

    cout <<mid(newString .GetString(),iP osition,3);

    the erorr will occur. However if i do

    cout <<strcpy(new char,mid(newStr ing.GetString() ,iPosition,3));

    the erorr does not occur.

    If i debug it . . im given the error message "Unhandled exception in
    Main.exe: 0xc0000005: Access Violation." and it takes me to the part that
    its says the error occurs.

    The following If statment is where it says it is going wrong (i did not
    write this code it must be part of the code that comes with c++)

    if (!CheckBytes(pb Data(pHead) + pHead->nDataSize,
    _bNoMansLandFil l, nNoMansLandSize ))
    _RPT3(_CRT_ERRO R, "DAMAGE: after %hs block (#%d) at
    0x%08X.\n",
    szBlockUseName[_BLOCK_TYPE(pHe ad->nBlockUse)],
    pHead->lRequest,
    (BYTE *) pbData(pHead));


    The following code is my mid function code that i wrote (it all compiles
    with out any errors).


    char* mid(char* cStringBeingSen tIn, int iPositionToStar tAt, int iLength)
    {
    iLength = iLength - 1;

    char *cNewStringBein gLookedAt = (char*)malloc(i Length+1+1);
    int iCharPositionTo BeStoredAt = 0;


    if ((iPositionToSt artAt + iLength+1) > (int)strlen(cSt ringBeingSentIn ) )
    {
    return new char = "ERROR";
    }

    if (cStringBeingSe ntIn == NULL)
    {
    return new char = "ERROR";
    }

    if ( (iPositionToSta rtAt <0) || (iLength < 0) )
    {
    return new char = "ERROR";
    }

    if (strlen(cString BeingSentIn) == 0)
    {
    return new char = "ERROR";
    }

    for (int iCount = iPositionToStar tAt; iCount <= iPositionToStar tAt +
    iLength; iCount++)
    {
    cNewStringBeing LookedAt[iCharPositionTo BeStoredAt] =
    cStringBeingSen tIn[iCount];
    iCharPositionTo BeStoredAt++;
    }

    cNewStringBeing LookedAt[iLength+1] = '\0';

    return strcpy(new char,cNewString BeingLookedAt) ;
    }


    Any help would be grate if any one can figure out what going on. Thanks in
    adv.

    Gizmo.








  • Aggro

    #2
    Re: run time Error :(

    Gizmo wrote:
    [color=blue]
    > hello all have been trying to write a Mid() function a bit like the one in
    > vb.[/color]

    First question: Why? What's wrong with std::string function substr()
    which does exactly the same.

    string substr (size_type pos, size_type n);
    Returns a substring of the current string, starting at position pos and
    of length n:

    Example:
    ------------------------------------------------------
    #include <iostream>
    #include <string>
    int main()
    {
    std::string str18 = "abcdefghi" ;
    std::string str19 = str18.substr(6, 2);
    std::cout << str19 << std::endl; // Prints out: "gh"
    return 0;
    }
    ------------------------------------------------------

    Comment

    • John Harrison

      #3
      Re: run time Error :(


      "Gizmo" <scottamillard@ hotmail.com> wrote in message
      news:bivsb4$2r6 $1@news6.svr.po l.co.uk...[color=blue]
      > hello all have been trying to write a Mid() function a bit like the one in
      > vb. i come to compile it and there are no errors however when i run it an
      > error accours and it says the program has to close. The odd thing is the
      > error accours after the mid function has done its job. but if i take out[/color]
      the[color=blue]
      > mid function the error does not accour.
      >[/color]
      [snip][color=blue]
      >
      > Any help would be grate if any one can figure out what going on. Thanks in
      > adv.
      >
      > Gizmo.
      >[/color]

      There quite a lot wrong with the posted code. I think you need to pick up
      your favourite C++ book and have a look at dynamic memory allocation and
      string handling.
      [color=blue]
      > return new char = "ERROR";[/color]
      [color=blue]
      > return strcpy(new char,cNewString BeingLookedAt) ;[/color]

      These two lines are completely bogus. I'd like to offer some advice but I
      can't really see what you think you are doing here. Here's how you should
      allocate memory for a string

      char* aString = new char[aLength + 1];

      Hope this helps.

      john


      Comment

      • Kevin Goodsell

        #4
        Re: run time Error :(

        Gizmo wrote:
        [color=blue]
        > hello all have been trying to write a Mid() function a bit like the one in
        > vb.[/color]

        That doesn't help me. What is Mid() supposed to do?
        [color=blue]
        > i come to compile it and there are no errors however when i run it an
        > error accours and it says the program has to close. The odd thing is the
        > error accours after the mid function has done its job. but if i take out the
        > mid function the error does not accour.
        >
        > i have found that if i do
        >
        > cout <<mid(newString .GetString(),iP osition,3);[/color]

        What is newString? It looks like some non-standard string class. Why are
        you not using the standard class std::string?
        [color=blue]
        >
        > the erorr will occur. However if i do
        >
        > cout <<strcpy(new char,mid(newStr ing.GetString() ,iPosition,3));[/color]

        This is doomed to fail. If mid returns a pointer to a string longer than
        0 characters you will write over memory you don't own. 'new char' gives
        you *one* char. Just one. Try to copy more than that and your program
        will go down in flames, or worse.

        Part of the problem here is that you are using char pointers as C-style
        strings. You would be much better off using std::string.
        [color=blue]
        >
        > the erorr does not occur.
        >
        > If i debug it . . im given the error message "Unhandled exception in
        > Main.exe: 0xc0000005: Access Violation." and it takes me to the part that
        > its says the error occurs.
        >
        > The following If statment is where it says it is going wrong (i did not
        > write this code it must be part of the code that comes with c++)[/color]

        'C++' does not come with code. This is part of a particular C++
        implementation. It looks like MS Visual C++ to me.

        The error indicates that you've written to memory you don't own. The
        code below indicates that you've written past the end (or before the
        beginning) of a dynamically allocated chunk of memory. Incidentally,
        this is exactly what is likely to happen with your 'strcpy(new char' above.
        [color=blue]
        >
        > if (!CheckBytes(pb Data(pHead) + pHead->nDataSize,
        > _bNoMansLandFil l, nNoMansLandSize ))
        > _RPT3(_CRT_ERRO R, "DAMAGE: after %hs block (#%d) at
        > 0x%08X.\n",
        > szBlockUseName[_BLOCK_TYPE(pHe ad->nBlockUse)],
        > pHead->lRequest,
        > (BYTE *) pbData(pHead));
        >
        >
        > The following code is my mid function code that i wrote (it all compiles
        > with out any errors).[/color]

        That certainly does not mean it's correct.
        [color=blue]
        >
        >
        > char* mid(char* cStringBeingSen tIn, int iPositionToStar tAt, int iLength)
        > {
        > iLength = iLength - 1;
        >
        > char *cNewStringBein gLookedAt = (char*)malloc(i Length+1+1);[/color]

        Don't use malloc unless you have a very good reason. You don't seem to
        have any reason in this case. Use 'new' instead.
        [color=blue]
        > int iCharPositionTo BeStoredAt = 0;
        >
        >
        > if ((iPositionToSt artAt + iLength+1) > (int)strlen(cSt ringBeingSentIn ) )[/color]

        This cast is a bad idea. First, don't use C-style casts in C++. Use a
        C++ cast operator instead (static_cast in this case). Second, converting
        size_t to int may have unexpected results. You'd do better to convert
        the left side to size_t, or to write code that doesn't require casts.
        [color=blue]
        > {
        > return new char = "ERROR";[/color]

        This is so wrong I hardly know where to start. 'new char' returns a
        pointer to one, single, solitary character. That single character cannot
        store an entire string, only a single character. Even if you allocated
        enough space for your entire "ERROR" string, the assignment is
        completely wrong. All you are doing is allocating a char, then
        immediately assigning the address of a string literal to the temporary
        char * that was pointing to your allocated char (which causes a memory
        leak since you no longer have a pointer to your allocated char), then
        returning that address. The effect is the same as this:

        new char;
        return "ERROR";

        Any attempt outside this function to modify the string that was returned
        or to delete it will break your program. You can't modify a string
        literal, so it's a very bad idea to ever create a (non-const) pointer to
        one. In fact, conversion from string literal to (non-const) char* is a
        deprecated language feature.

        Besides that, if you return here you leak the memory pointed to by
        cNewStringBeing LookedAt.
        [color=blue]
        > }
        >
        > if (cStringBeingSe ntIn == NULL)
        > {
        > return new char = "ERROR";[/color]

        Same problem.
        [color=blue]
        > }
        >
        > if ( (iPositionToSta rtAt <0) || (iLength < 0) )
        > {
        > return new char = "ERROR";[/color]

        And again.
        [color=blue]
        > }
        >
        > if (strlen(cString BeingSentIn) == 0)
        > {
        > return new char = "ERROR";[/color]

        And again.
        [color=blue]
        > }
        >
        > for (int iCount = iPositionToStar tAt; iCount <= iPositionToStar tAt +
        > iLength; iCount++)
        > {
        > cNewStringBeing LookedAt[iCharPositionTo BeStoredAt] =
        > cStringBeingSen tIn[iCount];
        > iCharPositionTo BeStoredAt++;
        > }
        >
        > cNewStringBeing LookedAt[iLength+1] = '\0';[/color]

        I don't understand what any of this is intended to do, but it looks very
        dangerous.
        [color=blue]
        >
        > return strcpy(new char,cNewString BeingLookedAt) ;[/color]

        This is extremely broken. Again, you are returning without freeing the
        memory pointed to by cNewStringBeing LookedAt. But even worse is that you
        are again trying to copy a string into a single char.
        [color=blue]
        > }
        >
        >
        > Any help would be grate if any one can figure out what going on. Thanks in
        > adv.[/color]

        What's going on is that you seem to have very little understanding of
        pointers, strings, or dynamic memory. You need to back up and learn
        these things before you attempt to use them in anything non-trivial.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        Working...