2010-01-13 28 views
3

我正在写一个arduino库来在web上发布http请求。C++中函数调用时不更新的字符串引用

我使用String类从http://arduino.cc/en/Tutorial/TextString

我的代码的行为出现异常的时候,我一个函数调用后指我定义的字符串对象。

这里其实我试图得到我的GET请求的正文,并从http GET请求的响应中删除http头。

以下是说明:

方法调用:

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 

方法说明:

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

输出:

Modified String: 
Ø 
<html> 
<head> 
<title>Ashish Sharma 
</title> 
</head> 
<body> 
Wed Jan 13 20:06:28 IST 2010 
</body> 
</html> 

Modified String Address: 2259 

从示例中可以看出,字符串引用对象在Get方法内部给出了正确的字符串,但Get方法返回时字符串内容的引用发生了变化。

回答

3

如果我理解正确的代码,你可能会想要做这样的事情:

*response = response->substring(response->indexOf("\n\r\n"),response->length()); 

代替

response = &response->substring(response->indexOf("\n\r\n"),response->length()); 

也可能没有必要在一个指针传递(参考会可能会使代码看起来更好)。

3

首先,您正在学习字符串的地址,而不是字符串本身。但是该字符串的地址被传递给功能,值为,因此被复制。在函数内修改它不会在外部修改它。

其次,这个代码在这里是坏:

response = &response->substring(response->indexOf("\n\r\n"),response->length()); 

因为它创建一个指针到一个临时对象 - 这意味着:一个悬挂指针,因为表达式已被评估之后,临时对象将被破坏。

真的想要的是通过引用(String& response)传递对象,然后修改它,而不是它的指针:

response = response->substring(response->indexOf("\n\r\n"),response->length()); 

这应该工作,提供您正确使用String类重载行为的转让运营商=

1

线

Serial.println((int)&response); 

自己的函数中是错误的反应已经是一个指针(字符串*响应),与&响应你的指针的指针。
将其更改为

Serial.println((int)response); 

,你应该得到相同的地址与

Serial.println((int)&body); 

,其中身体是一个String和&体是字符串指针

相关问题