Thread: shift characters inside array

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    shift characters inside array

    hi,

    i have an unsigned char array...say my_array[3] and i'm trying to shift every content in the array by 2.

    for example, i have: my_array[1] = 'aa'
    my goal is to shift the 'aa' by 2 to the right so its ' aa'

    can anyone give me any suggestions? i thought about using memcopy but i figured using the shift operators (<< and >>) will be much faster since my overall goal is to reduce processing time.

    thanks,
    bg742
    Last edited by barneygumble742; 07-17-2009 at 06:35 PM. Reason: forgot a space

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Use a deque, instead of an array. Safer and allows for fast insertions and access. For the example you provided, you'd want to then use the push_front member function.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    hi. how will it work with pointers? for example i have this:
    Code:
    unsigned char *array;
    array = (unsigned char *)malloc(SIZE);
    fread(array, 1, SIZE, fin);                // reading from file

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is C rather than C++. Which are you actually using?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    only the file IO part is in C but the rest of the code is C++.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It would be a lot faster if you just read the file with a std::string. You could then insert spaces wherever you want with one of the insert functions.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    very true. i thought about that. but for whatever reason, using C style file IO reported slightly faster times than C++.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Can you prove that to me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-24-2008, 06:16 PM
  2. copying the first 10 characters to an array?
    By arya6000 in forum C Programming
    Replies: 3
    Last Post: 09-20-2008, 10:39 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

Tags for this Thread