NO

Author Topic: memcpy question  (Read 4583 times)

Elpower

  • Guest
memcpy question
« on: May 12, 2012, 03:36:13 PM »
Code: [Select]
struct Data{
struct Data* prev;
int id;
char* name;
struct Data* next;
};

...

struct Data * _data;
struct Data * _data_b;

...
_data_b->prev = _data->prev;
_data_b->next = _data->next;
memcpy(data,_data_b, sizeof(struct Data)) //I don't know if this is correct

Just what is said in comment is my question
« Last Edit: May 12, 2012, 03:39:00 PM by Elpower »

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: memcpy question
« Reply #1 on: May 12, 2012, 03:58:13 PM »
No need to use the type.

Code: [Select]
memcpy(data, _data_b, sizeof(Data));
---
Stefan

Proud member of the UltraDefrag Development Team

iZzz32

  • Guest
Re: memcpy question
« Reply #2 on: May 12, 2012, 11:30:56 PM »
sizeof(Data)
Your code would work only if you use C++ or if there is typedef struct Data Data; somewhere.

Btw, I presume that Flpower wants to copy data into data_b, so memcpy should be invoked like memcpy(_data_b, _data, sizeof(struct Data)); or memcpy(_data_b, _data, sizeof(*_data));.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: memcpy question
« Reply #3 on: May 13, 2012, 01:09:03 PM »
If data and _data_b are pointers to the same type of structure:
Code: [Select]
  *data = *_data_b;
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: memcpy question
« Reply #4 on: May 13, 2012, 04:27:49 PM »
If data and _data_b are pointers to the same type of structure:
Code: [Select]
  *data = *_data_b;

Hi frankie...  They'd better be the same or even memcpy() would mess it up.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: memcpy question
« Reply #5 on: May 13, 2012, 05:24:13 PM »
Of course.
But there can be a couple of cases that work with memcpy and not for the equality operator.
You can declare two structures with exaclty the same layout, but in two different declarations. The compiler doesn't check consistency between the two, but simply consider them two different variables rising an error.
The second case is for a structure fully defined, and another one which have the same contents at beginning then other variables or is an incomplete struct.
Anyway you are right, sometimes I just forget to be enaugh adherent to the question and consider some 'advanced' use (and also misuse) of 'C' programming.  :-[
As you said in another thread 'C' will never be replaced easily because is enough low level for fast and efficient system programming and also enaugh high level for wathever else  8)
Even if googling you'll find some trials to write an operating system Java or C++ based base software is efficient only in 'C' that guarantee portability between different processors.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: memcpy question
« Reply #6 on: May 13, 2012, 06:17:28 PM »
Of course.
But there can be a couple of cases that work with memcpy and not for the equality operator.
You can declare two structures with exaclty the same layout, but in two different declarations. The compiler doesn't check consistency between the two, but simply consider them two different variables rising an error.
The second case is for a structure fully defined, and another one which have the same contents at beginning then other variables or is an incomplete struct.

A third case is nested structures... you can memcpy() to and from the nested struct but you can't always do it with the simple struct = struct; method. 

Quote
Anyway you are right, sometimes I just forget to be enaugh adherent to the question and consider some 'advanced' use (and also misuse) of 'C' programming.  :-[

:D We all do that from time to time, my friend.  It's kinda hard to keep our favorite tricks at bey while helping a beginner get his/her legs.