Arduino function changes argument for no reason - c++

I wrote a class for Aduino, for reading an ads124x, and I am having one major problem, I can't call a function defined by the class. Here is how it is structured:
.ino calls functions using . operator
.h contains register map and class definition
.cpp contains all functions of the class
so far here is what is happening the .ino is successfully calling a function in .cpp, but when that same function calles another in .cpp it fails to call.
.ino:
#include "ADS124X.h"
void setup(){
ADS124X ADS124X(1,2,3,4);
Serial.begin(9600);
ADS124X.setUP(0x20, 0x20);}
.h:
class ADS124X{
public:
void reset(void);
void setUP(unsigned char* mux1, unsigned char* sys0);
private:
void SPI_Write(unsigned char* data, unsigned char size);
}
.cpp:
void ADS124X::setUP(unsigned char * mux1, unsigned char * sys0)
{
Serial.println("hi"); //prints this
delay(1);
reset(); // stops here
Serial.println("hi"); // doesn't print this
delay(1);
stopDataCont();
delay(210);
setREG(MUX1, mux1, 1);
setREG(SYS0, 0x01, 1);
delay(1);
}
void ADS124X::reset(void)
{
unsigned char dataToSend[] = { RESET };
START_HIGH;
CS_LOW;
Serial.println(RESET); // prints this as 0x06 (correct value)
SPI_Write(dataToSend, 1); // Seems to stop here
START_LOW;
CS_HIGH;
}
void ADS124X::SPI_Write(unsigned char * data, unsigned char size)
{
Serial.println("SPI_Write"); //prints this
for (unsigned int i = 0; i < size; i++) {
Serial.println("SPI_Write"); //prints this
Serial.println(* data); //prints this as 126 (if RESET is 3 * data becomes 189...)
SPI.transfer(*data);
Serial.println("SPI_Write");
data++;
}
}

This is not right...
Serial.println(RESET); // println expects a null terminated string
// you are sending a char.
You should define dataToSend as a null-terminated char array.
void ADS124X::reset(void)
{
char dataToSend[] = { RESET, 0 };
// ...
serial.println(dataToSend); // maybe println "reset" would be better?
SPI_Write(dataToSend, 1);
//...
}
Don't expect to see a nice 0x06 on your serial monitor, since that is not a printable character.

Related

PlatformIO Unidentified reference error to defined attributes

I am creating a project using PlatformIO and a Nodemcuv2 micro-controller.
I have written a class for serial communication SerialCommunicationHandler. This class ICommunicationHandler implements a Interface. See the code below.
ICommunicationHandler.h
class ICommunicationHandler {
public:
virtual void sendTemperature(float temp) = 0;
virtual void receiveData() = 0;
virtual void update() = 0;
protected:
virtual void parseData() = 0;
virtual void showParsedData() = 0;
};
SerialCommunicationHandler
headerfile
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
bool receiving;
bool newData;
void parseData() override;
void receiveData() override;
void showParsedData() override;
public:
explicit SerialCommunicationHandler();
void sendTemperature(float temp) override;
void update() override;
};
.cpp
#include <Arduino.h>
#include "SerialCommunicationHandler.h"
SerialCommunicationHandler::SerialCommunicationHandler() {
messagePosition = 0;
receiving = false;
newData = false;
}
void SerialCommunicationHandler::receiveData() {
//check if there are bytes in the serial buffer.
while (Serial.available() > 0){
char inByte = Serial.read();
//check if the byte is a starting or ending character;
switch (inByte) {
case '<':
//start receiving characters
receiving = true;
break;
case '>':
//stop receiving and parse the incoming data.
receiving = false;
newData = true;
strcpy(receivedData, incomingData); //copy incoming data into receivedData for further parsing.
memset(incomingData, 0, sizeof(incomingData)); //resetting incomingData.
messagePosition = 0;
break;
default:
if (receiving) {
incomingData[messagePosition] = inByte; //add incoming byte to array.
messagePosition++;
}
break;
}
}
}
void SerialCommunicationHandler::parseData() {
if (newData) {
showParsedData();
}
newData = false;
}
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
void SerialCommunicationHandler::sendTemperature(float temp) {
Serial.println(temp);
}
void SerialCommunicationHandler::update() {
receiveData();
parseData();
}
When building I get multiple undefined refernce errors:
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler14s
howParsedDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12receivedDataE`
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandlerC2E
v+0x4): undefined reference to `_ZN26SerialCommunicationHandler15messagePositionE'
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler11r
eceiveDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12incomingDataE'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nodemcuv2\firmware.elf] Error 1
I have checked my code multiple times for syntax errors or misspelling but have found nothing. My IDE doesnt bring up any errors as well. Any information on what might be causing the undefined reference error is welcome.
headerfile
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
// ... etc
These are only declarations of the static member variables. You also have to define them in the cpp file, just like you do with the member functions:
.cpp file:
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
// static data members
char SerialCommunicationHandler::incomingData[6] = {};
char SerialCommunicationHandler::receivedData[6] = {};
unsigned int SerialCommunicationHandler::messagePosition = 0;

