problem in code running on msvs2008 c++ while same logic runs fine onturbo c++ 3.0

Discussion in 'C++' started by ashjas, Dec 9, 2007.

  1. ashjas

    ashjas Guest

    Hello,
    I have run this logic on turboc++ 3.0 and it is working fine on it but
    its not running on msvs2008 c++.
    i am not able to assign the value like this *temp=*main,where main and
    temp are char pointers.

    there is some runtime access violation error on msvs..that i am not
    able to work out how to resolve..

    kind attention and feedback would be invaluable for me...

    thanks...the code is as below..





    // C++Test.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include "iostream"

    using namespace std;

    int i;
    int searchsub(char mainstr[],char fndstr[])
    {
    int mainlen=0,sublen=0,flag=-1,index=0;
    for( i=0;mainstr!='\0';i++)
    mainlen++;
    for( i=0;fndstr!='\0';i++)
    sublen++;
    for(i=0;i<mainlen;i++)
    {
    index=i;
    if(mainstr==fndstr[0])
    {
    for(int k=0;k<sublen;k++)
    {
    if(mainstr[i++]!=fndstr[k])
    {
    flag=0;
    break;
    }
    else flag=1;
    }
    if(flag==1)
    break;
    i=index;

    }
    }
    if(flag==1)
    return index;
    else return -1;


    }

    char * strrep(char * main,char* sub,char* rep)
    {
    int sublen=0,mainlen=0,replen=0;
    char* save=main;
    // cout<<rep;
    // cout<<main<<" "<<save;
    char*temp=new char;
    //*temp='k';
    // cout<<"-"<<temp<<"1";
    for( i=0;*rep!='\0';i++,rep++)
    replen++;
    rep=rep-i;
    //for( i=0;*main!='\0';i++,main++)
    // mainlen++;
    //main=main-i;
    int index=searchsub(main,sub);
    for( i=0;*sub!='\0';i++,sub++)
    sublen++;
    //cout<<sublen;
    sub=sub-i;
    //cout<<sub;
    int reststrindex=index+sublen;
    // cout<<reststrindex;
    main=main+reststrindex;
    //cout<<main;
    for(i=0;*main!='\0';i++,temp++,main++)
    *temp=*main;
    *temp='\0';
    main=main-i-reststrindex;
    //cout<<main;
    int templen=0;
    temp=temp-i;
    //cout<<temp;
    for( i=0;*temp!='\0';i++,temp++)
    templen++;
    temp=temp-i;
    //cout<<temp;
    //cout<<main;
    main=main+index;
    //cout<<main;
    for(i=index;i<index+replen;i++)
    {
    *main=*rep;
    main++;
    rep++;
    }
    for(i=0;i<templen;i++)
    {
    *main=*temp;
    main++;
    temp++;
    }
    *main='\0';
    main=save;
    return main;


    }


    int _tmain(int argc, _TCHAR* argv[])
    {
    cout<<strrep("abcdefghij","cde","peternorton");
    return 0;
    }
     
    ashjas, Dec 9, 2007
    #1
    1. Advertising

  2. ashjas

    ashjas Guest

    Re: problem in code running on msvs2008 c++ while same logic runsfine on turbo c++ 3.0

    On Dec 9, 3:05 pm, ashjas <> wrote:
    > Hello,
    > I have run this logic on turboc++ 3.0 and it is working fine on it but
    > its not running on msvs2008 c++.
    > i am not able to assign the value like this *temp=*main,where main and
    > temp are char pointers.
    >
    > there is some runtime access violation error on msvs..that i am not
    > able to work out how to resolve..
    >
    > kind attention and feedback would be invaluable for me...
    >
    > thanks...the code is as below..
    >
    > // C++Test.cpp : Defines the entry point for the console application.
    > //
    >
    > #include "stdafx.h"
    > #include "iostream"
    >
    > using namespace std;
    >
    > int i;
    > int searchsub(char mainstr[],char fndstr[])
    > {
    > int mainlen=0,sublen=0,flag=-1,index=0;
    > for( i=0;mainstr!='\0';i++)
    > mainlen++;
    > for( i=0;fndstr!='\0';i++)
    > sublen++;
    > for(i=0;i<mainlen;i++)
    > {
    > index=i;
    > if(mainstr==fndstr[0])
    > {
    > for(int k=0;k<sublen;k++)
    > {
    > if(mainstr[i++]!=fndstr[k])
    > {
    > flag=0;
    > break;
    > }
    > else flag=1;
    > }
    > if(flag==1)
    > break;
    > i=index;
    >
    > }
    > }
    > if(flag==1)
    > return index;
    > else return -1;
    >
    > }
    >
    > char * strrep(char * main,char* sub,char* rep)
    > {
    > int sublen=0,mainlen=0,replen=0;
    > char* save=main;
    > // cout<<rep;
    > // cout<<main<<" "<<save;
    > char*temp=new char;
    > //*temp='k';
    > // cout<<"-"<<temp<<"1";
    > for( i=0;*rep!='\0';i++,rep++)
    > replen++;
    > rep=rep-i;
    > //for( i=0;*main!='\0';i++,main++)
    > // mainlen++;
    > //main=main-i;
    > int index=searchsub(main,sub);
    > for( i=0;*sub!='\0';i++,sub++)
    > sublen++;
    > //cout<<sublen;
    > sub=sub-i;
    > //cout<<sub;
    > int reststrindex=index+sublen;
    > // cout<<reststrindex;
    > main=main+reststrindex;
    > //cout<<main;
    > for(i=0;*main!='\0';i++,temp++,main++)
    > *temp=*main;
    > *temp='\0';
    > main=main-i-reststrindex;
    > //cout<<main;
    > int templen=0;
    > temp=temp-i;
    > //cout<<temp;
    > for( i=0;*temp!='\0';i++,temp++)
    > templen++;
    > temp=temp-i;
    > //cout<<temp;
    > //cout<<main;
    > main=main+index;
    > //cout<<main;
    > for(i=index;i<index+replen;i++)
    > {
    > *main=*rep;
    > main++;
    > rep++;
    > }
    > for(i=0;i<templen;i++)
    > {
    > *main=*temp;
    > main++;
    > temp++;
    > }
    > *main='\0';
    > main=save;
    > return main;
    >
    > }
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > cout<<strrep("abcdefghij","cde","peternorton");
    > return 0;
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -



    please ignore above post there were some errors in cut /paste..

    take a look here..

    Hello,
    I have run this logic on turboc++ 3.0 and it is working fine on it
    but
    its not running on msvs2008 c++.

    the code does this::
    the fun strrep takes 3 char arrays the i.e main,sub,rep.
    the sub array is to be found out i the main array and if found its
    index is taken on found pos,(this is achieved by the searchsub
    function separately)
    then the sub array is replaced in the main array by the rep array,and
    the rep array can be of different size from the sub array.



    i am not able to assign the value like this *temp=*main,where main
    and
    temp are char pointers.

    there is a runtime access violation error on msvs..that i am not
    able to work out how to resolve..


    kind attention and feedback would be invaluable for me...


    thanks...the code is as below..


    // C++Test.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include "iostream"

    using namespace std;

    int i;
    int searchsub(char mainstr[],char fndstr[])
    {
    int mainlen=0,sublen=0,flag=-1,index=0;
    for( i=0;mainstr!='\0';i++)
    mainlen++;
    for( i=0;fndstr!='\0';i++)
    sublen++;
    for(i=0;i<mainlen;i++)
    {
    index=i;
    if(mainstr==fndstr[0])
    {
    for(int k=0;k<sublen;k++)
    {
    if(mainstr[i++]!=fndstr[k])
    {
    flag=0;
    break;
    }
    else flag=1;
    }
    if(flag==1)
    break;
    i=index;

    }
    }
    if(flag==1)
    return index;
    else return -1;


    }

    char * strrep(char * main,char* sub,char* rep)
    {
    int sublen=0,mainlen=0,replen=0;
    char* save=main;

    char*temp;
    for( i=0;*rep!='\0';i++,rep++)
    replen++;
    rep=rep-i;
    int index=searchsub(main,sub);
    for( i=0;*sub!='\0';i++,sub++)
    sublen++;
    sub=sub-i;
    int reststrindex=index+sublen;
    main=main+reststrindex;
    for(i=0;*main!='\0';i++,temp++,main++)
    *temp=*main;
    *temp='\0';
    main=main-i-reststrindex;
    int templen=0;
    temp=temp-i;
    for( i=0;*temp!='\0';i++,temp++)
    templen++;
    temp=temp-i;
    main=main+index;
    for(i=index;i<index+replen;i++)
    {
    *main=*rep;
    main++;
    rep++;
    }
    for(i=0;i<templen;i++)
    {
    *main=*temp;
    main++;
    temp++;
    }
    *main='\0';
    main=save;
    return main;


    }


    int _tmain(int argc, _TCHAR* argv[])
    {
    cout<<strrep("Abcdefghijk","cde","peternorton");
    return 0;
    }
     
    ashjas, Dec 9, 2007
    #2
    1. Advertising

  3. Re: problem in code running on msvs2008 c++ while same logic runsfine on turbo c++ 3.0

    On 2007-12-09 11:23, ashjas wrote:
    > On Dec 9, 3:05 pm, ashjas <> wrote:


    [please trim quotes a bit]

    > please ignore above post there were some errors in cut /paste..
    >
    > take a look here..
    >
    > Hello,
    > I have run this logic on turboc++ 3.0 and it is working fine on it
    > but
    > its not running on msvs2008 c++.
    >
    > the code does this::
    > the fun strrep takes 3 char arrays the i.e main,sub,rep.
    > the sub array is to be found out i the main array and if found its
    > index is taken on found pos,(this is achieved by the searchsub
    > function separately)
    > then the sub array is replaced in the main array by the rep array,and
    > the rep array can be of different size from the sub array.


    So you replace a sub-string with a longer string but I do not see you
    allocating any memory for the new, larger string, anywhere. Also since
    you are passing string literals as arguments you are very likely to try
    to change constant data (it is an unfortunate inheritance from C that a
    string literal is not a char const*).

    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > cout<<strrep("Abcdefghijk","cde","peternorton");
    > return 0;
    > }


    And the correct name for the main function is main and nothing else. If
    you are not using the arguments you might also leave them out:

    int main()
    {
    ...
    }

    Unless this is homework I would suggest that you use std::string instead
    of char* to handle strings, it will make things much easier. If that is
    not possible you might want to consider using <string.h> which will give
    you a number of useful functions when working with char*. If this *is*
    homework be advised that we do not do your homework for you, put we will
    answer specific questions about C++ and how to use it.

    --
    Erik Wikström
     
    Erik Wikström, Dec 9, 2007
    #3
  4. ashjas

    Bo Persson Guest

    Re: problem in code running on msvs2008 c++ while same logic runs fine on turbo c++ 3.0

    ashjas wrote:
    :: On Dec 9, 3:05 pm, ashjas <> wrote:
    ::
    :: Hello,
    :: I have run this logic on turboc++ 3.0 and it is working fine on it
    :: but
    :: its not running on msvs2008 c++.

    It just seemed to work on Turbo C++. MSVC caugth a real error!

    ::
    :: i am not able to assign the value like this *temp=*main,where main
    :: and
    :: temp are char pointers.
    ::
    :: there is a runtime access violation error on msvs..that i am not
    :: able to work out how to resolve..
    ::
    ::
    :: char * strrep(char * main,char* sub,char* rep)
    :: {
    :: int sublen=0,mainlen=0,replen=0;
    :: char* save=main;
    ::
    :: char*temp;

    Here you create a pointer that doesn't point to anything.

    :: for( i=0;*rep!='\0';i++,rep++)
    :: replen++;
    :: rep=rep-i;
    :: int index=searchsub(main,sub);
    :: for( i=0;*sub!='\0';i++,sub++)
    :: sublen++;
    :: sub=sub-i;
    :: int reststrindex=index+sublen;
    :: main=main+reststrindex;
    :: for(i=0;*main!='\0';i++,temp++,main++)
    :: *temp=*main;


    Here you try to assign a value to the non-existant space. The pointer
    temp is just a pointer, there is not any room for the char value.


    Bo Persson
     
    Bo Persson, Dec 9, 2007
    #4
  5. ashjas

    ashjas Guest

    Re: problem in code running on msvs2008 c++ while same logic runsfine on turbo c++ 3.0

    On Dec 9, 4:30 pm, Erik Wikström <> wrote:
    > On 2007-12-09 11:23, ashjas wrote:
    >
    > > On Dec 9, 3:05 pm, ashjas <> wrote:

    >
    > [please trim quotes a bit]
    >
    >
    >
    >
    >
    > > please ignore above post there were some errors in cut /paste..

    >
    > > take a look here..

    >
    > > Hello,
    > > I have run this logic on turboc++ 3.0 and it is working fine on it
    > > but
    > > its not running on msvs2008 c++.

    >
    > > the code does this::
    > > the fun strrep takes 3 char arrays the i.e main,sub,rep.
    > > the sub array is to be found out i the main array and if found its
    > > index is taken on found pos,(this is achieved by the searchsub
    > > function separately)
    > > then the sub array is replaced in the main array by the rep array,and
    > > the rep array can be of different size from the sub array.

    >
    > So you replace a sub-string with a longer string but I do not see you
    > allocating any memory for the new, larger string, anywhere. Also since
    > you are passing string literals as arguments you are very likely to try
    > to change constant data (it is an unfortunate inheritance from C that a
    > string literal is not a char const*).
    >
    > > int _tmain(int argc, _TCHAR* argv[])
    > > {
    > > cout<<strrep("Abcdefghijk","cde","peternorton");
    > > return 0;
    > > }

    >
    > And the correct name for the main function is main and nothing else. If
    > you are not using the arguments you might also leave them out:
    >
    > int main()
    > {
    > ...
    > }
    >
    > Unless this is homework I would suggest that you use std::string instead
    > of char* to handle strings, it will make things much easier. If that is
    > not possible you might want to consider using <string.h> which will give
    > you a number of useful functions when working with char*. If this *is*
    > homework be advised that we do not do your homework for you, put we will
    > answer specific questions about C++ and how to use it.
    >
    > --
    > Erik Wikström- Hide quoted text -
    >
    > - Show quoted text -


    thanks for replying erik.

    firstly even if this would have been my homework.. i would not have
    asked for a complete soln.
    what i am interested in this discussion is the point where i am going
    wrong nothing else.
    yes i know that im not allocating any new memory space for new main
    char array but what struck me is that why msvs caught this as error
    and the turbo c++ didnt.

    secondly i dont want to use any inbuilt functions.

    so in short allocating new memory space for larger main string and the
    temp string would solve my problems?

    thanks.
     
    ashjas, Dec 9, 2007
    #5
  6. ashjas

    ashjas Guest

    Re: problem in code running on msvs2008 c++ while same logic runsfine on turbo c++ 3.0

    On Dec 9, 4:35 pm, "Bo Persson" <> wrote:
    > ashjas wrote:
    >
    > :: On Dec 9, 3:05 pm, ashjas <> wrote:
    > ::
    > :: Hello,
    > :: I have run this logic on turboc++ 3.0 and it is working fine on it
    > :: but
    > :: its not running on msvs2008 c++.
    >
    > It just seemed to work on Turbo C++. MSVC caugth a real error!
    >
    > ::
    > :: i am not able to assign the value like this *temp=*main,where main
    > :: and
    > :: temp are char pointers.
    > ::
    > :: there is a runtime access violation error on msvs..that i am not
    > :: able to work out how to resolve..
    > ::
    > ::
    > :: char * strrep(char * main,char* sub,char* rep)
    > :: {
    > :: int sublen=0,mainlen=0,replen=0;
    > :: char* save=main;
    > ::
    > :: char*temp;
    >
    > Here you create a pointer that doesn't point to anything.
    >
    > :: for( i=0;*rep!='\0';i++,rep++)
    > :: replen++;
    > :: rep=rep-i;
    > :: int index=searchsub(main,sub);
    > :: for( i=0;*sub!='\0';i++,sub++)
    > :: sublen++;
    > :: sub=sub-i;
    > :: int reststrindex=index+sublen;
    > :: main=main+reststrindex;
    > :: for(i=0;*main!='\0';i++,temp++,main++)
    > :: *temp=*main;
    >
    > Here you try to assign a value to the non-existant space. The pointer
    > temp is just a pointer, there is not any room for the char value.
    >
    > Bo Persson


    so problem should be that i should use the new operator to dynamically
    allocate the mem space for temp n main char pointers..as i have
    understood from this discussion is it?

    and also one important thing that its good time to completely switch
    over to msvs from turbo c++. :)

    thanks.
     
    ashjas, Dec 9, 2007
    #6
  7. Re: problem in code running on msvs2008 c++ while same logic runsfine on turbo c++ 3.0

    On 2007-12-09 16:29, ashjas wrote:
    > On Dec 9, 4:30 pm, Erik Wikström <> wrote:
    >> On 2007-12-09 11:23, ashjas wrote:
    >>
    >> > On Dec 9, 3:05 pm, ashjas <> wrote:

    >>
    >> [please trim quotes a bit]
    >>
    >>
    >>
    >>
    >>
    >> > please ignore above post there were some errors in cut /paste..

    >>
    >> > take a look here..

    >>
    >> > Hello,
    >> > I have run this logic on turboc++ 3.0 and it is working fine on it
    >> > but
    >> > its not running on msvs2008 c++.

    >>
    >> > the code does this::
    >> > the fun strrep takes 3 char arrays the i.e main,sub,rep.
    >> > the sub array is to be found out i the main array and if found its
    >> > index is taken on found pos,(this is achieved by the searchsub
    >> > function separately)
    >> > then the sub array is replaced in the main array by the rep array,and
    >> > the rep array can be of different size from the sub array.

    >>
    >> So you replace a sub-string with a longer string but I do not see you
    >> allocating any memory for the new, larger string, anywhere. Also since
    >> you are passing string literals as arguments you are very likely to try
    >> to change constant data (it is an unfortunate inheritance from C that a
    >> string literal is not a char const*).
    >>
    >> > int _tmain(int argc, _TCHAR* argv[])
    >> > {
    >> > cout<<strrep("Abcdefghijk","cde","peternorton");
    >> > return 0;
    >> > }

    >>
    >> And the correct name for the main function is main and nothing else. If
    >> you are not using the arguments you might also leave them out:
    >>
    >> int main()
    >> {
    >> ...
    >> }
    >>
    >> Unless this is homework I would suggest that you use std::string instead
    >> of char* to handle strings, it will make things much easier. If that is
    >> not possible you might want to consider using <string.h> which will give
    >> you a number of useful functions when working with char*. If this *is*
    >> homework be advised that we do not do your homework for you, put we will
    >> answer specific questions about C++ and how to use it.


    Please do not quote signatures

    >
    > thanks for replying erik.
    >
    > firstly even if this would have been my homework.. i would not have
    > asked for a complete soln.
    > what i am interested in this discussion is the point where i am going
    > wrong nothing else.
    > yes i know that im not allocating any new memory space for new main
    > char array but what struck me is that why msvs caught this as error
    > and the turbo c++ didnt.


    It is because of how MSVC constructs to program (where it places the
    different pieces in memory and what protections it places on different
    memory-segments) which is off-topic here. Since I believe that modifying
    const objects gives undefined behaviour non of the compilers are wrong,
    but I like MSVC's way better.

    > secondly i dont want to use any inbuilt functions.


    Then create your own, start by creating a function for finding the
    length of a string, than one that finds a sub-string in another string
    then use those to implement a function which replaces a sub-string.

    > so in short allocating new memory space for larger main string and the
    > temp string would solve my problems?


    Maybe, I did not look very closely at your code, I just thought that
    that might be the problem and then confirmed it, you might have other
    bugs as well.

    --
    Erik Wikström
     
    Erik Wikström, Dec 9, 2007
    #7
  8. ashjas

    ashjas Guest

    Re: problem in code running on msvs2008 c++ while same logic runsfine on turbo c++ 3.0

    On Dec 9, 11:56 pm, Erik Wikström <> wrote:
    > On 2007-12-09 16:29, ashjas wrote:
    >
    >
    >
    >
    >
    > > On Dec 9, 4:30 pm, Erik Wikström <> wrote:
    > >> On 2007-12-09 11:23, ashjas wrote:

    >
    > >> > On Dec 9, 3:05 pm, ashjas <> wrote:

    >
    > >> [please trim quotes a bit]

    >
    > >> > please ignore above post there were some errors in cut /paste..

    >
    > >> > take a look here..

    >
    > >> > Hello,
    > >> > I have run this logic on turboc++ 3.0 and it is working fine on it
    > >> > but
    > >> > its not running on msvs2008 c++.

    >
    > >> > the code does this::
    > >> > the fun strrep takes 3 char arrays the i.e main,sub,rep.
    > >> > the sub array is to be found out i the main array and if found its
    > >> > index is taken on found pos,(this is achieved by the searchsub
    > >> > function separately)
    > >> > then the sub array is replaced in the main array by the rep array,and
    > >> > the rep array can be of different size from the sub array.

    >
    > >> So you replace a sub-string with a longer string but I do not see you
    > >> allocating any memory for the new, larger string, anywhere. Also since
    > >> you are passing string literals as arguments you are very likely to try
    > >> to change constant data (it is an unfortunate inheritance from C that a
    > >> string literal is not a char const*).

    >
    > >> > int _tmain(int argc, _TCHAR* argv[])
    > >> > {
    > >> > cout<<strrep("Abcdefghijk","cde","peternorton");
    > >> > return 0;
    > >> > }

    >
    > >> And the correct name for the main function is main and nothing else. If
    > >> you are not using the arguments you might also leave them out:

    >
    > >> int main()
    > >> {
    > >> ...
    > >> }

    >
    > >> Unless this is homework I would suggest that you use std::string instead
    > >> of char* to handle strings, it will make things much easier. If that is
    > >> not possible you might want to consider using <string.h> which will give
    > >> you a number of useful functions when working with char*. If this *is*
    > >> homework be advised that we do not do your homework for you, put we will
    > >> answer specific questions about C++ and how to use it.

    >
    > Please do not quote signatures
    >
    >
    >
    > > thanks for replying erik.

    >
    > > firstly even if this would have been my homework.. i would not have
    > > asked for a complete soln.
    > > what i am interested in this discussion is the point where i am going
    > > wrong nothing else.
    > > yes i know that im not allocating any new memory space for new main
    > > char array but what struck me is that why msvs caught this as error
    > > and the turbo c++ didnt.

    >
    > It is because of how MSVC constructs to program (where it places the
    > different pieces in memory and what protections it places on different
    > memory-segments) which is off-topic here. Since I believe that modifying
    > const objects gives undefined behaviour non of the compilers are wrong,
    > but I like MSVC's way better.
    >
    > > secondly i dont want to use any inbuilt functions.

    >
    > Then create your own, start by creating a function for finding the
    > length of a string, than one that finds a sub-string in another string
    > then use those to implement a function which replaces a sub-string.
    >
    > > so in short allocating new memory space for larger main string and the
    > > temp string would solve my problems?

    >
    > Maybe, I did not look very closely at your code, I just thought that
    > that might be the problem and then confirmed it, you might have other
    > bugs as well.
    >
    > --
    > Erik Wikström- Hide quoted text -
    >
    > - Show quoted text -


    thanks for help...

    I have completed both versions of the programs now on turbo c++ and
    msvs.

    :)
     
    ashjas, Dec 10, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Addison

    Server errors galore, local runs fine

    George Addison, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    318
    Tommy
    Nov 29, 2003
  2. Peri
    Replies:
    2
    Views:
    2,979
  3. Sam
    Replies:
    0
    Views:
    424
  4. Sebastien de Menten
    Replies:
    0
    Views:
    672
    Sebastien de Menten
    Feb 1, 2005
  5. S_K
    Replies:
    7
    Views:
    1,265
    Andy B.
    Mar 25, 2010
Loading...

Share This Page