CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Apr 2003
    Posts
    134

    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

  2. #2
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363
    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!

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637
    assuming the pointers have been properly allocated,

    strcpy(pointer1, pointer2);

  4. #4
    Join Date
    Apr 2003
    Posts
    134
    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

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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);
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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.

  7. #7
    Join Date
    Jun 2003
    Location
    Heck-:D-Hokkaido.
    Posts
    47

    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

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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

  10. #10
    Join Date
    Jun 2003
    Location
    Heck-:D-Hokkaido.
    Posts
    47
    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

  11. #11
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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;
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  12. #12
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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.

  13. #13
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by TDM
    Looks like an access violation to me.
    Okay, I'll bite. Where would the access violation occur?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  14. #14
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    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'.

  15. #15
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    Bingo

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured