![]() |
| | #1 |
| Registered User Join Date: Nov 2003
Posts: 161
| moving pointers to pointers 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 | |
| | #2 |
| Super Moderator 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 | |
| | #3 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| Re: moving pointers to pointers Quote:
Code: char **pp; /* your array of ponters */ char ***ppp; /* your pointer to the above */ ppp = &pp; /* points to your array now */ 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 */
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #4 |
| 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 | |
| | #5 |
| Guest 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 | |
| | #6 |
| Super Moderator 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 | |
| | #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 | |
| | #8 | ||
| Registered User Join Date: Mar 2003
Posts: 3,898
| Quote:
Quote:
gg | ||
| Codeplug is offline | |
| | #9 | |
| Super Moderator Join Date: Aug 2001
Posts: 7,814
| Quote:
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 | |
| | #10 |
| Registered User 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 |
| Codeplug is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |