er****@gmail.com said:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int i;
int a[1];
int *p = NULL;
p = a;
p = (int *) realloc(p,3*sizeof(int));
The Standard (C89 cite) says:
void *realloc(void *ptr, size_t size);
[...] If ptr is a null pointer, the realloc function behaves like the
malloc function for the specified size. Otherwise, if ptr does not match
a pointer earlier returned by the calloc , malloc , or realloc function,
or if the space has been deallocated by a call to the free or realloc
function, the behavior is undefined."
Since you passed to realloc a pointer that was neither NULL nor a pointer
earlier returned by calloc, malloc, or realloc, the behaviour is
undefined.
Isn't it possible to reallocate the array "a" again via pointer p ?
No. Start with p = malloc(n * sizeof *p) or p = realloc(NULL, n * sizeof
*p). Then you'll be fine. Don't forget that malloc and realloc can fail,
in which case they return NULL.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999