C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-25-2003, 11:26 AM   #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.
Benzakhar is offline   Reply With Quote
Old 12-25-2003, 09:26 PM   #2
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,814
?????

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.
Bubba is offline   Reply With Quote
Old 12-25-2003, 09:45 PM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
Re: moving pointers to pointers

Quote:
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 12-25-2003, 10:20 PM   #4
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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??
WDT is offline   Reply With Quote
Old 12-26-2003, 12:35 AM   #5
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,034
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;
}
Sebastiani is offline   Reply With Quote
Old 12-26-2003, 05:18 AM   #6
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,814
And there are functions better suited to the task at hand - try looking into the string manipulation functions.
Bubba is offline   Reply With Quote
Old 12-26-2003, 11:27 AM   #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]".
Benzakhar is offline   Reply With Quote
Old 12-26-2003, 12:01 PM   #8
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,898
Quote:
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!!
Quote:
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
Codeplug is offline   Reply With Quote
Old 12-26-2003, 09:59 PM   #9
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,814
Quote:
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.
Bubba is offline   Reply With Quote
Old 12-27-2003, 08:30 AM   #10
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,898
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
Attached Files
File Type: txt memcpy.asm.txt (21.6 KB, 45 views)
Codeplug is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers InvariantLoop C Programming 13 02-04-2005 09:32 AM
2D Array of Pointers Slavakion C++ Programming 12 03-31-2004 05:05 PM
Passing pointers between functions heygirls_uk C Programming 5 01-09-2004 06:58 PM
Array of pointers for dynamically allocating class instances sh0x C++ Programming 4 09-12-2001 02:05 PM


All times are GMT -6. The time now is 06:35 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22