您的位置:首页 > 其它

问题大纠结 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:

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

Marked As Answer by
alpha.beta10 Wednesday, September 28, 2011 11:28 AM

 


Reply


Quote

 

All Replies

Wednesday, September 28, 2011 7:54 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

 




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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: