Reusing pointers to parse strings, issue with free() & ptr = NULL

/I am trying to have my program reparse a string by reusing the pointers, I understand that free() is an import function to deallocate the memory of the ptrs but when I use free() it messes up the program, even more, when I compile it parses the string during the first iteration and first only after that it just prints NULL is it something I am doing with my dereferencing or Nulling of my token ptr or something, HELP./

#include <Adafruit_GPS.h>

#include <SoftwareSerial.h>

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

[Mod edit swear word removed]

int i=0, a;

float wind, temperature;

static const int RXPin = 3, TXPin = 2; //Wind : UNO, yellow(Tx+):D3, Green(Tx-):D2

static const uint32_t GPSBaud = 4800;

char *tokp=NULL, *winddir=NULL, *ref=NULL, *windmagraw=NULL, *wunit, *error=NULL, *status=NULL, *checksum=NULL, *type=NULL, *temp=NULL, *tunit=NULL;

char tw[50], WNMEA[] = "$IIMWV,226.0,R,000.0,N,A *0B", TNMEA[] = "$WIXDR,C,022.0,C,,*52", WMID[x]="$IIMWV", TMID[ ]="$WIXDR";

//we need to make the wind and temp vars int* to be able to compare the quantities

// The serial connection to the GPS device

//SoftwareSerial ss(RXPin, TXPin);

//Adafruit_GPS GPS(&ss);

void setup()

{

Serial.begin(9600); //this is matched the CV7-V outputs at 4800 Baud Rate

// ss.begin(GPSBaud); //BIGG TIME SERIAL PORT FOR

}

void loop()

{

/*the function below is for wind magnitude and direction parsing*/

//SSNMEA = ss.read();   //this is what it will really be

//tokp = strtok(SSNMEA, ","); //this is what it will really be

tokp = NULL;

tokp = strtok(WNMEA, ",");

Serial.println(tokp);

// tokp = strtok(TNMEA, ",");

if(strcmp(tokp, WMID)==0){    //"$IIMWV"

  winddir = strtok(NULL,",");

  Serial.print("Wind Direction in Degrees (000) : ");

  Serial.println(winddir);

  winddir = NULL;

  

  ref = strtok(NULL,",");

  Serial.println(ref);

  ref = NULL;

  

  windmagraw = strtok(NULL,",");

  Serial.print("Wind speed in Knots : ");

  wind = atof(windmagraw);   //any to float

  Serial.println(windmagraw);

  Serial.println(wind);

  windmagraw = NULL;

  

  wunit = strtok(NULL, ",");

  Serial.print("Wind speed UNIT [Knots = N] : ");

  Serial.println(wunit);

  wunit = NULL;

  

  error = strtok(NULL, ",");

  Serial.println(error);

  error = NULL;

  

  status = strtok(NULL, ",");

  Serial.println(status);

  status = NULL;

  

  checksum = strtok(NULL, ",");

  Serial.println(checksum);

  checksum = NULL;

}   

else if(strcmp(tokp, TMID)==0){    //$WIXDR

  type = strtok(NULL,",");

  Serial.print("Type of Transducer (C=CV7-V) : ");

  Serial.println(type);

  type = NULL;

  tokp = NULL;

  temp = strtok(NULL,",");

  Serial.print("The Temperature in DegC : ");

  temperature = atof(temp);     //any to flaot

  Serial.println(temp);   //print the string version 

  Serial.println(temperature);    //print the float version that is in my temperature var

  temp = NULL;

  temperature = NULL;

  tunit = strtok(NULL,",");

  Serial.print("Unit of Temp (DegC) : ");

  Serial.println(tunit);

  tunit = NULL;

  checksum = strtok(NULL, ",");

  Serial.println(checksum);

  checksum = NULL;

  Serial.println(tokp);

  tokp = NULL;

  Serial.println(tokp);

  free(tokp);

}

else

  tokp = NULL;

delay(3000);

}

free() deallocates the memory pointed to by the pointer, not the memory of the pointer itself. The memory that the pointer points to must be allocated by malloc() and friends. You cannot call free() on just any pointer. See the documentation for free(): std::free - cppreference.com

You're not calling malloc() anywhere in your code, so you shouldn't call free() either.
In fact, unless you're developing a low-level memory allocator or data structure, don't use malloc and free in C++.

You seem to be calling free() on a null pointer, which has no effect. As a side note, don't use NULL, use nullptr instead.


Please surround your code with code fences so that it's readable, and explain why you think you need to use free().

looks like you process a string depending on a keyword.

consider the following which tokenizes a string allowing the substrings to be more easily be looked at

#define MAX_TOKS   10
char * toks [MAX_TOKS];
float  vals [MAX_TOKS];

int
tokenize (
    char *t )
{
    char s [80];
    int  i = 0;

    printf ("%s: %s\n", __func__, s);

    strcpy (s, t);

    for (toks [i] = strtok (s, ","); toks [i]; )  {
        vals [i] = atof (toks [i]);
        toks [++i] = strtok (NULL, ",");
    }

    return i;
}

// -----------------------------------------------------------------------------
const char t [] = "12.3,23,45.6,345,67";

void
setup (void)
{
    Serial.begin (9600);

    int nTok = tokenize ((char*) t);

    for (int n = 0; n < nTok; n++)
        Serial.println (vals [n]);
}

void
loop (void)
{
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.