realloc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D²

    #1

    realloc

    Hello,
    I was trying to enlarge the size of an array wqith this code:

    float data [100];

    data = realloc (data, n*sizeof(float) );

    but I encountered an error during compile fase, is this sintax right? How
    can should I do?

    thank you all

    Daniele


  • Walter Roberson

    #2
    Re: realloc

    In article <wJS9e.58204$IN .1027775@twiste r2.libero.it>,
    D² <spectrix@wind. it> wrote:[color=blue]
    >I was trying to enlarge the size of an array wqith this code:
    >float data [100];
    >data = realloc (data, n*sizeof(float) );[/color]

    You can only realloc() space that was dynamically allocated,
    such as via malloc() or alloc() .
    --
    "This was a Golden Age, a time of high adventure, rich living and
    hard dying... but nobody thought so." -- Alfred Bester, TSMD

    Comment

    • Keith Thompson

      #3
      Re: realloc

      roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:[color=blue]
      > In article <wJS9e.58204$IN .1027775@twiste r2.libero.it>,
      > D² <spectrix@wind. it> wrote:[color=green]
      >>I was trying to enlarge the size of an array wqith this code:
      >>float data [100];
      >>data = realloc (data, n*sizeof(float) );[/color]
      >
      > You can only realloc() space that was dynamically allocated,
      > such as via malloc() or alloc() .[/color]

      What is alloc()? Do you mean calloc()?

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • Al Bowers

        #4
        Re: realloc



        Walter Roberson wrote:[color=blue]
        > In article <wJS9e.58204$IN .1027775@twiste r2.libero.it>,
        > D² <spectrix@wind. it> wrote:
        >[color=green]
        >>I was trying to enlarge the size of an array wqith this code:
        >>float data [100];
        >>data = realloc (data, n*sizeof(float) );[/color]
        >
        >
        > You can only realloc() space that was dynamically allocated,
        > such as via malloc() or alloc() .[/color]

        The first arguement in function realloc may have a NULL value
        which makes the function behave like function malloc for the
        requested space. 'data' must have either the value of NULL or
        a value previously returned by function malloc, calloc, or
        realloc.

        In the above realloc statement, it is unwise to assign the
        return value of function realloc to 'data'. If the function
        returned NULL, 'data' with have that NULL value and the previous
        value of 'data' which may have been a pointer to allocated memory
        would be lost making the space unaccessible for use
        or for freeing the space.

        Typically, one will make the allocations as follows.

        #include <stdlib.h>

        float *data = NULL;
        float *tmp;
        size_"t n;

        n = 15;
        tmp = realloc(data,n * sizeof *data);
        if(tmp != NULL)
        {
        data = tmp;
        /* use the storage */
        }
        n = 8;
        tmp = realloc(data,n * sizeof *data);
        if(tmp != NULL)
        {
        data = tmp;
        /* use the storage */
        }
        free(data); /* Finished with it */
        data = NULL; /* put it back to NULL */



        --
        Al Bowers
        Tampa, Fl USA
        mailto: xabowers@myrapi dsys.com (remove the x to send email)
        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


        Comment

        • D²

          #5
          Re: realloc

          Thank you all again!
          bye



          Comment

          • Emmanuel Delahaye

            #6
            Re: realloc

            D² wrote on 21/04/05 :[color=blue]
            > I was trying to enlarge the size of an array with this code:
            >
            > float data [100];[/color]

            Bad feeling...
            [color=blue]
            > data = realloc (data, n*sizeof(float) );[/color]

            This is damned wrong. Only an allocated block can be reallocated.
            [color=blue]
            > but I encountered an error during compile fase,[/color]

            Sure. An array is not a modifiable left-value.
            [color=blue]
            > is this sintax right?[/color]

            Nope.
            [color=blue]
            > How can should I do?[/color]

            Try that. Not exactly trivial, but hard to make good and less...

            size_t n = 100
            float *data = malloc (sizeof *data * n);

            if (data != NULL)
            {
            /* ... */

            /* reallocation (say double the size, a common approach) */

            {
            float *p = realloc (data, sizeof *data * n * 2);

            if (p != NULL)
            {
            n *= 2;
            data = p;
            }
            else
            {
            /* We do have a memory problem... */
            n = 0;
            free (data), data = NULL;
            }
            }

            if (data != NULL)
            {
            /* ... */

            /* when finished ... */
            free (data), data = NULL;
            }

            }

            --
            Emmanuel
            The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
            The C-library: http://www.dinkumware.com/refxc.html

            I once asked an expert COBOL programmer, how to
            declare local variables in COBOL, the reply was:
            "what is a local variable?"

            Comment

            Working...