Arduino progmem reads back garbled data

I'm working on a small HTTP server. I am building a router and since there could be quite a few routes, I wanted to put them into flash memory so that I don't have to use the valuable SRAM. However either I don't understand something correctly or something weird is happening since I can't seem to be able to read back my stored data from flash.
I have a struct which contains a function pointer and a char pointer. I want to store an array of these structs into flash and read them back. However with a small debug print I can see I can't read back the char pointer correctly. It prints garbish to the serial port.
Here is a small example.
#include <avr/pgmspace.h>
typedef struct {
void (*func)();
const char *URI;
} Route;
void test1() {
Serial.println("Executed testfunc1");
}
void test2() {
Serial.println("Executed testfunc2");
}
const char route1URI[] PROGMEM = "/route1";
const Route route1 PROGMEM = {
test1,
route1URI
};
const char route2URI[] PROGMEM = "/route2";
const Route route2 PROGMEM = {
test2,
route2URI
};
const Route routingTable[] PROGMEM = {
route1,
route2
};
void (*getRoute(char *URI))() {
Route *r = (Route *)pgm_read_word(routingTable + 0);
char *f = (char *)pgm_read_word(r->URI);
Serial.println(f);
return r->func;
}
void setup() {
Serial.begin(9600);
while (!Serial) { }
Serial.println("started setup");
void (*fn)() = getRoute("sometest");
// will cause errors if called
//fn();
Serial.println("ended setup");
}
void loop() {
// put your main code here, to run repeatedly:
}
The PROGMEM is not that easy to use. And it can be little bit simplified:
#include <avr/pgmspace.h>
struct Route {
void (*func)();
const char *URI;
};
void test1() {
Serial.println(F("Executed testfunc1")); // if you are using progmem, why not for string literals?
}
void test2() {
Serial.println(F("Executed testfunc2"));
}
const char route1URI[] PROGMEM = "/route1";
const char route2URI[] PROGMEM = "/route2";
const Route routingTable[] PROGMEM = {
{test1,route1URI},
{test2,route2URI}
};
void (*getRoute(char *URI))() {
Route r;
memcpy_P((void*)&r, routingTable, sizeof(r)); // read flash memory into the r space. (can be done by constructor too)
Serial.println((__FlashStringHelper*)r.URI); // it'll use progmem based print
// for comparing use: strcmp_P( URI, r.URI)
return r.func; // r.func is already pointer to the function
}
void setup() {
Serial.begin(57600);
while (!Serial) { }
Serial.println("started setup");
void (*fn)() = getRoute("sometest");
// will cause errors if called
//fn();
Serial.print((uint16_t)test1, HEX); Serial.print(' ');
Serial.print((uint16_t)test2, HEX); Serial.print(' ');
Serial.println((uint16_t)fn, HEX);
Serial.println("ended setup");
}
void loop() {
// put your main code here, to run repeatedly:
}
I suppose route1 and route2 might cause all the troubles as it was used for copy into the routingTable. If you initialize elements of routingTable as I did, it works much better. And also getRoute was broken a lot.
Anyway, if you have flash string, you can use also String str {(__FlashStringHelper*)r.URI}; and then use compare operator: str == URI:
#include <avr/pgmspace.h>
// get size of array[]
template<typename T, int size> int GetArrLength(T(&)[size]){return size;}
struct Route {
void (*func)();
const char *URI;
};
void test1() {
Serial.println(F("Executed testfunc1")); // if you are using progmem, why not for string literals?
}
void test2() {
Serial.println(F("Executed testfunc2"));
}
void test3() {
Serial.println(F("Executed testfunc3"));
}
const char route1URI[] PROGMEM = "/route1";
const char route2URI[] PROGMEM = "/route2";
const char route3URI[] PROGMEM = "/route3";
const Route routingTable[] PROGMEM = {
{test1,route1URI},
{test2,route2URI},
{test3,route3URI}
};
void (*getRoute(char *URI))() {
for (int8_t i = 0; i < GetArrLength(routingTable); ++i) {
Route r;
memcpy_P((void*)&r, routingTable+i, sizeof(r)); // read flash memory into the r space. (can be done by constructor too)
String uri {(__FlashStringHelper*)r.URI};
if (uri == URI) {
return r.func; // r.func is already pointer to the function
}
}
return nullptr;
}
void setup() {
Serial.begin(57600);
while (!Serial) { }
Serial.println("started setup");
void (*fn)() = getRoute("/route3");
// will cause errors if called
//fn();
Serial.print((uint16_t)test1, HEX); Serial.print(' ');
Serial.print((uint16_t)test2, HEX); Serial.print(' ');
Serial.print((uint16_t)test3, HEX); Serial.print(' ');
Serial.println((uint16_t)fn, HEX);
Serial.println("ended setup");
}
char *f = (char *)pgm_read_word(r->URI);
Serial.println(f);
f is a pointer to a character array in PROGMEM, but Serial.println doesn't know that! It ends up trying to read the string from RAM, where it isn't.
The Arduino Serial library doesn't appear to support strings in PROGMEM. You will need to loop over the string printing one character at a time, use another library, or store the string in RAM.
As pointed by #KIIV, it's better to specify Route directly inside the declaration of routingTable. As an alternative solution, you could redefined the struct Route to
typedef struct {
void (*func)();
char URI[16]; //adjust the size to your need
} Route;
In this way, reading both URI and function address from flash can be done by single call to memcpy_P. The complete codes:
typedef struct {
void (*func)();
char URI[16]; //adjust the size to your need
} Route;
void test1() {
Serial.println("Executed testfunc1");
}
void test2() {
Serial.println("Executed testfunc2");
}
const Route routingTable[] PROGMEM = {
{test1, "/route1"},
{test2, "/route2"}
};
void (*getRoute(char *URI, int idx))() {
Route r;
memcpy_P(&r, &routingTable[idx], sizeof(Route));
Serial.print(idx); Serial.println(". -----------------------------");
Serial.print("Route: "); Serial.println(r.URI);
Serial.print("fn address: "); Serial.println((uint16_t)r.func, HEX);
Serial.print("test1 address: "); Serial.println((uint16_t)test1, HEX);
Serial.print("test2 address: "); Serial.println((uint16_t)test2, HEX);
return r.func;
}
void setup() {
Serial.begin(9600);
while (!Serial) { }
Serial.println("started setup");
void (*fn)();
const int n = sizeof(routingTable) / sizeof(Route);
for (int i = 0; i < n; i++) {
fn = getRoute("sometest", i);
fn();
}
Serial.println("ended setup");
}
void loop() {
// put your main code here, to run repeatedly:
}

