Does toCharArray prevent a String being created on the heap? I know it's bad to use Strings due to the memory problems that can occur.
I've tried to remove all instances of strings from my code.
But I'm not sure about this line:

http.getString().toCharArray(ipAddressBuffer, lengthOfIPAddress);  


Is calling toCharArray() enough to prevent memory problems? Or is there a string still being created on the heap?

If it's still created on the heap, how can I modify the code so it only deals with cstrings?

Here's my code:

void setIPAddress(char* ipAddressBuffer){
  HTTPClient http;
  http.begin("http://bot.whatismyipaddress.com/"); //Only allowed to call once per 5 mins.
  http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
  int httpCode = http.GET();
  if (httpCode &gt; 0) {    
    http.getString().toCharArray(ipAddressBuffer, lengthOfIPAddress);    
  }
  else {
    Serial.println("Error on HTTP request");
  }
  http.end();
}


I'm using the HTTPClient.h library btw.
The return value of getString allocates and the deletes a char array in heap for a temporary String object.

For ESP8266HTTPClient and ESP32 HTTPClient library you can use writeToStream or getStream()to read the response without using String class with getString(), where stream is WiFiClient&amp; stream = client.getStream();

getStream, returns the underlying WiFiClient. You can read the data with classic while (stream.available()) { char c = stream.read; ... }

writeToStream copies the data to a stream. My StreamLib library has a stream wrapper over a char array. It is called CStringBuilder.