• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

c standards with regard to pointer behavior

If i have two pointers to structures a and b, like this:

Thing *a, *b;

I can copy the contents of the Thing pointed to by b to a like so:

*a = *b;

(which is basically the same as memcpy (a, b, sizeof(*a))...)

So the question is, what if a and b both point to exact same location?  Is the behavior undefined, or should it leave the contents unchanged as you would expect?

I can test it easily, but i want to know if it is guaranteed portable.
0
catbutt
Asked:
catbutt
  • 2
1 Solution
 
sunnycoderCommented:
Hi catbutt,

*a = *b should be fine

memcpy ( a,b, sizof (Thing) ) is undefined

memmove ( a,b .. )  will be fine

Sunnycoder
0
 
catbuttAuthor Commented:
I'm so impressed with the speed of your answer (2 minutes) that I'll tack on a few more points. :)  
0
 
sunnycoderCommented:
:o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now