关于c ++指针 [英] About c++ pointer

查看:55
本文介绍了关于c ++指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/////////////// code 1 start ///////////////////////

char * GetString(void)

{

char p [] =" hello world" ;;

return p;

}

void Test4(无效)

{

char * str = NULL;

str = GetString();

cout<< str<< endl;

}

/////////////// code 1 end ///////////// ///////////


///////////////// code 2 start /////// /////////////

char * GetString2(无效)

{

char * p =" hello world" ;;

返回p;

}

void Test5(无效)

{

char * str = NULL;

str = GetString2();

cout<< str<< endl;

}

///////////////// code 2 end /////////// //////////


问:代码1和代码2有什么区别?

/////////////// code 1 start ///////////////////////
char *GetString(void)
{
char p[] = "hello world";
return p;
}
void Test4(void)
{
char *str = NULL;
str = GetString();
cout<< str << endl;
}
/////////////// code 1 end ////////////////////////

///////////////// code 2 start ////////////////////
char *GetString2(void)
{
char *p = "hello world";
return p;
}
void Test5(void)
{
char *str = NULL;
str = GetString2();
cout<< str << endl;
}
///////////////// code 2 end /////////////////////

Q: What is the difference between code 1 and code 2?

推荐答案

zhou xiang写道:
zhou xiang wrote:
/////////////// code 1 start ////////////// /////////
char * GetString(void)
{
char p [] =" hello world" ;;
返回p;
}
void Test4(void)
{* * char * str = NULL;
str = GetString();
cout<< str<< endl;
}
/////////////// code 1 end ///////////////////// ///

///////////////// code 2 start /////////////////// /
char * GetString2(void)
{
char * p =" hello world" ;;
return p;
}
void Test5(void )
{* * char * str = NULL;
str = GetString2();
cout<< str<< endl;
}
///////////////// code 2 end /////////////////// //

问:代码1和代码2有什么区别?
/////////////// code 1 start ///////////////////////
char *GetString(void)
{
char p[] = "hello world";
return p;
}
void Test4(void)
{
char *str = NULL;
str = GetString();
cout<< str << endl;
}
/////////////// code 1 end ////////////////////////

///////////////// code 2 start ////////////////////
char *GetString2(void)
{
char *p = "hello world";
return p;
}
void Test5(void)
{
char *str = NULL;
str = GetString2();
cout<< str << endl;
}
///////////////// code 2 end /////////////////////

Q: What is the difference between code 1 and code 2?



代码1不起作用,返回时指针为空,

因为对象(字符串数组)是在堆栈中创建的。虽然第二个

很好,因为你在堆中动态分配内存。


The code 1 will not work, the pointer will be empty when it returns,
because the object(string array) is created in stack. While the second
is fine since you dynamically allocate memory in the heap.




" zhou xiang" <菊******** @ eyou.com> skrev i en meddelelse

news:82 ************************** @ posting.google.c om .. 。

"zhou xiang" <ju********@eyou.com> skrev i en meddelelse
news:82**************************@posting.google.c om...
/////////////// code 1 start ///////////////////////
char * GetString(void)
{
char p [] =" hello world" ;;


在此声明一个本地数组并将其初始化为Hello world。

return p;
....并且你返回一个指向该数组的指针,然后释放它 - 因此p将

可能指向垃圾。 }
void Test4(void)
{* * char * str = NULL;
str = GetString();
cout<< str<< ENDL;


这是未定义的行为,可能会打印垃圾或停止程序。

}
//////////// ///代码1结束////////////////////////
////////////// ///// code 2 start ////////////////////
char * GetString2(void)
{
char * p =hello world;


这里你声明一个指针,指向一个包含

" hello world"的恒定静态区域。这是可以的,但很危险:可以更改区域(例如,通过执行p [0] =''H'来获得
),但这会导致未定义的行为。

上面的行应该是

char const * p = ...;

return p;
}
void Test5(void)
{
char * str = NULL;
str = GetString2();
cout<< str<< endl;
}
/////////////// code 1 start ///////////////////////
char *GetString(void)
{
char p[] = "hello world";
Here you declare a local array and initialise it to "Hello world".
return p; .... and you return a pointer to that array, and deallocate it - thus p will
likely point to garbage. }
void Test4(void)
{
char *str = NULL;
str = GetString();
cout<< str << endl;
This is undefined behaviour and might print garbage or stop the program.
}
/////////////// code 1 end ////////////////////////

///////////////// code 2 start ////////////////////
char *GetString2(void)
{
char *p = "hello world";
Here you declare a pointer which points to a constant static area containing
"hello world". This is okay, but dangerous: the area could be changed (e.g.
by executing p[0] = ''H''), but that would lead to undefined behaviour.
The above line should read
char const* p= ...;
return p;
}
void Test5(void)
{
char *str = NULL;
str = GetString2();
cout<< str << endl;
}



/ Peter


/Peter


Andrey写道:
Andrey wrote:
第二个没问题,因为你在堆中动态分配内存。
the second is fine since you dynamically allocate memory in the heap.



在''char * p =" hallo";''动态内存分配的距离?

指针p在离开示波器时也没有任何指示。


我错过了什么吗?


问候,

Matthias



In how far is ''char * p = "hallo";'' dynamic memory allocation?
The pointer p will point to nothing as well when leaving the scope.

Am I missing something?

Regards,
Matthias


这篇关于关于c ++指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