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

A

ashjas

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;
}
 
A

ashjas

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;
}
 
E

Erik Wikström

On Dec 9, 3:05 pm, ashjas <[email protected]> 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.
 
B

Bo Persson

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
 
A

ashjas

On Dec 9, 3:05 pm, ashjas <[email protected]> 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.

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.
 
A

ashjas

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.
 
E

Erik Wikström

On Dec 9, 3:05 pm, ashjas <[email protected]> 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.
 
A

ashjas

On 2007-12-09 11:23, 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.

thanks for help...

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

:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top