strange behaviour of realloc()

Discussion in 'C Programming' started by diegotorquemada@gmail.com, May 23, 2013.

  1. Guest

    Hello,

    As far as I understand realloc() should not modify the old array, however this does not seem to happen:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    int *p, i;
    size_t size=0;

    printf("Size of the vector: ");
    scanf("%d", &size);

    p = (int *)malloc(size*sizeof(int));
    if (p==NULL)
    {
    perror("malloc() failed\n");
    exit(EXIT_FAILURE);
    }

    printf("p = %p\n", p);
    for(i=0; i<size; i++)
    {
    p = i;
    printf("%d, ", p);
    }
    printf("\b\b.\n");

    printf("Now we will duplicate the size of the list: %d\n", size);
    size *= 2;
    p = (int *)realloc(p, size);
    if (p==NULL)
    {
    perror("realloc() failed\n");
    exit(EXIT_FAILURE);
    }

    printf("p = %p\n", p);

    for(i=0; i<size; i++)
    printf("%d, ", p);
    printf("\b\b.\n");

    free(p);

    return 0;
    }

    I compile with:
    [daalvarez@earendil ~]$ gcc -Wall -o 08_realloc 08_realloc.c

    And I get:
    [daalvarez@earendil ~]$ ./08_realloc
    Size of the vector: 10
    p = 0x84e4008
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
    Now we will duplicate the size of the list: 10
    p = 0x84e4008
    0, 1, 2, 3, 4, 25, 0, 7, 8, 9, 0, 135121, 0, 0, 0, 0, 0, 0, 0, 0.
    **
    ||
    LOOK for the 5... not it is 25


    Using:
    [daalvarez@earendil ~]$ gcc --version
    gcc (GCC) 4.7.2
    Copyright (C) 2012 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    What am I doing wrong?

    Kind regards,

    Diego Andrés
    , May 23, 2013
    #1
    1. Advertising

  2. Stupid me... I just forgot to realloc with the proper size:


    >
    > #include <stdio.h>
    >
    > #include <stdlib.h>
    >
    >
    >
    > int main(void)
    >
    > {
    >
    > int *p, i;
    >
    > size_t size=0;
    >
    >
    >
    > printf("Size of the vector: ");
    >
    > scanf("%d", &size);
    >
    >
    >
    > p = (int *)malloc(size*sizeof(int));
    >
    > if (p==NULL)
    >
    > {
    >
    > perror("malloc() failed\n");
    >
    > exit(EXIT_FAILURE);
    >
    > }
    >
    >
    >
    > printf("p = %p\n", p);
    >
    > for(i=0; i<size; i++)
    >
    > {
    >
    > p = i;
    >
    > printf("%d, ", p);
    >
    > }
    >
    > printf("\b\b.\n");
    >
    >
    >
    > printf("Now we will duplicate the size of the list: %d\n", size);
    >
    > size *= 2;
    >

    p = (int *)realloc(p, size*sizeof(int));
    >
    > if (p==NULL)
    >
    > {
    >
    > perror("realloc() failed\n");
    >
    > exit(EXIT_FAILURE);
    >
    > }
    >
    >
    >
    > printf("p = %p\n", p);
    >
    >
    >
    > for(i=0; i<size; i++)
    >
    > printf("%d, ", p);
    >
    > printf("\b\b.\n");
    >
    >
    >
    > free(p);
    >
    >
    >
    > return 0;
    >
    > }
    Diego Andres Alvarez Marin, May 23, 2013
    #2
    1. Advertising

  3. James Kuyper Guest

    On 05/23/2013 05:08 PM, wrote:
    > Hello,
    >
    > As far as I understand realloc() should not modify the old array, however this does not seem to happen:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > int *p, i;
    > size_t size=0;
    >
    > printf("Size of the vector: ");
    > scanf("%d", &size);
    >
    > p = (int *)malloc(size*sizeof(int));


    That should be:
    p = malloc(size * sizeof *p);

    The 'int*' is unnecessary and, in C90 can actually hide the problem that
    would occur if you forgot to #include <stdlib.h>. Using *p rather than
    (int) makes sure that the code remains manifestly correct, even if the
    type p points at were to change - you only have to examine that one line
    to make sure it's correct. With the original form, you would have to
    also check the definition of 'p'.

    > if (p==NULL)
    > {
    > perror("malloc() failed\n");
    > exit(EXIT_FAILURE);
    > }
    >
    > printf("p = %p\n", p);
    > for(i=0; i<size; i++)
    > {
    > p = i;
    > printf("%d, ", p);
    > }
    > printf("\b\b.\n");
    >
    > printf("Now we will duplicate the size of the list: %d\n", size);


    I think you mean "double" rather than "duplicate".

    > size *= 2;
    > p = (int *)realloc(p, size);
    > if (p==NULL)
    > {
    > perror("realloc() failed\n");
    > exit(EXIT_FAILURE);
    > }


    That should be
    char *temp = realloc(p, size * sizeof *p);
    if(temp==NULL)
    {
    perror("realloc() failed\n");
    free(p);
    exit(EXIT_FAILURE);
    }
    p = temp;

    You should not store the value returned by realloc() in the only pointer
    object that the original pointer value was stored in. That's because, if
    the return value is a null pointer, the original pointer is still valid,
    and the memory it pointed at is still allocated, and still needs to be
    free()d. However, since you've replaces that pointer value with a null
    pointer value, you can neither access that memory nor free() it. You've
    just created a potential memory leak. That's not a problem in a tiny
    program like this, but you should make using realloc() this way an
    automatic habit, so you don't even have to think about it.

    > printf("p = %p\n", p);
    >
    > for(i=0; i<size; i++)
    > printf("%d, ", p);
    > printf("\b\b.\n");
    >
    > free(p);
    >
    > return 0;
    > }
    >
    > I compile with:
    > [daalvarez@earendil ~]$ gcc -Wall -o 08_realloc 08_realloc.c
    >
    > And I get:
    > [daalvarez@earendil ~]$ ./08_realloc
    > Size of the vector: 10
    > p = 0x84e4008
    > 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
    > Now we will duplicate the size of the list: 10
    > p = 0x84e4008
    > 0, 1, 2, 3, 4, 25, 0, 7, 8, 9, 0, 135121, 0, 0, 0, 0, 0, 0, 0, 0.
    > **
    > ||
    > LOOK for the 5... not it is 25


    I see that you've already found the main problem. My comments were
    mainly about peripheral issues.
    James Kuyper, May 23, 2013
    #3
  4. Eric Sosman Guest

    On 5/23/2013 5:08 PM, wrote:
    > Hello,
    >
    > As far as I understand realloc() should not modify the old array, however this does not seem to happen:


    Your code has many errors. Most are probably not important,
    but one is glaringly wrong.

    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > int *p, i;
    > size_t size=0;
    >
    > printf("Size of the vector: ");


    PNIP: The prompt might never appear; see the FAQ.

    > scanf("%d", &size);


    PNIP: "%d" converts an `int' value, but `size' is a `size_t'.
    PNIP: If scanf() fails, you'll never find out about it.

    >
    > p = (int *)malloc(size*sizeof(int));
    > if (p==NULL)
    > {
    > perror("malloc() failed\n");
    > exit(EXIT_FAILURE);
    > }
    >
    > printf("p = %p\n", p);


    PNIP: "%p" expects a `void*', not an `int*'.

    > for(i=0; i<size; i++)
    > {
    > p = i;
    > printf("%d, ", p);
    > }
    > printf("\b\b.\n");
    >
    > printf("Now we will duplicate the size of the list: %d\n", size);


    PNIP: "%d" converts an `int', not a `size_t'.

    > size *= 2;
    > p = (int *)realloc(p, size);


    GLARING: The second argument calls for `size' bytes, but you
    need space for `size' `int's.

    > if (p==NULL)
    > {
    > perror("realloc() failed\n");
    > exit(EXIT_FAILURE);
    > }
    >
    > printf("p = %p\n", p);


    PNIP: As above.

    > for(i=0; i<size; i++)
    > printf("%d, ", p);


    GLARING: Unless sizeof(int)==1, this marches off the end of
    the allocated area.

    PNIP: Even if sizeof(int)==1, this reads a lot of uninitialized
    ("indeterminate") values.

    > printf("\b\b.\n");
    >
    > free(p);
    >
    > return 0;
    > }



    --
    Eric Sosman
    d
    Eric Sosman, May 23, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    424
    Antonio
    Dec 29, 2004
  2. Jan
    Replies:
    2
    Views:
    1,428
    Mike Treseler
    Dec 16, 2004
  3. David Cantin

    Strange behaviour with perl and apache

    David Cantin, Nov 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    453
    Jim Gibson
    Nov 3, 2003
  4. Dennis Johansson
    Replies:
    1
    Views:
    499
    Dennis Johansson
    Aug 21, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    384
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page