1

I have a query about using memset and memcopy on structures and their reliablity. For eg:

I have a code looks like this

typedef struct
    {
    short a[10];
    short b[10];
}tDataStruct;

 tDataStruct m,n;

memset(&m, 2, sizeof(m));
memcpy(&n,&m,sizeof(m));

My question is,

1): in memset if i set to 0 it is fine. But when setting 2 i get m.a and m.b as 514 instead of 2. When I make them as char instead of short it is fine. Does it mean we cannot use memset for any initialization other than 0? Is it a limitation on short for eg

2): Is it reliable to do memcopy between two structures above of type short. I have a huge strings of a,b,c,d,e... I need to make sure copy is perfect one to one.

3): Am I better off using memset and memcopy on individual arrays rather than collecting in a structure as above?

One more query,

In the structue above i have array of variables. But if I am passed pointer to these arrays and I want to collect these pointers in a structure

typedef struct
    {
    short *pa[10];
    short *pb[10];
}tDataStruct;

 tDataStruct m,n;

memset(&m, 2, sizeof(m));
memcpy(&n,&m,sizeof(m));

In this case if i or memset of memcopy it only changes the address rather than value. How do i change the values instead? Is the prototype wrong?

Please suggest. Your inputs are very imp

Thanks dsp guy

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
user437777
  • 1,423
  • 4
  • 17
  • 28

1 Answers1

1
  1. memset set's bytes, not shorts. always. 514 = (256*2) + (1*2)... 2s appearing on byte boundaries. 1.a. This does, admittedly, lessen it's usefulness for purposes such as you're trying to do (array fill).
  2. reliable as long as both structs are of the same type. Just to be clear, these structures are NOT of "type short" as you suggest.
  3. if I understand your question, I don't believe it matters as long as they are of the same type.

Just remember, these are byte level operations, nothing more, nothing less. See also this.

For the second part of your question, try

memset(m.pa, 0, sizeof(*(m.pa));
memset(m.pb, 0, sizeof(*(m.pb));

Note two operations to copy from two different addresses (m.pa, m.pb are effectively addresses as you recognized). Note also the sizeof: not sizeof the references, but sizeof what's being referenced. Similarly for memcopy.

Community
  • 1
  • 1
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
  • Hi Richard, thanks for reply. My problem is I have all data as words 16 bit. If memset works on bytes only then i cannot use it on words because it gives me wrong answer. is this correct? – user437777 Jul 27 '12 at 05:44
  • You are correct with two exceptions: you can use it to fill 0's or -1's (0xFF). It's not so useful for other values. – Richard Sitze Jul 27 '12 at 05:48
  • Thanks richard. It clear now. I have updated one more query, will be grateful for your inputs – user437777 Jul 27 '12 at 05:51
  • HI Richard. I tried it and it works fine. but it would mean that if i have 'n' Members in structures i will need to perform 'n' memset operations. This defeats my purpose because I wanted to collect all pointers in one structure and use only one memset to reset all values pointed by those pointers. In that context, am I not better off using individual arrays and do 'n' different memsets? or is there a way to realize that? – user437777 Jul 27 '12 at 06:35
  • Again, these are low-level byte operations. They execute over a block of memory. If you have disjoint memory (struct of pointers to who-knows-where) then you'll need to address each block separately. If you have contiguous memory (struct of arrays) then you can do it with one. – Richard Sitze Jul 27 '12 at 11:02