Sign in to follow this  
Gage64

Access violation - why? [Solved]

Recommended Posts

I have two arrays of pointers to integers, arr and result. I need to copy size elements from result to arr, starting from low. If I do this:
memcpy(arr + low, result, sizeof(int*) * size);
it works fine, but if I do this:
for (i = low; i < low + size; ++i)
    arr[i] = result[i];
I get an access violation when I later try to dereference one of the pointers in arr. I thought the loop does the same thing as the call to memcpy(), but looks like I was wrong. What is the difference between the two? Thanks in advance.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
I thought the loop does the same thing as the call to memcpy(), but looks like I was wrong. What is the difference between the two?


memcpy reads from result[0] to result[size-1], whereas the loop reads from result[low] to result[low+size-1].

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
memcpy reads from result[0] to result[size-1], whereas the loop reads from result[low] to result[low+size-1].


I can't believe I missed that...

Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this