Originally Posted by
Salem
> memcpy(msg->payload, &msg, sizeof(struct send_buf));
Well &msg is the address of the pointer itself, not the data it points to.
Setting aside the issue of why you would want to make two copies of the same data inside itself, then this works
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct send_buf {
size_t remaining;
size_t pkt_len;
char payload[0];
};
int main ( ) {
struct send_buf *msg= (struct send_buf *)malloc(sizeof(struct send_buf) + 1024);
msg->remaining = 4090;
msg->pkt_len = 1080;
// Trying to copy this data to payload member
//!! note: no & on msg
memcpy(msg->payload, msg, sizeof(struct send_buf));
return 0;
}
$ gcc -g main.c
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) b 18
Breakpoint 1 at 0x40056d: file main.c, line 18.
(gdb) run
Starting program: /home/sc/Documents/a.out
Breakpoint 1, main () at main.c:18
18 return 0;
(gdb) x/50xb msg
0x602010: 0xfa 0x0f 0x00 0x00 0x00 0x00 0x00 0x00
0x602018: 0x38 0x04 0x00 0x00 0x00 0x00 0x00 0x00
0x602020: 0xfa 0x0f 0x00 0x00 0x00 0x00 0x00 0x00
0x602028: 0x38 0x04 0x00 0x00 0x00 0x00 0x00 0x00
0x602030: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x602038: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x602040: 0x00 0x00
(gdb) p/x *msg
$1 = {remaining = 0xffa, pkt_len = 0x438, payload = 0x602020}
Thanks Salem. Appreciate your time. My final goal is to send data in chunks. Actual data is stored in structservice_t *service. I need to attach some more header and break this data into chunks before sending them to other end. The another problem is, API only accepts structservice_t *service. I got this struct from upper layer. It has following members:
Code:
struct service_t {
int len;
int *data;
};
This data member has all data. I need to fragment it and send it into some chunks with some header. I don't how to copy this int * to msg->payload. I tried memcpy but it gave me segmentation fault.
Since API only accepts this, I need to define/include this structure into structsend_buf and store this chinked data back to data member of service_t structure. Do you think this approach is Okay? For example,
Code:
struct service_t {
int len; // total len
int *data; // It has actual data
};
struct send_buf {
struct service_t *service_info;
bool last_pkt;
size_t remaining;
size_t pkt_len;
char payload[0];
};
int send_to_m2(struct service_t *info)
{
struct send_buf *msg= (struct send_buf*)malloc(sizeof(struct send_buf)) + 1000];
int ch_len, send_position = 0;
msg->remaining = info->len;
do {
ch_len = (msg->remaining > 1024) ? 1024 : msg->remaining;
if(msg->remaining > 1024) {
msg->last_pkt = false;
} else {
msg->last_pkt = true;
}
memcpy(msg->payload, msg, sizeof(structsend_buf));
// OR copy everything to msg->service_info->data and then use strncat to copy data -----?????
printf("Data = %s\n", msg);
// Now copy only first chunk length data to "data" member of struct service_t
strncat((char *)msg->service_info->data, (char *)info->data + send_position, ch_len );
// GETTING ERROR
msg->pkt_len = ch_len + sizeof(struct send_buf);
service_info->len = msg->pkt_len;
send_data(&info_send); // GIVEN API
msg->remaining -= ch_len;
send_position += ch_len;
}while(msg->last_pkt != true);
return 0;
}
I hope I make sense. I know I didn't explain the problem nicely. I appreciate if you can help me.