|
-
January 29th, 2004, 11:41 AM
#1
Data transfer between Two pointers
Hi: I have two pointers
char* pointer1;
char* pointer2;
in a loop, new data are read from file into buffer pointed by pointer2 and the old data should move from pointer2 to pointer1; pointer1 always pointer to the old data, and pointer2 always points to new data;
how can I do that?
Thank you
-
January 29th, 2004, 11:55 AM
#2
I don't know if I'm reading your post correctly, but it sounds like you want something akin to a fifo maybe?
In that case, you need to do a lot of pointer management. This really depends on how you're using it.
One method might be:
// got new data
pointer1 = pointer2; // Now pointer1 is pointing to the old data.
// increment pointer2
// put new data at pointer2
That's sort-of a general outline. If pointer2 becomes the "old data" just as the new stuff somes in all you need to do is assign pointer1 to pointer2 and then put the new data in the buffer.
Post a more in-depth example of what you need if this is totally off the mark. Thanks!
-
January 29th, 2004, 12:26 PM
#3
assuming the pointers have been properly allocated,
strcpy(pointer1, pointer2);
-
January 29th, 2004, 01:45 PM
#4
Thank you:
I used the following code to obtain what I need:
char* pointer1;
char* pointer2;
after this, I used memcpy(pointer1,pointer2,size);
here size is known;
then read new data to buffer pointed to pointer2.
thank you
-
January 29th, 2004, 11:51 PM
#5
If pointer1 and pointer2 point to (null-terminated) strings, then you can do the following, but whichever way you do it, be sure that the area ponited to by pointer1 is large enough.
Code:
strcpy(pointer1,pointer2);
-
January 30th, 2004, 12:16 AM
#6
Just for clarity
Not a bad idea
Code:
char* pointer1;
char* pointer2=new char[128];
pointer1=pointer2;
...
...
...
delete [] pointer2;
Bad idea
Code:
char* pointer1=new char[128];
char* pointer2=new char[128];
pointer1=pointer2;
...
...
...
delete [] pointer1;
delete [] pointer2;
A memory leak results because the pointer to the memory originally allocated for pointer 2 is lost. Also a runtime error will result since the memory pointer to by pointer2 is the same memory pointed to by pointer1 which will already have been deleted by delete [] pointer1.
TDM
Last edited by TDM; January 30th, 2004 at 12:33 AM.
-
January 30th, 2004, 01:40 AM
#7
Just a thought,
If you really want to use strcpy() function as GCDEF and Sam pointed out, I think first of all, it is advisable for you to make sure of the way you handle those two character pointers. During the time you read the data from file into buffer, those two pointers should never be NULLs or it will be one of many famous causes for which you will receive exceptions thrown from your system. Moreover, both of your char pointers, as Sam mentioned in his post, must always point to NULL_terminated strings, which helps you avoid undefined behaviors when you compile and run your program. Therefore, a check to see if both strings aren't NULL and a comparison between their lengths right before they are used in this error-prone strcpy() function are really of necessity.
Personally, I don't use string handling functions from cstring although my first programming languages are old Pascal and C, it is simply because of their inefficiency and error-prone_NESS. For small "exercises" in colleges, they sure never give many worries, but in large projects, the worries they bring us seem to be much more or "larger" than what we can imagine. That is also why I always try to stick myself with String class and STL.
Regards,
-Vu
-
January 30th, 2004, 01:48 AM
#8
Originally posted by TDM
A memory leak results because the pointer to the memory originally allocated for pointer 2 is lost. Also a runtime error will result since the memory pointer to by pointer2 is the same memory pointed to by pointer1 which will already have been deleted by delete [] pointer1.
Note that john100 needs to have two strings or sets of data; a new and an old, at least that is my understaning.
-
January 30th, 2004, 02:22 AM
#9
Uh.....yeah.....that is my understanding as well. One of the other posts in this thread suggested setting one pointer equal to the other pointer. Since Memory would have to be allocated for both pointers to hold the new and old data I didn't think that would be a good idea. Therefore, I made an example and gave a reason why I thought that would not be a good idea. I also gave an additional example where setting one pointer equal to another pointer could be a good idea....................
TDM
-
January 30th, 2004, 03:34 AM
#10
Originally posted by Sam Hobbs
Note that john100 needs to have two strings or sets of data; a new and an old, at least that is my understaning.
Sam, please do not get me wrong but I think TDM's suggestions are really great...
There are a lot of different solutions to only one problem though...
Regards,
-Vu
-
January 30th, 2004, 09:55 AM
#11
Originally posted by VuQ_Le
Sam, please do not get me wrong but I think TDM's suggestions are really great...
There are a lot of different solutions to only one problem though...
Regards,
-Vu
Yes, it depends on requirements. I was just not sure that the code would satisfy the stated requirements; if it does, then my comments can be ignored. However note that in the original question, there is no use of nor any mention of a need to allocate the data dynamically. Assuming it is possible to allocate two areas of storage large enough for all the data to be processed, then there is no need to reallocate. It is elegant to be able to copy just pointers but the reallocation makes the solution less efficient.
If it is advantageous to allocate the exact amount of memory for each occurance of data, then the following might be closer to what is required:
Code:
char* pointer1=NULL;
char* pointer2;
int n;
while (More) {
delete [] pointer1;
pointer1 = pointer2;
// get current n
pointer2 = new char[n];
// other processing
}
delete [] pointer2;
-
January 30th, 2004, 11:10 AM
#12
Code:
char* pointer1=NULL;
char* pointer2;
int n;
while (More) {
delete [] pointer1;
pointer1 = pointer2;
// get current n
pointer2 = new char[n];
// other processing
}
delete [] pointer2;
Looks like an access violation to me.
-
January 30th, 2004, 11:40 AM
#13
Originally posted by TDM
Looks like an access violation to me.
Okay, I'll bite. Where would the access violation occur?
-
January 30th, 2004, 12:09 PM
#14
Originally posted by Sam Hobbs
Okay, I'll bite. Where would the access violation occur?
the seond iteration will cause an access violation on 'delete [] pointer1'.
-
January 30th, 2004, 12:13 PM
#15
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|