Thread: moving pointers to pointers

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    161

    moving pointers to pointers

    I have a pointer that points to an array of pointers(char **pp). The array of pointers that "pp" points to, points to sections of a string. I want to move the address's that the pointers point to in a string to make room for a new pointer.

    Here is my formula using memcpy to copy data.

    memcpy(pp + (sizeof(char *) * (Index+1)), pp+(sizeof(char *) * Index), (pplen - Index) * sizeof(char *));

    "Index" is an int of where to add the new pointer in memory. This is sopposed to copy data from one spot to another which will make room for a new element in the correct spot for fast searching. The problem is it doesn't work properly. Everything is ok if The corect spot to put the new pointer is at the end because that does not require memcpy. But if it requires to use memcpy or memmove to move data to make room for a new pointer it all messes up.

    I get no errors from that line. But because something is wrong in my formula I get errors when searching. How can I move a memory block of pointers to a new location?

    Thanx in advance!
    Last edited by Benzakhar; 12-25-2003 at 11:29 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ?????

    This seems very obfuscated to me. There's got to be a better way. Memcpy was not designed for what you are using it for.

    Memcpy is designed to copy a block of memory to another block of memory - or a bit blt.

    lds esi,[Source]
    les edi,[Target]
    mov ecx,[sizeofbuffer]
    rep movsd

    but memcpy copies in bytes, not dwords.


    Not sure what you are wanting to do.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: moving pointers to pointers

    Originally posted by Benzakhar
    I have a pointer that points to an array of pointers(char **pp). The array of pointers that "pp" points to, points to sections of a string. I want to move the address's that the pointers point to in a string to make room for a new pointer.
    That's horribly worded. Let's see if we can sort it out:
    Code:
    char **pp; /* your array of ponters */
    char ***ppp; /* your pointer to the above */
    
    ppp = &pp; /* points to your array now */
    Now you want ppp to point to a new array which has more room?
    Code:
    char **pp2;
    
    pp2 = malloc( some_new_size );
    for( x = 0; x < old_array_size; x++ )
        pp2[x] = pp[x];
    
    ppp = &pp2;
    free( pp ); /* don't free what it points to, just it */
    Something like that? Of course, you're on the C++ board, so you probably should use new and delete.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ye tied mah tongues and lost me at the 3rd "points" laddie
    But if l get what you are trying to do you are trying to insert a character into the middle of the string?? Yaahh??

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    memcpy works fine when you're moving data from one chunk of memory to another, but when you use it on the same piece of memory, it can fail. How?

    Imagine you are at element[0]. First you put e[0] into e[1],
    then e[1] into e[2]...uhoh! You already overwrote e[1]! So to get it to work, you have to either write a reverse_copy routine (which starts from the end), or else use some swapping technique...


    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void
     print(const char * msg, char ** arp, int lng)
    {
     cout << (msg ? msg : "") << endl;
     
         for(int n = 0; n < lng; ++n)
          cout << arp[n] << endl;
    
     cin.get();     
    }
    
    
    bool
     insert(char ** arp, int lng, char * adr, int where)
    {
     if(where >= lng)
      return false;
      
     char * t1, * t2 = arp[where];
     
         for(int n = where+1; n < lng; ++n)
        {
         t1 = arp[n];
         arp[n] = t2;
         t2 = t1;      
        }
      
     arp[where] = adr;
     
     return true;  
    }
    
    
    int
     main(int argc, char *argv[])
    {
     const int size = 10;
     
     char str[size+1] = "0123456789";
     
     char * index[size] = 
     {
      &str[0], 
      &str[0], 
      &str[0], 
      &str[0], 
      &str[0], 
      &str[0], 
      &str[0], 
      &str[0], 
      &str[0], 
      &str[0] 
     };
     
     print("Start:", index, size);
     
     insert(index, size, &str[4], 4); 
     
     print("Finish:", index, size); 
     
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And there are functions better suited to the task at hand - try looking into the string manipulation functions.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    Thanx everyone. I got it to work by using a loop to just move data on by one. I don't know why but I use memcpy to make some space for new char's in the middle of a string and it works just fine for char's but not for char pointers. I just have one pointer declared pointing to array's of pointers e.g. "char ** pp = new char *[34]".

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Originally posted by Bubba
    but memcpy copies in bytes, not dwords.
    There are no restrictions on how memcpy() can be implemented - I know my version better be copy'n dwords if it can!!
    Originally posted by Sebastiani
    memcpy works fine when you're moving data from one chunk of memory to another, but when you use it on the same piece of memory, it can fail.
    Use memmove() if your source and destination buffers overlap.

    gg

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There are no restrictions on how memcpy() can be implemented - I know my version better be copy'n dwords if it can!!
    Based on several disassemblies of code that calls memcpy() it has been my experience that memcpy() uses movsb, not movsw or movsd.

    Also a 32-bit memcpy like the one I provided is faster than a memcpy() - but you must also point to the stack and not use temporaries. Best bet is to assemble it in a separate asm module and link it in with your code.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's what every application that uses memcpy() from MSVCRT.DLL uses - and you'll find something similiar in any other non-CS101 implementation.

    (Attached file in its entirety instead of posting snippets)

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. 2D Array of Pointers
    By Slavakion in forum C++ Programming
    Replies: 12
    Last Post: 03-31-2004, 05:05 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Replies: 4
    Last Post: 09-12-2001, 02:05 PM