Thread: memcpy

  1. #1
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    memcpy

    It occured to me that when using realloc, you have to reassign the destination pointer... like so...

    Code:
    dest_ptr = (type_cast) realloc (ptr, newsize);
    with return checking of course. you'd need to do this since the returned destination pointer might not be the same address as the sent destination pointer [so you have to reassign it to keep dest_ptr pointing to the same ideal location]...

    anyway, it dawned on me that memcpy might behave the same way. I wanted to do something like the following...

    Code:
    // makeshift example
    char *ptr_a;
    char *ptr_b;
    char *ptr_c;
    
    // allocate a, blah, assume it works... etc
    a = calloc (100, 1);
    
    b = a + 25;  // b points to the 25th element
    c = a + 50; // c points to the 50th element
    
    //  if the return pointer from b is not equal to the sent destination pointer, you'd get a mismapping, and you couldn't address from ptr_a correctly
    b = memcpy (b, c, 25);
    
    //  however if the return ptr from memcpy is the same location, everything works out nicely...
    anyways... in my dirty rectangles video setup, my usage of memcpy seems to be okay in that all the return pointers are still contigious w/ the master pointer's relative location... so, i suppose then that memcpy would never dislocate on me then eh? i could understand if the request was too large, but it would never 'succeed' with a different pointer, right? thanks.
    hasafraggin shizigishin oppashigger...

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Your assumption is correct: memcpy doesn't change any pointers.

    It only copies data from the from the second pointer to the dest. So you can use it safely without worrying about getting another ptr back.

    Also:

    Code:
    ptr1 = memcpy(ptr1, ptr2, len);
    is equal to:

    Code:
    memcpy(ptr1, ptr2, len);
    ptr1 = ptr1;
    No need to worry about the return value, if you are setting ptr1 to the return value!

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    phew, that was a close one... i'll proceed to do what i was doing... but why then does memcpy bother to return a pointer? so you could assign it to another pointer [with a possible different base data type]? that would be useful i'd imagine, are there any other reasons?

    thank you very much _ninebit_... [heh! bubba you didn't reply! ]
    hasafraggin shizigishin oppashigger...

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Correct.

    Just don't forget that the returned pointer points to the same place in memory as ptr1 does.

    Consider this:
    Code:
    void *retptr = memcopy(ptr1, ptr2, len);
    
    UsePtrToDoStuff(ptr1); // result OK!
    UsePtrToDoStuff(retptr); // result OK!
    delele ptr1; //Wooops! retptr freed trough ptr1!
    UsePtrToDoStuff(retptr); //result undefined!! retptr pointing into the void!!
    As you can see, it wasn't safe to use the retptr. So I ask the same question as you: Why does it bother returning a pointer?

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    probably for the same reason fgets returns a pointer to the buffer sent...


    cout << "you just typed " << fgets(buf, 10000, fp) << endl;

  6. #6
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    Hmmm, still though clown you could just use the original pointer, and you could distribute it later anyway so I have no idea, maybe it's just the standard or something. Oh I have another question. Since pointers can seek to whereever they want, does that mean that I can free the memory so long as I have a pointer to that memory location? Even if I don't use the pointer that I originally allocated it with? Suppose I do lose a pointer, I've come to see that the memory is freed after the end of the scope of the allocation anyway [unless you use multiple indirection], so what would be the point of free? Etc, thank you.
    hasafraggin shizigishin oppashigger...

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Correct again.

    Code:
    char *ptr1 = new char[1024];
    char *ptr2 = ptr1;
    
    // ptr2 and ptr1 now points to the same location in memory.
    //thus will this be valid 
    
    delete ptr2;
    ptr2 = NULL;
    //now the memory pointed to by the pointers are freed.
    //any use of ptr1 has undefined result (a crash)
    I use this in my gameengine to handle garbage collection. This means that if a pointer is not released before the game exits, its freed anyway by the collector.

  8. #8
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    but wait there's more! :)

    _but_!!... suppose i 'lose' the address of the first element of the buffer! [then i guess i'm screwed and this is bad practice... but this'll never happen and i'm just curious...] if it's pointing to somewhere in the middle of the block, would it still be freed correctly? or, if i don't have an explicit call to free at the end of the block is it deallocated automatically at the end of the scope? [i do it all for cleanliness anyway, but this seems to be the case] thanks...
    hasafraggin shizigishin oppashigger...

  9. #9
    Unregistered
    Guest
    I think u have to rephrase that dblAnti... realy don't get it...


    anyway look at this

    Code:
    char *ptr1 = new char[13];
    char *ptr2 = ptr1;
    
    memset(ptr1, 0, 13);
    strcpy(ptr1, "Hello World!", 12
    
    // Now ptr1 and ptr 2 both will return the same string.
    
    printf("%s\n", ptr1);
    printf("%s\n", ptr2);
    
    ptr2 += 6;
    
    printf("%s\n", ptr1);
    printf("%s\n", ptr2);
    
    // Now try to delete ptr2
    
    delete ptr2; // Crash!
    ptr2 isn't a pointer to a valid allocated memory position, but a valid memory position + 6! Since Ptr2 acctually points into the void (a valid mem position +- any number is a pointer into the void. exept that you know where in the void... ehh... ) So delete of ptr2 has an undefined behavior! So don't you lose your original pointers!!

    hope this answers the question.. i wouldn't really know...

  10. #10
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    thank you, good... and what about deallocation at the end of scope? i'm sure it's so...
    hasafraggin shizigishin oppashigger...

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    41
    I don't think a var that is new'd is deallocated at the end of the scope. It's created at the heap and all the vars that deallocate at the end of the scope are created at the stack.
    So it's not good to loose a pointer. You *have* to delete it!

    Code:
    void func()
    {
      char str[1000];
      char * temp = new [1000];
    }
    str is allocated at the stack. the pointer temp also. But the memory allocated by new is created on the heap.
    str and the *pointer* temp are deallocated at the end of the function but the memory pointed to by temp isn't.

    Hope this helps

    Joren

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disagreement about memcpy
    By ch4 in forum C Programming
    Replies: 9
    Last Post: 05-28-2009, 10:12 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. Trying to copy buffers using memcpy in C under UNIX
    By Meeper in forum C Programming
    Replies: 3
    Last Post: 07-26-2003, 08:51 AM