C++ C+;中的函数调用中未更新字符串引用+;
我正在编写一个arduino库来在web上发布http请求 我正在使用来自的String类 当我在函数调用后引用我定义的字符串对象时,我的代码行为异常 实际上,这里我试图获取get请求的主体,并从http get请求的响应中删除http头 描述如下:C++ C+;中的函数调用中未更新字符串引用+;,c++,c,http,arduino,C++,C,Http,Arduino,我正在编写一个arduino库来在web上发布http请求 我正在使用来自的String类 当我在函数调用后引用我定义的字符串对象时,我的代码行为异常 实际上,这里我试图获取get请求的主体,并从http get请求的响应中删除http头 描述如下: bool Pinger::Get(String host, String path, String *response) { bool connection = false; bool status = false; St
bool Pinger::Get(String host, String path, String *response) {
bool connection = false;
bool status = false;
String post1 = "GET ";
post1 = post1.append(path);
post1 = post1.append(" HTTP/1.1");
String host1 = "Host: ";
host1 = host1.append(host);
for (int i = 0; i < 10; i++) {
if (client.connect()) {
client.println(post1);
client.println(host1);
client.println();
connection = true;
break;
}
}
int nlCnt = 0;
while (connection) {
if (client.available()) {
int c = client.read();
response->append((char) c);
if (c == 0x000A && nlCnt == 0) {
nlCnt++;
if (response->contains("200")) {
status = true;
continue;
} else {
client.stop();
client.flush();
break;
}
}
}
if (!client.connected()) {
client.stop();
connection = false;
}
}
response = &response->substring(response->indexOf("\n\r\n"),response->length());
Serial.println("Modified String: ");
Serial.println(*response);
Serial.println();
Serial.print("Modified String Address: ");
Serial.println((int)&response);
return status;
}
方法调用:
String body;
if(pinger.Get(host,path,&body))
{
Serial.println("Modified String Outside :");
Serial.println(body);
Serial.println();
Serial.println("Modified String Outside Address");
Serial.println((int)&body);
}
输出
Modified String Outside :
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html
Content-Length: 113
Date: Wed, 13 Jan 2010 14:36:28 GMT
<html>
<head>
<title>Ashish Sharma
</title>
</head>
<body>
Wed Jan 13 20:06:28 IST 2010
</body>
</html>
Modified String Outside Address
2273
外部修改的字符串:
HTTP/1.1200ok
服务器:ApacheCoote/1.1
内容类型:text/html
内容长度:113
日期:2010年1月13日星期三14:36:28 GMT
阿什·夏尔马
2010年1月13日星期三20:06:28
修改的字符串外部地址
2273
方法说明:
bool Pinger::Get(String host, String path, String *response) {
bool connection = false;
bool status = false;
String post1 = "GET ";
post1 = post1.append(path);
post1 = post1.append(" HTTP/1.1");
String host1 = "Host: ";
host1 = host1.append(host);
for (int i = 0; i < 10; i++) {
if (client.connect()) {
client.println(post1);
client.println(host1);
client.println();
connection = true;
break;
}
}
int nlCnt = 0;
while (connection) {
if (client.available()) {
int c = client.read();
response->append((char) c);
if (c == 0x000A && nlCnt == 0) {
nlCnt++;
if (response->contains("200")) {
status = true;
continue;
} else {
client.stop();
client.flush();
break;
}
}
}
if (!client.connected()) {
client.stop();
connection = false;
}
}
response = &response->substring(response->indexOf("\n\r\n"),response->length());
Serial.println("Modified String: ");
Serial.println(*response);
Serial.println();
Serial.print("Modified String Address: ");
Serial.println((int)&response);
return status;
}
boolpinger::Get(字符串主机、字符串路径、字符串*响应){
布尔连接=假;
布尔状态=假;
字符串post1=“GET”;
post1=post1.append(路径);
post1=post1.append(“HTTP/1.1”);
字符串host1=“主机:”;
host1=host1.append(主机);
对于(int i=0;i<10;i++){
if(client.connect()){
client.println(post1);
client.println(主机1);
client.println();
连接=真;
打破
}
}
int-nlCnt=0;
while(连接){
if(client.available()){
int c=client.read();
响应->附加((char)c);
如果(c==0x000A&&nlCnt==0){
nlCnt++;
如果(响应->包含(“200”)){
状态=真;
继续;
}否则{
client.stop();
client.flush();
打破
}
}
}
如果(!client.connected()){
client.stop();
连接=错误;
}
}
response=&response->substring(response->indexOf(“\n\r\n”),response->length();
Serial.println(“修改的字符串:”);
Serial.println(*响应);
Serial.println();
Serial.print(“修改的字符串地址:”);
Serial.println((int)和response);
返回状态;
}
输出:
Modified String:
Ø
<html>
<head>
<title>Ashish Sharma
</title>
</head>
<body>
Wed Jan 13 20:06:28 IST 2010
</body>
</html>
Modified String Address: 2259
修改的字符串:
Ø
阿什·夏尔马
2010年1月13日星期三20:06:28
修改字符串地址:2259
从示例中可以看出,string reference对象在Get方法中为我提供了正确的字符串,但是当Get方法返回时,字符串内容的引用会发生更改。首先,您要修改的是字符串的地址,而不是字符串本身。但是字符串的地址是通过值传递给函数的,因此被复制。在函数内部修改它不会在外部修改它 其次,这里的代码很糟糕:
response = &response->substring(response->indexOf("\n\r\n"),response->length());
因为它创建了一个指向临时对象的指针——这意味着:一个悬空指针,因为临时对象将在表达式求值后被销毁
您真正想要的是通过引用(String&response
)传递对象并修改它,而不是它的指针:
response = response->substring(response->indexOf("\n\r\n"),response->length());
如果您正确使用的
字符串
类重载赋值运算符的行为,应该会起作用,如果我正确理解了您的代码,您可能会希望执行以下操作:
*response = response->substring(response->indexOf("\n\r\n"),response->length());
而不是
response = &response->substring(response->indexOf("\n\r\n"),response->length());
此外,可能不需要传入指针(引用可能会使代码看起来更好)。行
Serial.println((int)&response);
在函数内部是错误的响应已经是指针(String*response),使用&response可以获得指向指针的指针。换成
Serial.println((int)response);
你应该和我的地址一样
Serial.println((int)&body);
其中body是字符串,&body是指向该字符串的指针