Does this 'client' object exist entirely on the stack?

Looking at the example Ethernet - AdvancedChatServer sketch supplied with the Auduino IDE, it contains the following:

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

I don't see any code within the rest of the loop() function that deletes the client object and I don't see anything in the source for EthernetClient that looks like a destructor.

Does it automatically get deleted after the function completes?

As this is an example sketch supplied with the IDE I would assume that this code doesn't leak memory, is this correct?


(edited for typo)

Yes, local variables and objects are deleted when they go out of scope. Generally, if you're not using new to create them, then you don't have to worry about deleting them yourself.

Destructor would be nice but these are Arduino LLC people. You'll be lucky to have .end() to cover all the .begin().
I assume that default destructor is called (or maybe not) to free memory on the stack.

Anyway, I did some tests with an adafruit RTClib library RTC_DS3231 object and the pointer value to an object I create in loop always points into the same location iteration after iteration. The source code has no destructor defined.

Depending on your mood, you may find this link enlightning or just scary.


I'd assume that client contains a reference to something on the heap or in static memory. If the ethernet library can only handle one ethernet connection (which is fair enough for what most people want to use it for), then it's a good bet that it's static.

So, the answer is "probably not" - it doesn't exist entirely on the stack in the sense that some of its parts are elsewhere. It depends on what you mean by "part of".