memcpy of structure to a const char*

Oct 19, 2016 at 7:11pm
Hello All,
I want to copy a structure to a const char* using memcpy.

Really need some help.

Thanks,

Actually I want to put the data members of a struct into a set function which accepts only const char*.
Something like this:

setData (const char* Data)

and the Data is: string Data = "Hello".
Last edited on Oct 19, 2016 at 7:44pm
Oct 19, 2016 at 7:18pm
You can't copy into to const char*. It's const. You can't change the values because it's const.
Oct 19, 2016 at 7:18pm
I want to copy a structure to a const char* using memcpy.

More context is needed.
Oct 20, 2016 at 1:50am
Your edit doesn't do much to clarify. If you want to use something as a buffer, use a std::vector<char> rather than a std::string.

One can do something like this (which likely results in undefined behavior, and which I definitely wouldn't recommend):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstring>
#include <iostream>
#include <string>


void setData(const char* data) {
    char * buf = const_cast<char*>(data);
    memcpy(buf, "Bye", sizeof("Bye"));
}


int main() {
    std::string Data = "Hello";

    std::cout << "Before: " << Data << '\n';

    setData(Data.data());

    std::cout << "After: " << Data << '\n';
}


On my system that produces this (possibly surprising) result:
Before: Hello
After: Bye o


Also, it would probably behoove you to either post new information at the end of the thread so the thread is also bumped, or bump the thread drawing attention to the edit after you've made it to a previous post. Just editing a previous post in the thread will not bump it up any.
Oct 20, 2016 at 9:39am
Thanks Cire. Sorry I am new to c++.
I am working on a project to form a cluster algorithm in Omnet++ and veins. Actually I have a struct which I have to copy to constant char* so that to pass it as a string. I am trying to do like this to setWsmData as a cstring in the below function "sendMessage" and to receive the sent value in getWsmData in the function "onData" :

File:- pedcluster.h
=============
class PedCluster:: public BaseWaveApplayer {
...
protected:
void sendMessage(std::string blockedRoadId) ;

struct cluster{
int clustNum;
std::string messageName;
....
};
}

File:- pedcluster.cc
==============
void PedCluster::sendMessage(std::string blockedRoadId) {
sentMessage = true;

t_channel channel = dataOnSch ? type_SCH : type_CCH;

cluster *form = new cluster{2, "invite"};

const char* hello;
memcpy(hello, form, sizeof(form));

blockedRoadId = std::string (hello);

WaveShortMessage* wsm = prepareWSM("data", dataLengthBits, channel, dataPriority, -1, 2);

wsm->setWsmData(blockedRoadId.c_str()); // Output gives garbage value

sendWSM(wsm);
}

void PedCluster::onData(WaveShortMessage* wsm) {

if (!sentMessage) sendMessage(wsm->getWsmData());

const char* hello = {wsm->getWsmData()};

const cluster *form = reinterpret_cast <const cluster*>(hello);

}

File:- WaveShortMessage.cc
======================
const char * WaveShortMessage::getWsmData() const
{
return wsmData_var.c_str();
}

void WaveShortMessage::setWsmData(const char * wsmData)
{
this->wsmData_var = wsmData;
}

Sorry, its lengthy. Please let me know if you need some more info.
Thanks
Oct 20, 2016 at 12:29pm
1
2
3
4
const char* hello; 
memcpy(hello, form, sizeof(form));

blockedRoadId = std::string (hello);


Even if hello were not const, it points to some random place in memory. You cannot write to that random place with memcpy or by any other method and expect your program to do anything sensible. This is undefined behavior.

(And it looks like you have control over the type of hello so why make it a pointer-to-const since you want to modify what's pointed to? Why is blockedRoadId a parameter instead of a local variable as you never use the value passed into the function?)
Last edited on Oct 20, 2016 at 12:38pm
Oct 20, 2016 at 12:54pm
Thank you Cire. Yeah I think this is where I am making mistake in these three lines.
Could you please suggest how to solve this, so that to pass the data of the cluster in setWsmData.
Topic archived. No new replies allowed.