473,386 Members | 1,752 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,386 software developers and data experts.

Realloc

Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory. However, when I call
realloc on an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.

Therefore I was wondering if the realloc will only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?

Cheers,
Nick

May 16 '07 #1
5 2810
polas wrote:
Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory.
However, when I call
realloc on an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.
What makes you think so? (Other than that the implementation may
impose a limit on the size of mallocated objects, or have limited
space available.)
Therefore I was wondering if the realloc will only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?
Not and be standard-conformant. (And assuming you haven't trashed
the space in which malloc/realloc/free operate.)

Note that `realloc` is free to allocate fresh memory if it can't
expand in-place.

--
Nit-picking is best done among friends.

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

May 16 '07 #2
In article <11********************@q75g2000hsh.googlegroups.c om>,
polas <ni**@helpforce.comwrote:
>Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory. However, when I call
realloc on an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.
No, there is no more limit than there is for the malloc() case.

>Therefore I was wondering if the realloc will only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?
realloc() will never (in any implementation) allocate memory that
overlaps with any other current allocation (not unless you've accidently
bashed it's control structures). But if you have allocated
memory and freed it, and then continue to use the memory in violation
of the implied contract between you and the allocator, then Sure, that
no-longer-yours memory could get overwritten; the same can happen
if you write outside of an allocated object, since outside of the object
might be memory that the allocator was using to keep track of what
was allocated.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
May 16 '07 #3
polas wrote:
>
Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc
then I can allocate an arbitrary amount of memory. However, when
I call realloc on an already existing block of memory (to increase
it's size) then there is a limit to the size it will go.

Therefore I was wondering if the realloc will only increase the
size of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?
There is no C reason for this behaviour. There may be something
peculiar about your application, or C installation. realloc can
increase or decrease size. But, at some point, it may need to have
both versions allocated, and do a memory copy. This could cause a
different limit that that for malloc.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 17 '07 #4
On May 16, 7:20 pm, polas <n...@helpforce.comwrote:
Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory. However, when I callreallocon an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.
realloc() returns a pointer to the newly allocated memory... The
current block may be moved in case more memory is required to get
contiguous block... The realloc() takes care of free() etc if it is
required in case memory block has been copied to another block...
>
Therefore I was wondering if thereallocwill only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, couldreallocever
overwrite other, existing data?

Cheers,
Nick
- Surinder Kumar
http:///www.techpenguin.com/

May 17 '07 #5
On 17 May, 03:10, CBFalconer <cbfalco...@yahoo.comwrote:
polas wrote:
Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc
then I can allocate an arbitrary amount of memory. However, when
I call realloc on an already existing block of memory (to increase
it's size) then there is a limit to the size it will go.
Therefore I was wondering if the realloc will only increase the
size of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?

There is no C reason for this behaviour. There may be something
peculiar about your application, or C installation. realloc can
increase or decrease size. But, at some point, it may need to have
both versions allocated, and do a memory copy. This could cause a
different limit that that for malloc.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account fromhttp://www.teranews.com
Right, thanks for the answers - It more than answers my question :)

Nick

May 17 '07 #6

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

Similar topics

12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
7
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
3
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; }...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.