Solved

c standards with regard to pointer behavior

Posted on 2004-03-23
3
362 Views
Last Modified: 2010-04-15
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
Comment
Question by:catbutt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 75 total points
ID: 10664771
Hi catbutt,

*a = *b should be fine

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

memmove ( a,b .. )  will be fine

Sunnycoder
0
 
LVL 1

Author Comment

by:catbutt
ID: 10664793
I'm so impressed with the speed of your answer (2 minutes) that I'll tack on a few more points. :)  
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10664800
:o)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question