Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 20, 2012, 09:22 AM   #1
farmerdoug
macrumors 6502a
 
Join Date: Sep 2008
shifting array rows up

I have an array of nxm elements.
I want to remove array[0]; move everything up; add something to array[n-1].

Do I use memcpy for the shifting? Is there a better way?

Thanks
farmerdoug is offline   0 Reply With Quote
Old Feb 20, 2012, 09:32 AM   #2
subsonix
macrumors 68040
 
Join Date: Feb 2008
Quote:
Originally Posted by farmerdoug View Post
I have an array of nxm elements.
I want to remove array[0]; move everything up; add something to array[n-1].

Do I use memcpy for the shifting? Is there a better way?

Thanks
You can't use memcpy because if the memory regions overlap the behavior is undefined. You can use memmove() to do it, which is the same except that the memory regions may overlap (you can also do it manually of course).

Code:
    int size = 10;
    int a[] = {1,2,3,4,5,6,7,8,9,10};

    memmove(a, a + 1, (size - 1) * sizeof(int));
subsonix is offline   0 Reply With Quote
Old Feb 20, 2012, 11:30 AM   #3
farmerdoug
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
Thanks. This is good to know.
farmerdoug is offline   0 Reply With Quote
Old Feb 20, 2012, 11:52 AM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by farmerdoug View Post
Thanks. This is good to know.
The caution about overlapping areas and memmove() is in memcpy's man page:
Code:
DESCRIPTION
     The memcpy() function copies len bytes from memory area src to memory
     area dst.  If src and dst overlap, behavior is undefined.  Applications
     in which src and dst might overlap should use memmove(3) instead.
If you were using Bwana, you could click man:memcpy and it would open the memcpy man page in your browser. The references to memmove and related functions would also be links in the presented man page.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
shifting partition with OS on macbook air airpoland MacBook Air 0 Mar 6, 2014 02:49 AM
Glitch: shifting wallpaper and icons manuelcalavera iOS 7 3 Oct 19, 2013 01:15 PM
Camera Image Shifting Problem Pixelmage iPhone Tips, Help and Troubleshooting 1 Apr 2, 2013 03:42 PM
iPhone: Aquaboard... Shifting my wallpaper to the left JrFreak Jailbreaks and iOS Hacks 1 Feb 5, 2013 06:48 PM

Forum Jump

All times are GMT -5. The time now is 10:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC