Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
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 68030
 
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
White shift arrow showing up on desktop (leopard) flyingscott OS X 3 Jan 9, 2014 09:41 AM
really annoying command option control and shift keys showing up on screen theglaps Mac Applications and Mac App Store 3 Aug 31, 2012 11:19 AM
Values into array from XML beesbane iPhone/iPad Programming 0 Dec 6, 2010 07:03 PM
Aarrghhh - Shift Click bringing up all my open windows deltrotter OS X 2 Nov 18, 2008 08:10 AM


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

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

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