问题大纠结 debug要用debug的库 release要用release库 没错 这就是vs2010
2012-12-04 15:19
609 查看
先上两篇文章 再分析自己的:
http://blog.csdn.net/pcliuguangtao/article/details/5990561
C++中赋值和初始化不同“实例说明---欢迎大家发表自己的看法
#include
using namespace std;
class Astring
{
public:
Astring(){s_=NULL;}
Astring(const char *init);
~Astring();
Astring(const Astring &that);
Astring &operator=(const Astring &that);
Astring &operator=(const char *str);
friend ostream& operator<<(ostream&,Astring&);
void swap(Astring &that);
private:
Astring(const char*,const char *);
char *s_;
};
ostream& operator<<(ostream& output,Astring& str)
{
output<<STR.S_<<" n?;
return output;
}
Astring::Astring(const char *init)
{
if(!init) init="";
s_=new char[strlen(init)+1];
strcpy(s_,init);
}
Astring::~Astring()
{delete [] s_;}
Astring &Astring::operator=(const char *str)
{
if(!str) str="";
char *tmp=strcpy(new char[strlen(str)+1],str);
delete [] s_;
s_=tmp;
return *this;
}
int main()
{
Astring *names=static_cast(::operator new(100));
*names="dsfsdsfsd";
cout<<NAMES;
return 0;
}
上面mian函数中*names="dsfsdsfsd";是有问题的,在vs2010里面原因是:
First-chance exception at 0x59eb59da (msvcr100d.dll) in test.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1.
Unhandled exception at 0x59eb59da (msvcr100d.dll) in test.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1. 非法访问内存地址!!!
但是我们已经定义了:Astring &Astring::operator=(const char *str),是因为赋值的时候会先清除左边的目标,然后再进行赋值;但是,我们直接使用的是new:
Astring *names=static_cast(::operator new(100));
并没有调用Astring的任何构造函数,即names并没有被初始化,但是*names="dsfsdsfsd"就会先删除*names里面的内容,所以出现错误。
改为下面的就可以了:
int main()
{
Astring names;
names="dsfsdsfsd";
cout<<NAMES;
return 0;
}
其中Astring names会默认调用
Astring(){s_=NULL;}构造函数,已经进行了初始化,所以可以成功的进行赋值。
http://social.msdn.microsoft.com/Forums/en-NZ/vcgeneral/thread/00ff8bf8-df4d-4d5f-ae72-d998165cee26
I have this:
I get unhandled exception at some memory block. Access violation reading some memory location. The stack frame in VS shows _output_s_l() in output.c breaking at line 1643. Your input will be highly appreciated.
Signature series
Reply
Quote
Giovanni Dicanio
Giovanni Dicanio
MVP
15,095 Points
13
7
2
Recent Achievements
Forums Replies IV
Forums Curator I
Gallery Contributor II
Giovanni Dicanio's threads
View Profile
(MVP)
15,095 Points
0
Sign
In to Vote
On 28/09/2011 09:59, alpha.beta10 wrote:
I see, but I need the first 2 or 3 characters at most. What would you reckon to use for copying single characters from a string?
For just 2/3 characters you may want to directly copy them from source to destination:
Giovanni
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 8:13 AM
Unmarked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:26 AM
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:31 AM
Reply
Quote
Wednesday, September 28, 2011 11:22 AM
WayneAKing
WayneAKing
MCC
39,130 Points
10
4
3
Recent Achievements
Forums Replies V
Forums Replies VI
Proposed Answerer I
WayneAKing's threads
View Profile
(MCC)
39,130 Points
0
Sign
In to Vote
Some other ways to copy a specified number of chars only:
sprintf_s(buffer,128,"%.4s", something); // copy max 4
memcpy_s(buffer, 128, something, 4); // always copy 4
With memcpy_s always ensure that there's a nul character
at the end after the copy if you intend to use buffer as
a C string. It does't add one automatically, while
sprintf_s does.
- Wayne
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:28 AM
Reply
Quote
Giovanni Dicanio
Giovanni Dicanio
MVP
15,095 Points
13
7
2
Recent Achievements
Forums Replies IV
Forums Curator I
Gallery Contributor II
Giovanni Dicanio's threads
View Profile
(MVP)
15,095 Points
1
Sign
In to Vote
On 28/09/2011 09:37, alpha.beta10 wrote:
I have this:
You may want to try using "something" (a string) instead of "something[0]" (a character).
Moreover, I'd use sizeof(buffer) instead of "128" inside sprintf_s. Or, better, if you are compiling in C++, just use sprintf_s with template magic understanding buffer size:
Giovanni
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 8:13 AM
Unmarked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:30 AM
Reply
Quote
Wednesday, September 28, 2011 7:59 AM
alpha.beta10
alpha.beta10
0 Points
4
0
0
Recent Achievements
Code Answerer I
First Helpful Vote
First Forums Reply
alpha.beta10's threads
View Profile
0 Points
0
Sign
In to Vote
I see, but I need the first 2 or 3 characters at most. What would you reckon to use for copying single characters from a string?
Signature series
Reply
Quote
Wednesday, September 28, 2011 8:04 AM
Giovanni Dicanio
Giovanni Dicanio
MVP
15,095 Points
13
7
2
Recent Achievements
Forums Replies IV
Forums Curator I
Gallery Contributor II
Giovanni Dicanio's threads
View Profile
(MVP)
15,095 Points
0
Sign
In to Vote
On 28/09/2011 09:59, alpha.beta10 wrote:
I see, but I need the first 2 or 3 characters at most. What would you reckon to use for copying single characters from a string?
For just 2/3 characters you may want to directly copy them from source to destination:
Giovanni
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 8:13 AM
Unmarked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:26 AM
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:31 AM
Reply
Quote
Wednesday, September 28, 2011 11:22 AM
WayneAKing
WayneAKing
MCC
39,130 Points
10
4
3
Recent Achievements
Forums Replies V
Forums Replies VI
Proposed Answerer I
WayneAKing's threads
View Profile
(MCC)
39,130 Points
0
Sign
In to Vote
Some other ways to copy a specified number of chars only:
sprintf_s(buffer,128,"%.4s", something); // copy max 4
memcpy_s(buffer, 128, something, 4); // always copy 4
With memcpy_s always ensure that there's a nul character
at the end after the copy if you intend to use buffer as
a C string. It does't add one automatically, while
sprintf_s does.
- Wayne
http://blog.csdn.net/pcliuguangtao/article/details/5990561
C++中赋值和初始化不同“实例说明---欢迎大家发表自己的看法
#include
using namespace std;
class Astring
{
public:
Astring(){s_=NULL;}
Astring(const char *init);
~Astring();
Astring(const Astring &that);
Astring &operator=(const Astring &that);
Astring &operator=(const char *str);
friend ostream& operator<<(ostream&,Astring&);
void swap(Astring &that);
private:
Astring(const char*,const char *);
char *s_;
};
ostream& operator<<(ostream& output,Astring& str)
{
output<<STR.S_<<" n?;
return output;
}
Astring::Astring(const char *init)
{
if(!init) init="";
s_=new char[strlen(init)+1];
strcpy(s_,init);
}
Astring::~Astring()
{delete [] s_;}
Astring &Astring::operator=(const char *str)
{
if(!str) str="";
char *tmp=strcpy(new char[strlen(str)+1],str);
delete [] s_;
s_=tmp;
return *this;
}
int main()
{
Astring *names=static_cast(::operator new(100));
*names="dsfsdsfsd";
cout<<NAMES;
return 0;
}
上面mian函数中*names="dsfsdsfsd";是有问题的,在vs2010里面原因是:
First-chance exception at 0x59eb59da (msvcr100d.dll) in test.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1.
Unhandled exception at 0x59eb59da (msvcr100d.dll) in test.exe: 0xC0000005: Access violation reading location 0xcdcdcdc1. 非法访问内存地址!!!
但是我们已经定义了:Astring &Astring::operator=(const char *str),是因为赋值的时候会先清除左边的目标,然后再进行赋值;但是,我们直接使用的是new:
Astring *names=static_cast(::operator new(100));
并没有调用Astring的任何构造函数,即names并没有被初始化,但是*names="dsfsdsfsd"就会先删除*names里面的内容,所以出现错误。
改为下面的就可以了:
int main()
{
Astring names;
names="dsfsdsfsd";
cout<<NAMES;
return 0;
}
其中Astring names会默认调用
Astring(){s_=NULL;}构造函数,已经进行了初始化,所以可以成功的进行赋值。
http://social.msdn.microsoft.com/Forums/en-NZ/vcgeneral/thread/00ff8bf8-df4d-4d5f-ae72-d998165cee26
I have this:
char *something="thisthing"; char buffer[128]={0}; sprintf_s(buffer,128,"%s", something[0]);
I get unhandled exception at some memory block. Access violation reading some memory location. The stack frame in VS shows _output_s_l() in output.c breaking at line 1643. Your input will be highly appreciated.
Signature series
Reply
Quote
Answers
Wednesday, September 28, 2011 8:04 AMGiovanni Dicanio
Giovanni Dicanio
MVP
15,095 Points
13
7
2
Recent Achievements
Forums Replies IV
Forums Curator I
Gallery Contributor II
Giovanni Dicanio's threads
View Profile
(MVP)
15,095 Points
0
Sign
In to Vote
On 28/09/2011 09:59, alpha.beta10 wrote:
I see, but I need the first 2 or 3 characters at most. What would you reckon to use for copying single characters from a string?
For just 2/3 characters you may want to directly copy them from source to destination:
buffer[0] = something[0]; buffer[1] = something[1]; buffer[2] = '\0'; // end of string
Giovanni
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 8:13 AM
Unmarked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:26 AM
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:31 AM
Reply
Quote
Wednesday, September 28, 2011 11:22 AM
WayneAKing
WayneAKing
MCC
39,130 Points
10
4
3
Recent Achievements
Forums Replies V
Forums Replies VI
Proposed Answerer I
WayneAKing's threads
View Profile
(MCC)
39,130 Points
0
Sign
In to Vote
Some other ways to copy a specified number of chars only:
sprintf_s(buffer,128,"%.4s", something); // copy max 4
memcpy_s(buffer, 128, something, 4); // always copy 4
With memcpy_s always ensure that there's a nul character
at the end after the copy if you intend to use buffer as
a C string. It does't add one automatically, while
sprintf_s does.
- Wayne
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:28 AM
Reply
Quote
All Replies
Wednesday, September 28, 2011 7:54 AMGiovanni Dicanio
Giovanni Dicanio
MVP
15,095 Points
13
7
2
Recent Achievements
Forums Replies IV
Forums Curator I
Gallery Contributor II
Giovanni Dicanio's threads
View Profile
(MVP)
15,095 Points
1
Sign
In to Vote
On 28/09/2011 09:37, alpha.beta10 wrote:
I have this:
char *something="thisthing"; char buffer[128]={0}; sprintf_s(buffer,128,"%s", something[0]);
You may want to try using "something" (a string) instead of "something[0]" (a character).
Moreover, I'd use sizeof(buffer) instead of "128" inside sprintf_s. Or, better, if you are compiling in C++, just use sprintf_s with template magic understanding buffer size:
sprintf_s(buffer, "%s", something);
Giovanni
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 8:13 AM
Unmarked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:30 AM
Reply
Quote
Wednesday, September 28, 2011 7:59 AM
alpha.beta10
alpha.beta10
0 Points
4
0
0
Recent Achievements
Code Answerer I
First Helpful Vote
First Forums Reply
alpha.beta10's threads
View Profile
0 Points
0
Sign
In to Vote
I see, but I need the first 2 or 3 characters at most. What would you reckon to use for copying single characters from a string?
Signature series
Reply
Quote
Wednesday, September 28, 2011 8:04 AM
Giovanni Dicanio
Giovanni Dicanio
MVP
15,095 Points
13
7
2
Recent Achievements
Forums Replies IV
Forums Curator I
Gallery Contributor II
Giovanni Dicanio's threads
View Profile
(MVP)
15,095 Points
0
Sign
In to Vote
On 28/09/2011 09:59, alpha.beta10 wrote:
I see, but I need the first 2 or 3 characters at most. What would you reckon to use for copying single characters from a string?
For just 2/3 characters you may want to directly copy them from source to destination:
buffer[0] = something[0]; buffer[1] = something[1]; buffer[2] = '\0'; // end of string
Giovanni
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 8:13 AM
Unmarked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:26 AM
Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:31 AM
Reply
Quote
Wednesday, September 28, 2011 11:22 AM
WayneAKing
WayneAKing
MCC
39,130 Points
10
4
3
Recent Achievements
Forums Replies V
Forums Replies VI
Proposed Answerer I
WayneAKing's threads
View Profile
(MCC)
39,130 Points
0
Sign
In to Vote
Some other ways to copy a specified number of chars only:
sprintf_s(buffer,128,"%.4s", something); // copy max 4
memcpy_s(buffer, 128, something, 4); // always copy 4
With memcpy_s always ensure that there's a nul character
at the end after the copy if you intend to use buffer as
a C string. It does't add one automatically, while
sprintf_s does.
- Wayne
相关文章推荐
- VS2010-Debug&Release烦人的问题记录
- ffmpeg在Win7 VS2010中debug通过,release出错的问题解决方法
- 关于vs2010编译的问题#debug编译成功release编译失败#
- vs2010 下release编译的时候出现问题:_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”
- vs2010 创建预编译头 Debug 正常 Release Link Error问题解决
- Debug & Release时遇到的问题
- 解决 CListCtrl 中用GetColumn在debug下正常, Release模式程序崩溃的问题
- win7, vs2010, ,ffmpeg release 版本崩溃问题
- opencv下关于debug和release的切换报错问题
- VS2010 Release版本运行提示丢失msvcr100d.dll和msvcp100d.dll(Debug版本的库)
- DEBUG和RELEASE 版本差异及调试相关问题 (转载)
- dll的内存申请和释放问题--Debug程序正常而Release程序崩溃
- Debug模式和Release模式需要注意的问题
- 关于Debug版正常运行,release版运行崩溃的问题
- VC下关于debug和release的不同的问题总结
- 记录一个问题:win32程序release版本和debug版本运行效果不同
- Opencv学习笔记——release和debug两个模式的运行问题
- release 和 debug 的问题集
- VS2010 release 和 debug 调试区别