C++ - one function makes others malfunction

Here is my task:
Write class Word which has:
pointer on array of characters
constructors and destructors
function to read word
function to check if character which is passed to it as argument occurs in word and return position of occurance
function to check which of two words has more occurances of number 10 and to return that number of occurances
Here is my solution. I compiled it without errors but it doesn't work as it shoud.
#include <iostream>
#include <string.h>
using namespace std;
class Word
{
private:
char *content;
int length;
public:
Word();
Word(char *);
~Word();
void print_content(void);
int check_character(char);
friend int check_number(Word,Word);
};
Word::Word()
{
}
Word::Word(char *n)
{
length=strlen(n);
content=new char [length];
for(int i=0;i<length;i++)
{
content[i]=n[i];
}
}
Word::~Word()
{
delete content;
}
void Word::print_content(void)
{
for(int i=0;i<length;i++)
{
cout<<""<<content[i];
}
}
int Word::check_character(char a)
{
int position=0;
for(int i=0;i<length;i++)
{
if(content[i]==a)
{
position=i+1;
}
}
if(position>0)
{
return position;
}
else return 0;
}
int check_number(Word n,Word m)
{
int counter_n=0;
int counter_m=0;
for(int i=1;i<n.length;i++)
{
if((n.content[i-1]=='1')&&(n.content[i]=='0'))
{
counter_n=counter_n+1;
}
}
for(int i=1;i<m.length;i++)
{
if((m.content[i-1]=='1')&&(m.content[i]=='0'))
{
counter_m=counter_m+1;
}
}
if(counter_n>counter_m)
{
return counter_n;
}
else if(counter_n<counter_m)
{
return counter_m;
}
else return 0;
}
int main()
{
char characters1[]="qwerty10",*p1,*p2;
char characters2[]="stackoverflow101010";
p1=characters1;
p2=characters2;
Word first(p1);
Word second(p2);
cout<<""<<first.check_character('q')<<endl;
cout<<""<<second.check_character('f')<<endl;
//cout<<""<<check_number(first,second)<<endl;
first.print_content();
second.print_content();
}
Function check_number(first,second) for some reason makes other functions to work incorrectly, if you call it by removing "//" you will see that first.print_content() and second.print_content() don't give us correct result. Or if function first.check_character('r') is first called, second.check_character('j') second called and then check_number(first,second), then two firsly called functions don't work.
What's reason for this strange behaviour?
Word objects are passed by copy to check_number, but you did not define the copy constructor. So default one is used by the compiler and this one copies pointer (char* content). Temporary objects passed to the function are pointing to data created first and second in the main function...upon deletion (temprary objects are deleted after the function is called), they delete the pointers, so first and second objects are pointing to deleted memory. You have undetermined behaviour here, this explains side effects you experienced.
Passing objects by reference to check_number is an easy way to fix your problem. Here is a working code (including many fixes because you did not access arrays correctly):
#include <iostream>
using namespace std;
#include <iostream>
#include <string.h>
using namespace std;
class Word
{
private:
char *content;
int length;
public:
Word();
Word(char *);
~Word();
void print_content(void);
int check_character(char);
friend int check_number(const Word&,const Word&); // changed here
};
Word::Word()
{
}
Word::Word(char *n)
{
length=strlen(n);
content=new char [length];
for(int i=0;i<length;i++)
{
content[i]=n[i]; // changed here
}
}
Word::~Word()
{
delete [] content; // changed here
}
void Word::print_content(void)
{
for(int i=0;i<length;i++)
{
cout<<""<<content[i]; // changed here
}
}
int Word::check_character(char a)
{
int position=0;
for(int i=0;i<length;i++)
{
if(content[i]==a) // changed here
{
position=i+1;
}
}
if(position>0)
{
return position;
}
else return 0;
}
int check_number( const Word& n, const Word& m)// changed here
{
int counter_n=0;
int counter_m=0;
for(int i=1;i<n.length;i++)
{
if((n.content[i-1]=='1')&&(n.content[i]=='0')) // changed here
{
counter_n=counter_n+1;
}
}
for(int i=1;i<m.length;i++)
{
if((m.content[i-1]=='1')&&(m.content[i]=='0')) // changed here
{
counter_m=counter_m+1;
}
}
if(counter_n>counter_m)
{
return counter_n;
}
else if(counter_n<counter_m)
{
return counter_m;
}
else return 0;
}
int main()
{
char characters1[]="qwerty10",*p1,*p2;
char characters2[]="stackoverflow101010";
p1=characters1;
p2=characters2;
Word first(p1);
Word second(p2);
cout<<""<<first.check_character('q')<<endl;
cout<<""<<second.check_character('f')<<endl;
cout<<""<<check_number(first,second)<<endl;
first.print_content();
second.print_content();
}
This outputs:
1
10
3
qwerty10stackoverflow101010
Declaring a copy constructor is another way to fix the problem:
Word( const Wodr& word ) :
length( word.length ),
content( new char[word.length] )
{
memcpy( content, word.content, word.length );
}
That would be less efficient than passing objects by const reference, but would make your code safer (it's good to always declare copy constructor to prevent bug you experienced here).
Finally, if you are lazy, you can also declare the copy constructor as private, to prevent compiler to copy objects, just declare it, don't impelment it:
class Word
{
....
private:
Word( const Word& word ); // this makes argument passed by copy impossible.
};
Then, compiler will not let you call check_number.

C++ abstraction of libpng causes Crash in malloc -- While deleting a "Jagged" 2D Array

The class corresponding to this crash is:
#ifndef IMAGE_DATA_
#define IMAGE_DATA_
#include <stdexcept>
template <typename data_type>
class ImageData
{
public:
ImageData(unsigned long width, unsigned long height);
~ImageData();
data_type **&get_data();
unsigned long int get_width() const
{
return _m_Width;
}
unsigned long int get_height() const
{
return _m_Height;
}
protected:
ImageData(ImageData &copy);
ImageData& operator= (ImageData &copy);
private:
data_type **_m_rData;
unsigned long _m_Width;
unsigned long _m_Height;
};
template <typename data_type>
ImageData<data_type>::ImageData(unsigned long width, unsigned long height) :
_m_rData(NULL),
_m_Width(width),
_m_Height(height)
{
if (width == 0 || height == 0)
throw std::runtime_error("Invalid width or height");
try {
_m_rData = new data_type*[_m_Height]();
for (unsigned long int i = 0; i < _m_Height; ++i) {
_m_rData[i] = NULL;
}
for (unsigned long int i = 0; i < _m_Height; ++i) {
_m_rData[i] = new data_type[_m_Width];
}
}
catch (std::bad_alloc e) {
throw std::runtime_error("Failure to create space for Image");
}
}
template <typename data_type>
ImageData<data_type>::~ImageData()
{
for (unsigned long i = 0; i < _m_Height; ++i) {
delete [] _m_rData[i];
_m_rData[i] = NULL;
}
delete [] _m_rData;
_m_rData = NULL;
}
template <typename data_type>
data_type **&ImageData<data_type>::get_data()
{
return _m_rData;
}
#endif
And it is used in the following manner:
PNGFileReader::PNGFileReader(const std::string &path) :
_m_Image(NULL),
_m_pPNG(NULL),
_m_pPNGInfo(NULL)
{
...
/*
* Read Image in all at once into users data
*/
_m_Image = new ImageData<unsigned char>(width, height);
png_read_image(_m_pPNG, _m_Image->get_data());
png_read_end(_m_pPNG, NULL);
fclose(_m_CFilePointer);
_m_CFilePointer = NULL;
}
PNGFileReader::~PNGFileReader()
{
if (_m_CFilePointer) {
fclose(_m_CFilePointer);
}
png_destroy_read_struct(&_m_pPNG, &_m_pPNGInfo, NULL);
delete _m_Image;
}
When stepping through with the debugger the _m_rData in the ImageData class is the same pointer as when I used new on it. I have even tried to wrap the delete statement inside ImageData destructor with if == NULL statments. However, I still get a sigabrt while running my code. The stack trace from gdb is:
0 __GI_raise raise.c 64 0x3512a36285
1 __GI_abort abort.c 91 0x3512a37b9b
2 __libc_message libc_fatal.c 198 0x3512a77a7e
3 malloc_printerr malloc.c 5021 0x3512a7dda6
4 _int_free malloc.c 3942 0x3512a7f08e
5 ImageData<unsigned char>::~ImageData imagedata.h 57 0x40236d
6 PNGFileReader::~PNGFileReader pngfilereader.cpp 59 0x401ed3
7 main main.cpp 8 0x40246a
UPDATE
For anyone that is curios the following now works. Apparently it is an issue with how png_alligns its data. This forces you I guess to use libpng's method calls which internally use free and malloc, not new. This is essentially the same things as calling free(data) where data was created with data = new type[N]. The code below depicts how to correctly use libpng.
#ifndef PNG_FILE_READER_H_
#define PNG_FILE_READER_H_
#include "imagedata.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
#include <iostream>
#include <vector>
#include <string>
template <typename data_type>
class ImageData;
class PNGFileReader
{
public:
// Ctor and Dtor
PNGFileReader(const std::string &path);
~PNGFileReader();
// For testing purposes
friend std::ostream &operator<< (std::ostream &out,
PNGFileReader *object)
{
for (unsigned long i = 0; i < object->get_image_height(); ++i) {
for (unsigned long j = 0; j < object->get_image_width(); ++j) {
png_byte c = object->_m_ImageData[i][j];
out << c;
}
}
return out;
}
// Getters
long unsigned int get_image_width() const;
long unsigned int get_image_height() const;
private:
// Helper functions:
bool _create_png_structs();
// Member variables:
FILE *_m_CFilePointer;
unsigned long int _m_ImageWidth;
unsigned long int _m_ImageHeight;
png_bytepp _m_ImageData;
png_structp _m_pPNG;
png_infop _m_pPNGInfo;
// Enums
enum PNGBOOL {NOT_PNG, PNG};
enum PNGERRORS {ERROR, SUCCESS};
};
#endif /* PNG_FILE_READER_H_ */
#include "pngfilereader.h"
#include "filereader.h"
#include <stdexcept>
PNGFileReader::PNGFileReader(const std::string &path) :
_m_ImageData(NULL),
_m_pPNG(NULL),
_m_pPNGInfo(NULL)
{
/*
* Check if first 8 bytes are the correct PNG header
*/
enum {BYTES_TO_READ = 8};
unsigned char sig[BYTES_TO_READ];
FileReader(path, sig, BYTES_TO_READ);
bool not_png = png_sig_cmp(sig, 0, BYTES_TO_READ);
if (not_png) {
throw std::runtime_error("Your file is not of PNG format");
}
/*
* Create the png structs using a FILE *. libpng requires
* this type and will not take a C++ stream
*/
_m_CFilePointer = fopen(path.c_str(), "rb");
if (!_m_CFilePointer) {
throw std::runtime_error("Failure to open PNG file");
}
if (!_create_png_structs()) {
throw std::runtime_error("Failure to create PNG structs");
}
/*
* Initialize PNG io and read data into PNG structs
*/
png_init_io(_m_pPNG, _m_CFilePointer);
png_read_info(_m_pPNG, _m_pPNGInfo);
_m_ImageHeight = png_get_image_height(_m_pPNG, _m_pPNGInfo);
_m_ImageWidth = png_get_rowbytes(_m_pPNG, _m_pPNGInfo);
/*
* Create sufficient PNG Space and Read Image in all at
* once into users data. Note that you have to use png's
* types to prevent sigabrt (6) while freeing memory.
*/
_m_ImageData = (png_bytepp)png_malloc(_m_pPNG,
sizeof(png_bytep)*_m_ImageHeight);
if (_m_ImageData == NULL) {
throw std::runtime_error("Memory allocation failure");
}
for (unsigned long int i = 0; i < _m_ImageHeight; ++i) {
_m_ImageData[i] = NULL;
}
for (unsigned long int i = 0; i < _m_ImageHeight; ++i) {
_m_ImageData[i] = (png_bytep)png_malloc(_m_pPNG,
sizeof(png_byte)*_m_ImageWidth);
if (_m_ImageData[i] == NULL) {
throw std::runtime_error("Memory allocation failure.");
}
}
png_read_image(_m_pPNG, _m_ImageData);
png_read_end(_m_pPNG, NULL);
fclose(_m_CFilePointer);
_m_CFilePointer = NULL;
}
PNGFileReader::~PNGFileReader()
{
if (_m_CFilePointer) {
fclose(_m_CFilePointer);
}
/*
* Free all resources (-1)
*/
png_free_data(_m_pPNG, _m_pPNGInfo, PNG_FREE_ALL, -1);
for (unsigned long int i = 0; i < _m_ImageHeight; ++i) {
png_free(_m_pPNG, _m_ImageData[i]);
}
free(_m_ImageData);
png_destroy_read_struct(&_m_pPNG, &_m_pPNGInfo, NULL);
}
// Getters
long unsigned int PNGFileReader::get_image_width() const
{
return _m_ImageWidth;
}
long unsigned int PNGFileReader::get_image_height() const
{
return _m_ImageHeight;
}
// Private helper functions
bool PNGFileReader::_create_png_structs()
{
/*
* Create the pointer to main libpng struct, as well as
* two info structs to maintain information after, and
* prior to all operations on png m_Data. Only necessary
* to release resource after function succeeds.
*/
_m_pPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
NULL, NULL);
if (!_m_pPNG){
return PNGFileReader::ERROR;
}
_m_pPNGInfo = png_create_info_struct(_m_pPNG);
if (!_m_pPNGInfo) {
return PNGFileReader::ERROR;
}
return PNGFileReader::SUCCESS;
}
If you need a really 2D array to pass to a library, but want to have the flexibility of a jagged array, what you do is
Allocate the first level pointer block as usual
Instead of allocating m separate rows of n cells (one for each pointer in the first level block) you allocate a single set of n*m cells and then set the first level pointers to point at every nth location. This way the main allocation is sized and laid out in memory just as a 2D array, but you can still use the two-pointer-dereference [][] syntax to get to the cells.
Pass the start of the second level allocation to the library.
This works because there are strict requirements on who multidimensional arrays are laid out in memory (i.e. the must be contiguous at every level of interpretation).

