Thread: Doubts concernign reallocation a 2-d array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    Doubts concernign reallocation a 2-d array

    Hello,

    My name is Michal and I live in Poland. I’ve found this forum while trying to get the information on how to reallocate a 2-d array and I’ve come across a thread which was concerned over 6 years ago on this forums. I hope you forgive me for coming back to it but I found there something which I find quite strange.

    The link is: How to Realloc 2-D array

    My question concerns the last post (by Salem) in this thread who wrote it in response to a piece of code posted by wots-guge in the post right above it. Salem wrote that there was a mistake in this code as the temp pointer “K2”, used to secure the outcome of realloc functions, points to the same locating that “K1” is pointing and therefore the poster’s “effort to avoid stamping on the old pointer fails”. I hope you can find that.

    My question is: Is it really a mistake? As I see it, even if two pointers (in this case ‘K1’ and ‘K2’) point to the same location and the latter’s value is changed it doesn’t affect the former nor the location to which it points, therefore there is no problem using the same temp pointer a couple of times in order to assume the result of realloc functions.

    Am I right or am I mixing something? Please, tell me if I don’t understand something and. I hope you don’t mind my refreshing a long forgotten thread but this issue really bothers me.

    Thanks you, Michal

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well, run the code in the debugger.
    Code:
    Starting program: /home/sc/Documents/a.out 
    2 	3 	
    3 	4 	
    4 	5 	
    5 	6 	
    
    Breakpoint 1, main () at foo.c:36
    36	    if ((K2[i] = (int *) realloc (K1[i],(Col+2)*(sizeof(int)))) == NULL)
    (gdb) print i
    $9 = 0
    (gdb) print K1[i]
    $10 = (int *) 0x602040
    (gdb) print K2[i]
    $11 = (int *) 0x602040
    (gdb) # now imagine realloc returned NULL on error
    (gdb) set K2[i] = 0
    (gdb) # now look at K1
    (gdb) print K1[i]
    $12 = (int *) 0x0
    (gdb) # oops!!!! bye bye the last pointer to that bit of memory.
    (gdb) quit
    A debugging session is active.
    
    	Inferior 1 [process 4532] will be killed.
    
    Quit anyway? (y or n) y
    Ideally, if realloc returns NULL, you should have the old pointer to hand to do something with it.
    But this code just blows it away by trying to be overly clever.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I must admit, it even took me a bit to realise the problem after only looking at the code snippet and not peeking at the answer. Writes through K2 go to the same location as writes through K1. You will be assuming these go to separate arrays.

    The for loop can be replaced with this, which avoids that particular problem:
    Code:
    for (i=0; i<Row; i++)
    {
        int *temp = realloc(K1[i], (Col+2)*(sizeof(int)));
        if (temp == NULL)
            printf("\n\n Error...");
        else
            K1[i] = temp;
    }
    However, the next problem is that no record is kept of whether any of these failed or not. If any have failed, then the later code will cause a buffer overrun.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Michal View Post
    My question is: Is it really a mistake? As I see it, even if two pointers (in this case ‘K1’ and ‘K2’) point to the same location and the latter’s value is changed it doesn’t affect the former nor the location to which it points, therefore there is no problem using the same temp pointer a couple of times in order to assume the result of realloc functions.
    A realloc() call (potentially) changes what is pointed at. There's therefore an interaction you're not accounting for.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. reallocation
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2008, 08:13 PM
  3. Memory reallocation in C++
    By spank in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2007, 09:56 AM
  4. reallocation
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 05:51 AM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM