Thread: does memcpy create a new block of memory?

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    does memcpy create a new block of memory?

    given two pointers :
    Code:
    int *p1;
    int *p2;
    
    p1 = new int[10];
    
    memcpy(p2,p1,sizeof(p1));
    I'm trying to get the program to have p2 it's own memory so that if I do something like p2[0] = 1;
    p1 won't be affected.

    Does memcpy create a new memory block like that for me? If not, what does?

    <-- trying to aviod constant creation of memory

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Re: does memcpy create a new block of memory?

    Originally posted by Diamonds
    Code:
    <-- trying to aviod constant creation of memory
    well then how are you suppose to have p2 its own memory if you are trying to avoid that...well for me..i dont know much functions of other things so i write my own for loops for copying for arrays
    nextus, the samurai warrior

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Re: does memcpy create a new block of memory?

    Originally posted by Diamonds
    given two pointers :
    Code:
    int *p1;
    int *p2;
    
    p1 = new int[10];
    
    memcpy(p2,p1,sizeof(p1));
    I'm trying to get the program to have p2 it's own memory so that if I do something like p2[0] = 1;
    p1 won't be affected.

    Does memcpy create a new memory block like that for me? If not, what does?

    <-- trying to aviod constant creation of memory



    No, memcpy simply copies the contents of one chunk of memory to another.

    int a = 12, b = 75;

    memcpy(&a, &b, sizeof(int));

    cout << a << endl; // prints '75'

    cin.get();
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 02:18 AM