473,396 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

To reallocate an array ?

#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)); // here the code crashes ?

p[0] = 1;
p[1] = 2;
p[2] = 3;

for (i=0; i < 3;i++)
printf("%d\n",p[i]);

free(p);

return 0;
}

The code above crashes ? What is the reason ?
Isn't it possible to reallocate the array "a" again via pointer p ?
Jun 27 '08 #1
4 3021
er****@gmail.com wrote:
The code above crashes ? What is the reason ?
Isn't it possible to reallocate the array "a" again via pointer p ?
In a word: no. realloc() (which, btw, does not take a cast in
well-written C code) only works on null pointers or previously allocated
pointers, not on arrays, pointers to normal objects, or pointers which
have had arithmetic done on them.

Richard
Jun 27 '08 #2
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
Jun 27 '08 #3
Richard Bos said:

<snip>
realloc() [doesn't work on] pointers which
have had arithmetic done on them.
Not strictly true. I know what you mean, but you haven't said (sufficiently
precisely) what you mean. Consider:

p = malloc(n * sizeof *p);
failif(p == NULL); /* <--- just shorthand, okay? */
while(i++ < n)
{
*p++ = foo();
}
p -= n;
q = realloc(p, newcount * sizeof *p);

Here, p has most certainly had arithmetic done on it, but it can still be
passed to realloc. But if we erased the p -= n line, that would indeed
break the realloc call.

--
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
Jun 27 '08 #4
On May 22, 8:54 pm, erk...@gmail.com wrote:
#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)); // here the code crashes ?

p[0] = 1;
p[1] = 2;
p[2] = 3;

for (i=0; i < 3;i++)
printf("%d\n",p[i]);

free(p);

return 0;

}

The code above crashes ? What is the reason ?
Isn't it possible to reallocate the array "a" again via pointer p ?
realloc only works with pointers whose pointed to memory is allocated
dynamically, for example using malloc.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
11
by: Piotrek | last post by:
Hi, I have no idea why my reallocation function (myadd) is breaking up my array. I'm trying to solve it and it took me few hours already. Maybe you can look at my code and explain me what is...
2
by: Rahul | last post by:
Hi everyone, I had allocated from memory for a character array using the new operator in cpp. Now i want to increase this allocation dynamically, realloc() works with memory allocated by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.