Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem in code running on msvs2008 c++ while same logic runs fine onturbo c++ 3.0

Reply
Thread Tools

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

 
 
ashjas
Guest
Posts: n/a
 
      12-09-2007
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[i]!='\0';i++)
mainlen++;
for( i=0;fndstr[i]!='\0';i++)
sublen++;
for(i=0;i<mainlen;i++)
{
index=i;
if(mainstr[i]==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;
}


 
Reply With Quote
 
 
 
 
ashjas
Guest
Posts: n/a
 
      12-09-2007
On Dec 9, 3:05 pm, ashjas <(E-Mail Removed)> 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[i]!='\0';i++)
> mainlen++;
> for( i=0;fndstr[i]!='\0';i++)
> sublen++;
> for(i=0;i<mainlen;i++)
> {
> index=i;
> if(mainstr[i]==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[i]!='\0';i++)
mainlen++;
for( i=0;fndstr[i]!='\0';i++)
sublen++;
for(i=0;i<mainlen;i++)
{
index=i;
if(mainstr[i]==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;
}


 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      12-09-2007
On 2007-12-09 11:23, ashjas wrote:
> On Dec 9, 3:05 pm, ashjas <(E-Mail Removed)> 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
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      12-09-2007
ashjas wrote:
:: On Dec 9, 3:05 pm, ashjas <(E-Mail Removed)> 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


 
Reply With Quote
 
ashjas
Guest
Posts: n/a
 
      12-09-2007
On Dec 9, 4:30 pm, Erik Wikstrm <(E-Mail Removed)> wrote:
> On 2007-12-09 11:23, ashjas wrote:
>
> > On Dec 9, 3:05 pm, ashjas <(E-Mail Removed)> 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 Wikstrm- 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.
 
Reply With Quote
 
ashjas
Guest
Posts: n/a
 
      12-09-2007
On Dec 9, 4:35 pm, "Bo Persson" <(E-Mail Removed)> wrote:
> ashjas wrote:
>
> :: On Dec 9, 3:05 pm, ashjas <(E-Mail Removed)> 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.
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      12-09-2007
On 2007-12-09 16:29, ashjas wrote:
> On Dec 9, 4:30 pm, Erik Wikström <(E-Mail Removed)> wrote:
>> On 2007-12-09 11:23, ashjas wrote:
>>
>> > On Dec 9, 3:05 pm, ashjas <(E-Mail Removed)> 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
 
Reply With Quote
 
ashjas
Guest
Posts: n/a
 
      12-10-2007
On Dec 9, 11:56 pm, Erik Wikstrm <(E-Mail Removed)> wrote:
> On 2007-12-09 16:29, ashjas wrote:
>
>
>
>
>
> > On Dec 9, 4:30 pm, Erik Wikstrm <(E-Mail Removed)> wrote:
> >> On 2007-12-09 11:23, ashjas wrote:

>
> >> > On Dec 9, 3:05 pm, ashjas <(E-Mail Removed)> 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 Wikstrm- Hide quoted text -
>
> - Show quoted text -


thanks for help...

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error while trying to establish connection to SQL Server from MSVS2008 S_K ASP .Net 7 03-25-2010 09:35 AM
While deploying application on AIX I got this error. while it's working fine on windows Sam Java 0 06-21-2006 06:26 AM
Call a DLL LIB function in ASP.NET (Code Behind) or VB.NET (The same code is working fine in VB 6.0) Peri ASP .Net 2 07-20-2005 03:13 AM
python ScriptControl error in Excel while running fine in python Sebastien de Menten Python 0 02-01-2005 11:47 AM
Server errors galore, local runs fine George Addison ASP .Net 1 11-29-2003 12:00 AM



Advertisments