473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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;
}
Dec 9 '07 #1
7 1510
On Dec 9, 3:05 pm, ashjas <ash...@gmail.comwrote:
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;
}
Dec 9 '07 #2
On 2007-12-09 11:23, ashjas wrote:
On Dec 9, 3:05 pm, ashjas <ash...@gmail.comwrote:
[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.hwhich 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
Dec 9 '07 #3
ashjas wrote:
:: On Dec 9, 3:05 pm, ashjas <ash...@gmail.comwrote:
::
:: 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
Dec 9 '07 #4
On Dec 9, 4:30 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-12-09 11:23, ashjas wrote:
On Dec 9, 3:05 pm, ashjas <ash...@gmail.comwrote:

[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.hwhich 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.
Dec 9 '07 #5
On Dec 9, 4:35 pm, "Bo Persson" <b...@gmb.dkwrote:
ashjas wrote:

:: On Dec 9, 3:05 pm, ashjas <ash...@gmail.comwrote:
::
:: 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.
Dec 9 '07 #6
On 2007-12-09 16:29, ashjas wrote:
On Dec 9, 4:30 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-12-09 11:23, ashjas wrote:
On Dec 9, 3:05 pm, ashjas <ash...@gmail.comwrote:

[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.hwhich 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
Dec 9 '07 #7
On Dec 9, 11:56 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-12-09 16:29, ashjas wrote:


On Dec 9, 4:30 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-12-09 11:23, ashjas wrote:
On Dec 9, 3:05 pm, ashjas <ash...@gmail.comwrote:
[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.hwhich 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.

:)
Dec 10 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hank | last post by:
I have two SQL Server 2000 machines (server_A and server_B). I've used sp_addlinkedserver to link them both, the link seems to behave fine. I can execute remote queries and do all types of neat...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
6
by: billmiami2 | last post by:
I'm experiencing a strange problem that I believe is related to ADO.NET but I can't say for sure. I have a simple ASP.NET reporting interface to a SQL Server 2000 database. One report that we...
8
by: sara | last post by:
I have a report that runs fine with data. If there is no data, I have its NO Data event sending a MsgBox and cancelling the report. Then it seems I still get the 2501 message on the Open Report...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
1
by: ARC | last post by:
I have some functions that will allow a user to attach to a different back-end database. I added this functionality to the program Ribbon in a custom Access 2007 app. I have 2 ways to attach to a...
11
by: Flexor | last post by:
I have a php script that runs from command line and makes an https request to paypal, using curl. It works fine if I run it from a web page. It fails if I run it from CLI. The error I get from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.