Memory management

Greetings,

I've made a small programm (on the arduino MKR vidor 4000) and in this one, I need to use some string.
I've read that it's better for the memory to use char[] than strings. So i've made a tiny function to create a simple string (just the same char with a space between each).

void genPix(char str[],int nbpix,char val)
{
  int i;
  for(i = 0;i<nbpix;i+=2)
  {
    str[i] = val;
    str[i+1] = ' ';
  }
  str[nbpix] ='\0';
}

It seems to be working but after the call of the function, the programm doesn't seems to respond.
It might also be linked to my use of the WifiNina Library, which in that case i will ask this on the related part of the forum.

It's probably something wrong with the code you didn't post.

Well, when I just comment the call of the function and create by myself a string
char something[] = "something";
It works perfectly.
So I don't know the problem should be in the other part of my code.

You're running off the end of your string.

Sorry but I don't understand why.
With the char[] the end depends on the char '\0' ?
So if I replace it by an other char then add the \0 as the last character it should be good ?

Because you don't own the memory past the end of your string.

And How can I do that ?
By declaring a string of the good size since the beggining ?
Or by allocating a new string each time I need it ?

By using a string of an appropriate size.

Like I said, the answer is in the code you didn't post.

Making the assumption that nbpix is the size of str[] , you cannot write to str[ nbpix ] because array elements are numbered starting with 0. As an example, a 20 element array is addressed as elements 0 through 19, writing to element 20 is outside the array and will overwrite whatever is stored in memory immediately after the array. Your for loop has the same problem, if i is 1 less than nbpix, then i+1 will be writing past the end of the array.

This may be totally incorrect, but without seeing the complete code we can only guess at what you are passing to the function, how variables were declared, etc.

void genPix(char str[],int nbpix,char val)
{
  int i;
  for(i = 0;i<nbpix;i+=2)
  {
    str[i] = val;
    str[i+1] = ' ';
  }
  str[nbpix] ='\0';
}
#include <SPI.h>
#include <WiFiNINA.h>
#include <string.h>

#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)


char myimg[101] = {"Just an image"};   
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
    Serial.println(myimg);
}


void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          genPix(myimg,100,'1');
          client.println(myimg);
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

void genPix(char str[],int nbpix,char val)
{
  int i;
  for(i = 0;i<nbpix;i+=2)
  {
    str[i] = val;
    str[i+1] = ' ';
  }
  str[nbpix] ='\0';
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Here is the full code ? So with the declaration of char myimg[101] it is supposed to work?

It worked ! thanks !