Optimal technique to parse a real-time char* data received from a buffer for text content in C++

I have created a live continuous mjpeg stream. A crude illustration is like this
....[image (jpeg)]->[text "content-length"]->[image (jpeg)]->[text "content-length"]->....
As you can see I receive data from gstreamer media pipe line which contains image and my own injected text
(Note: Although I am using Gstreamer, my question is only related to C++ principles.)
In order to parse this real-time data, I am trying to receive and push it into the queue. Subsequently I plan to parse the data for the word "content-length" after queue contains a certain number of packets.
My code looks like the following:
void clear( std::queue<char> &q )
{
std::queue<char> empty;
std::swap( q, empty );
}
static GstFlowReturn new_buffer (GstAppSink *app_sink, gpointer user_data)
{
GstBuffer* buffer = gst_app_sink_pull_buffer(app_sink);
//create queue
std::queue<char> q;
g_print("The input buffer contents are\n");
gint i=0;
for(i=0; buffer->data[i];i++)
{
//g_print("\n%d",i);
q.push(buffer->data[i]);
}
//g_print("\nsize of inbuf is %d\n",GST_BUFFER_SIZE(buffer));
g_print("\n");
gst_buffer_unref(buffer);
//#####################
//parsing method here???
//#####################
clear(q);
return GST_FLOW_OK;
}
I have used circular queues/ ring buffer in C/C++ before. Is that the best option? Or is the C++ STL queues would be more appropriate in this scenario like above?
I ended up using ringbuffer class
In header file declare
//queue size
enum { rb_size = 5 }; // ---->element1 -> element2 -> .... -> elementN -> gap ->
// ^ |
// | |
// <--------------------<------------------<-------------V
typedef struct
{
char * data[rb_size];
int head, tail;
} ring_buffer_struct;
namespace myspace{
class ring_buffer{
private:
protected:
public:
//========= constructor ============
ring_buffer()
{
//If necessary initialization can happen here.
}
//========== destructor =============
virtual ~ring_buffer()
{
}
//===================================
virtual void rb_start(ring_buffer_struct *b);
virtual bool rb_empty(ring_buffer_struct const *b);
virtual char * rb_front(ring_buffer_struct const *b);
virtual char * rb_rear(ring_buffer_struct const *b);
virtual void rb_pop_front(ring_buffer_struct *b);
virtual ring_buffer_struct* rb_push_back(ring_buffer_struct *b);
}; //end of class
}
In cpp file
//start
void myspace::ring_buffer::rb_start(ring_buffer_struct *b)
{
b->head = 0; b->tail = 0;
}
//clear
bool myspace::ring_buffer::rb_empty(ring_buffer_struct const *b)
{
return b->head == b->tail;
}
//front element
char * myspace::ring_buffer::rb_front(ring_buffer_struct const *b)
{
return b->data[b->head]; //data gets popped
}
//rear element
char * myspace::ring_buffer::rb_rear(ring_buffer_struct const *b)
{
return b->data[b->tail]; //data gets pushed
}
//pop out front element
void myspace::ring_buffer::rb_pop_front(ring_buffer_struct *b)
{
if(b->head < b->tail)
{
++b->head;
}
if(b->head > b->tail)
{
b->head = 0;
}
}
//push in rear element
ring_buffer_struct* myspace::ring_buffer::rb_push_back(ring_buffer_struct *b)
{
int new_tail = b->tail;
if (++new_tail >= rb_size)
{ //beginning of the queue
new_tail = 0;
}
if (new_tail != b->head)
{
//middle of the queue
b->tail = new_tail;
}
if (new_tail <= b->head)
{
b->tail = 0;
}
return b;
}
And to use in the main()
...
char element1[10] = "abcdefghi";
char element2[10] = "bcdefghij";
char element3[10] = "cdefghijk";
ring_buffer_struct rb;
myspace::ring_buffer q;
q.rb_empty(&rb); //make sure empty
q.rb_start(&rb); //start - initialize
//initialize
uint16_t i;
for(i=0;i<rb_size;i++)
{
rb.data[rb.tail] = (char *)"000000000";
q.rb_push_back(&rb);
}
rb.data[rb.tail] = element1;
q.rb_push_back(&rb);
q.rb_pop_front(&rb); //now parse
rb.data[rb.tail] = element2;
q.rb_push_back(&rb);
q.rb_pop_front(&rb); //now parse
...
For parsing: I looked at this post
Simple string parsing with C++
Off topic suggestion:
When using the swap trick to clear out an STL container, don't call std::swap explicitly, as you may end up not getting a better-optimized version. The better way is:
void clear( std::queue<char> &q )
{
std::queue<char> empty;
using std::swap;
swap( q, empty );
}
This allows the compiler to choose a specialized version of swap that's optimized for the type of container you're using. You could also try q.swap(empty);, but I'm not sure all STL implementations offer that.