realloc

D

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
 
W

Walter Roberson

I was trying to enlarge the size of an array wqith this code:
float data [100];
data = realloc (data, n*sizeof(float));

You can only realloc() space that was dynamically allocated,
such as via malloc() or alloc() .
 
K

Keith Thompson

I was trying to enlarge the size of an array wqith this code:
float data [100];
data = realloc (data, n*sizeof(float));

You can only realloc() space that was dynamically allocated,
such as via malloc() or alloc() .

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

Al Bowers

Walter said:
D² said:
I was trying to enlarge the size of an array wqith this code:
float data [100];
data = realloc (data, n*sizeof(float));


You can only realloc() space that was dynamically allocated,
such as via malloc() or alloc() .

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 */
 
E

Emmanuel Delahaye

D² wrote on 21/04/05 :
I was trying to enlarge the size of an array with this code:

float data [100];

Bad feeling...
data = realloc (data, n*sizeof(float));

This is damned wrong. Only an allocated block can be reallocated.
but I encountered an error during compile fase,

Sure. An array is not a modifiable left-value.
is this sintax right?
Nope.

How can should I do?

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?"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top