Thread: Keeping an Array the same after a function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    34

    Keeping an Array the same after a function

    Hi All,

    I would like to keep an array the same even after it is passed into a function. I am doing different sorts on the same data but when i try using

    Code:
    num=temp
    HeapSort(temp,n);
    where n is the total number in the array and num is an array.
    When I try using this, num and temp is sorted completed. Not sure why. Is there anyway to keep num the same after a function is used on it. I am not using any pointers by the way.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by Ramkiller1 View Post
    Code:
    num=temp
    num is an array
    I very much doubt that!
    arrays cannot be used as lvalues: thay cannot be assigned to.
    I believe both num and temp are pointers (you might like to read the comp.lang.c. FAQ, especially section 6).

    The pointers point to the same memory. Whatever you do through one pointer is visible through the other.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by qny View Post
    I very much doubt that!
    arrays cannot be used as lvalues: thay cannot be assigned to.
    I believe both num and temp are pointers (you might like to read the comp.lang.c. FAQ, especially section 6).

    The pointers point to the same memory. Whatever you do through one pointer is visible through the other.
    Is there any solution to my issue then?

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Make a copy of the array and pass the copy to the function.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by christop View Post
    Make a copy of the array and pass the copy to the function.
    thats why i used temp=num;

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by Ramkiller1 View Post
    thats why i used temp=num;
    But it did not work. It was used like a pointer

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have to make a separate copy in memory so that there are two arrays. A pointer to the original array will just make an alias for that array.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by whiteflags View Post
    You have to make a separate copy in memory so that there are two arrays. A pointer to the original array will just make an alias for that array.
    Any code come to mind?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Ramkiller1 View Post
    Any code come to mind?
    Try writing it yourself. First, allocate some memory, then copy the original array into the new array. Free the new array when you are done with it.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by whiteflags View Post
    Try writing it yourself. First, allocate some memory, then copy the original array into the new array. Free the new array when you are done with it.
    Will Do

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by whiteflags View Post
    Try writing it yourself. First, allocate some memory, then copy the original array into the new array. Free the new array when you are done with it.
    Well i just did this
    Code:
    int * p;
    p=(int *)malloc(max*sizeof(int)); //Allocate size of array
    p=num;
    sort1(p,n);
    free(p);
    This did the same thing as using temp. Unless I copied it wrongly.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You copied wrongly. What you did is set the pointer to point to the first element of the array, after having allocated memory that the pointer points to. Effectively, all you did was create a memory leak, and the free(p) results in undefined behaviour or a later double free error.

    Rather:
    Code:
    int *p = malloc(max * sizeof(*p));
    /* Loop over the elements of num and assign them to the elements of p */
    /* ... */
    sort1(p, n);
    free(p);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Thanks it worked, Guess I was thinking of it wrongly. Did I need to make a pointer then for this or just another array or are you showing me how to save space ?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to use dynamic memory allocation, then a pointer was the right approach. If not, another array of at least the same size would work. You cannot save space because you need to copy those elements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Once you made a second array, I think that the function which you are after is memcpy().
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. Replies: 2
    Last Post: 02-21-2012, 09:14 PM
  3. Replies: 6
    Last Post: 12-07-2011, 06:23 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Keeping track of values without an array
    By m712 in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2002, 10:49 AM