So i am learning C++ right now and i am pretty stuck right now. I have been researching in the Internet Form some time now, but could Not find a solution to my Problem. Maybe because i did not know what to look after..
And sry for the maybe misleading title, but here is the question:
Lets say I have a struct called Data with an int Array and some other members
struct Data{
...
uint8_t values [];
}
Now i have a Method test
void test (uint8_t *buffer, size_t buffer_size)
{
...
}
In that method i make an instance of Data and i want to assign the value stored in the buffer to that Array of the instance of Data and i have no Idea how to so this.
Not sure, but maybe i need a
Pointer-Pointer?
Thanks in advance!
Cheers
In c++ you can use the STL std::vector<>. You can declare it in :
struct Data{
...
std::vector<uint8_t> values;
}
Then in the Method Test :
void test (uint8_t *buffer, size_t buffer_size)
{
...
struct Data istanceData;
for (int i=0 ; i < buffer_size; ++i){
istanceData.values.push_back(buffer[i]);
}
}
Remember to dinamically allocate Struct Data (and return it's pointer) if you want to use it outside "test" function.
You can also pass an extra parameter to the "test" function and pass the istanced Struct Data with reference in this way:
void test (uint8_t *buffer, size_t buffer_size, struct Data &instanceData)
{
...
for (int i=0 ; i < buffer_size; ++i){
istanceData.values.push_back(buffer[i]);
}
}
Related
I have a class foo that manages data using small buffer optimization (SBO).
When size < 16, the data is held locally (in buffer), otherwise it is stored on the heap, with reserved holding the allocated space.
class foo {
static const int sbo_size = 16;
long size = 0;
char *ptr;
union {
char buffer[sbo_size];
long reserved;
};
public:
foo()
{
for (int i = 0; i < sbo_size; ++i)
buffer[i] = 0;
}
void clone(const foo &f)
{
// release 'ptr' if necessary
if (f.size < sbo_size)
{
memcpy(this, &f, sizeof(foo));
ptr = buffer;
} else
{
// handle non-sbo case
}
}
};
Question about clone():
With the SBO case, it may not be clear for the compiler that union::buffer will be used.
is it correct to use memcpy and set ptr accordingly?
If you can use C++17, I would side-step any potential type-punning problems by using std::variant in place of a union.
Although this uses a small amount of storage internally to keep track of the current type it contains, it's probably a win overall as your ptr variable can disappear (although that should be inside your union anyway).
It's also typesafe, which a union is not (because std::get will throw if the variant doesn't contain the desired type) and will keep track of the type of data it contains simply by assigning to it.
The resulting class fragment might look something like this (no doubt this code can be improved):
class foo
{
private:
static const size_t sbo_size = 16;
using small_buf = std::array <char, sbo_size>;
size_t size = 0;
std::variant <small_buf, char *> buf = { };
public:
void clone (const foo &f)
{
char **bufptr = std::get_if <char *> (&buf);
if (bufptr)
delete [] *bufptr;
size = f.size;
if (size < sbo_size)
buf = std::get <small_buf> (f.buf);
else
{
buf = new char [size];
std::memcpy (std::get <char *> (buf), std::get <char *> (f.buf), size);
}
}
};
Notes:
You will see that I've used std::array instead of a C-style array because std:array has lots of nice features that C-style arrays do not
Why clone and not a copy constructor?
if you want foo to have an empty state (after being default constructed, say), then you can look into the strangely named std::monostate.
For raw storage, std::byte is probably to be preferred over char.
Fully worked example here.
Edit: To answer the question as posed, I am no language lawyer but it seems to me that, inside clone, the compiler has no clue what the active member of f might be as it has, in effect, been parachuted in from outer space.
In such circumstances, I would expect compiler writers to play it safe and set the active member of the union to "don't know" until some concrete information comes along. But (and it's a big but), I wouldn't like to bet my shirt on that. It's a complex job and compiler writers do make mistakes.
So, in a spirit of sharing, here's a slightly modified version of your original code which fixes that. I've also moved ptr inside your union since it clearly belongs there:
class foo {
static const int sbo_size = 16;
long size = 0;
union {
std::array <char, sbo_size> buffer; // changing this
char *ptr;
long reserved;
};
public:
foo()
{
for (int i = 0; i < sbo_size; ++i)
buffer[i] = 0;
}
void clone(const foo &f)
{
// release 'ptr' if necessary
if (f.size < sbo_size)
{
buffer = f.buffer; // lets me do this
ptr = buffer.data ();
} else
{
// handle non-sbo case
}
}
};
So you can see, by using std::array for buffer (rather than one of those hacky C-style arrays), you can directly assign to it (rather than having to resort to memcpy) and the compiler will then make that the active member of your union and you should be safe.
In conclusion, the question is actually rather meaningless since one shouldn't (ever) need to write code like that. But no doubt someone will immediately come up with something that proves me wrong.
I have several structs containing various combinations of parameters. I wrote a function that takes a void pointer reference to the structs (there are several different types), and writes information from the data parameter into them. Here's the function below:
void SubModelBase::writeDataStruct(byte msgID, void *ptr, QByteArray data)
{
*(byte*)ptr[0] = msgID;
*(byte*)ptr[1] = data.length();
for (int i = 2; i < data.length(); i++)
{
*(byte*)ptr[i] = data.at(i);
}
}
The void *ptr is the reference to the struct that I want to write the data into but I'm having issues deferencing the pointer so I can write into it. I'm sure I'm just missing something silly in my syntax here but I'm not seeing it at the moment...
EDIT:
Ok, I rewrote the function to first cast the void pointer to a byte pointer before assigning values:
void SubModelBase::writeDataStruct(byte msgID, void *ptr, QByteArray data)
{
byte* structData = (byte*)ptr;
structData[0] = msgID;
structData[1] = data.length();
for (int i = 0; i < data.length(); i++)
{
structData[i + 2] = data.at(i);
}
}
Not tested yet but now it at least compiles. I did it this way because there are over 30-40 different structs that need to have data filled in them and I needed a single function that can handle the operation easily without knowing the details of each struct. However, if there is a better way to approach the problem, I'm definitely open to ideas.
EDIT 2: Fixed logic error in loop
What you seem to be looking for is a template so the compiler creates the correct method when you need it.
im trying to create a class which has malloc.
the class has internal struct.
the user will have pointer to the struct but he may not know about the struct or even care about it.
he must save the pointer and some functions will require the address of that struct.
so in the header of the library i did the following:
#define EELS_MAX_SLOTS 5
class EELS
{
typedef struct{
//struct difinition ...
}ee_slot_t;
public:
EELS();
uint8_t CreateSlot(uint16_t begin_addr, uint16_t length, uint8_t data_length);
~EELS();
protected:
private:
void* _slot_arr[EELS_MAX_SLOTS];
uint8_t _slot_counter;
};
and the code in the execution file:
// default constructor
EELS::EELS()
{
_slot_counter =0;
} //EELS
uint8_t EELS::CreateSlot(uint16_t begin_addr, uint16_t length, uint8_t data_length){
if (_slot_counter > EELS_MAX_SLOTS)
return NULL;
ee_slot_t* slot_p;
slot_p = malloc(sizeof(ee_slot_t))
if (!slot_p)
return NULL;
slot_p->begining = begin_addr;
slot_p->length = length;
slot_p->counter = 0; // TODO...init...
slot_p->position = 0; // TODO...init...
_slot_arr[_slot_counter] = (void*)slot_p;
_slot_counter++;
return _slot_counter;
}
// default destructor
EELS::~EELS()
{
for (int i=0; i<_slot_counter; i++)
{
free((ee_slot_t*)_slot_arr[i]);
}
}
as you can see im returning index of pointers array.. so (1-6) in this case and I'm saving the real address inside that pointers array.
but from what you see. is this safe? the free method and malloc.. there is some mistake or memory leakage?
why not vector?
because its for embedded system and the current IDE/toolchain im using doesnt support std:vectors.
What happens when _slot_counter == EELS_MAX_SLOTS.
So I think you should change the if statement
if (_slot_counter > EELS_MAX_SLOTS)
return NULL;
to
if (_slot_counter >= EELS_MAX_SLOTS)
return 0; // return type is uint8_t, not a pointer
I have the following structure, class and function snippet:
structure:
struct myData
{
short index;
char name[32];
}
class:
class myFoo
{
...
public:
short count;
myData** data;
...
}
function:
int Do_Bar(myFoo vFoo)
{
...
myData* data = *vFoo.data;
for (short i=0; i<vFoo.count; ++i)
{
Printf("%3d %s", data.index, data.name);
}
...
}
function call:
...
myFoo foo;
SomeAPI_GetCompleteObjectList(&foo);
Do_Bar(foo);
...
But my code crashes with these code. But if I removed the parameter and create a myFoo class in Do_Bar() function instead, the code works fine:
int Do_Bar(myFoo vFoo)
{
myFoo foo;
SomeAPI_GetCompleteObjectList(&foo);
...
myData* data = *vFoo.data;
for (short i=0; i<vFoo.count; ++i)
{
Printf("%3d %s", data.index, data.name);
}
...
}
Why is it? And how to resolve this?
EDIT1:
I forgot to mention that the initializations of foo is done before the function call. This was initialized using an API.
I modified the code for this.
You have not given memory to pointer data in line myData* data and trying to assign something to it.Alternative method are either
define myData data then use &data as pointer
or allocate memory using dynamic memory allocation.
You have a couple of undefined behaviors in that little piece of code...
You have a double-pointer, but never "point" either of them to anything. This mean they will point to random memory locations.
You print an uninitialized character array, which means it contains random data.
And since you don't do any initialization at all, foo.count will also contain a random value, which may be negative or very large.
And last bot not least, like I said in my comment, that code should not even compile as you use the wrong syntax for the access of the members in the structure.
i have a packet struct which have a variable len for a string example:
BYTE StringLen;
String MyString; //String is not a real type, just trying to represent an string of unknown size
My question is how i can make the implementation of this packet inside an struct without knowing the size of members (in this case strings). Here is an example of how i want it to "look like"
void ProcessPacket (PacketStruct* packet)
{
pointer = &packet.MyString;
}
I think its not possible to make since the compiler doesn't know the size of the string until run time. So how can make it look high level and comprehensible?.
The reason i need structs its for document every packet without the user actually have to look any of the functions that analyze the packet.
So i can resume the question to: is there a way to declare an struct of undefined size members or something close as a struct?
I would recommend a shell class that just interprets the packet data.
struct StringPacket {
char *data_;
StringPacket (char *data) : data_(data) {}
unsigned char len () const { return *data_; }
std::string str () const { return std::string(data_+1, len());
};
As mentioned in comments, you wanted a way to treat a variable-sized packet like a struct. The old C way to do that was to create a struct that looked like this:
struct StringPacketC {
unsigned char len_;
char str_[1]; /* Modern C allows char str_[]; but C++ doesn't */
};
And then, cast the data (remember, this is C code):
struct StringPacketC *strpack = (struct StringPacketC *)packet;
But, you are entering undefined behavior, since to access the full range of data in strpack, you would have to read beyond the 1 byte array boundary defined in the struct. But, this is a commonly used technique in C.
But, in C++, you don't have to resort to such a hack, because you can define accessor methods to treat the variable length data appropriately.
you can copy the string into a high-level std::string (at least, if my guess that String is a typedef for const char* is correct):
void ProcessPacket( const PacketStruct& packet )
{
std::string highLevelString( packet.MyString,
static_cast< size_t >( packet.StringLen ) );
...
}
A simple variant according to your posting would be:
struct PacketStruct {
std::string MyString;
size_t length () const { return MyString.length(); }
const char* operator & () const { return MyString.c_str(); }
};
This can be used (almost) as you desired above:
void ProcessPacket (const PacketStruct& packet)
{
const char * pointer = &packet;
size_t length = packet.length();
std::cout << pointer << '\t' << length << std::endl;
}
and should be invoked like:
int main()
{
PacketStruct p;
p.MyString ="Hello";
ProcessPacket(p);
}