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

Why realloc? (in __default_alloc_template, file: stl_alloc.h) (GCC ver 3.3.3)

In file: stl_alloc.h

First, we can find the definition of "__new_alloc" which manipulate
memory by "new" and "delete":

class __new_alloc
{
public:
static void*
allocate(size_t __n)
{ return ::operator new(__n); }

static void
deallocate(void* __p, size_t)
{ ::operator delete(__p); }
};

Then, in "__default_alloc_template", we can find its implementation of
"allocate", "deallocate" and "reallocate":

// in class "__default_alloc_template"
static void* allocate(size_t __n)
{
...
if ((__n > (size_t) _MAX_BYTES) || (_S_force_new > 0))
__ret = __new_alloc::allocate(__n); // uses "new"
...
}

static void deallocate(void* __p, size_t __n)
{
if ((__n > (size_t) _MAX_BYTES) || (_S_force_new > 0))
__new_alloc::deallocate(__p, __n); // uses "delete"
...
}

static void* reallocate(void* __p, size_t __old_sz, size_t __new_sz)
{
...
if (__old_sz > (size_t) _MAX_BYTES && __new_sz > (size_t)
_MAX_BYTES)
return(realloc(__p, __new_sz)); // uses "realloc"
...
}

When there's a memory block that is bigger than _MAX_BYTES, both
"allocate" and "deallocate" use C++ new-based statement to handle it,
but only "reallocate" uses C function "realloc".

What's the reason for this inconsistency? And what if we pass a "__p"
allocated by "new" into the member function "reallocate"?

Thanks.
Jul 22 '05 #1
0 1028

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

Similar topics

2
by: gbs | last post by:
I have been using gcc 3.3 for a long time. Today I tired building my software with version 3.4.1. One problem I found was that __default_alloc_template was gone. What has replaced it? (if anything)...
9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
26
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() {...
10
by: cijo | last post by:
Hi, My program has a pointer to a structure declared locally inside a function to which memory is allocated using malloc and reallocated later using realloc, and when I try to free the memory at...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
23
by: James Brown | last post by:
Hi all, I just wanted to make sure I understand realloc void *realloc( void *memblock, size_t size ); 1. when memblock is zero, realloc behaves like malloc 2. when memblock is non-zero, and...
16
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
31
by: banansol | last post by:
Hi, I just want to get this right. A call to realloc() will return NULL on error and the original memory is left untouched, both when requesting a larger or a smaller size that the original,...
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: 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
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.