Solved

c standards with regard to pointer behavior

Posted on 2004-03-23
3
345 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

770 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