473,659 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'new'd memeroy is contiguous?

No pun intended in the subject :)

Is dynamically allocated memory contiguous in C++? In C? Deails would
be appreciated.
warm regards,
Divya Rathore
(remove underscores for email ID)

Dec 19 '05 #1
22 3082
di************@ gmail.com wrote:
No pun intended in the subject :)
memeroy?
Is dynamically allocated memory contiguous in C++? In C? Deails would
be appreciated.


Yes, it is. What details do you expect?
Dec 19 '05 #2
On 19 Dec 2005 09:04:23 -0800, "di************ @gmail.com"
<di**********@g mail.com> wrote:
Is dynamically allocated memory contiguous in C++? In C? Details would
be appreciated.


C questions are off-topic here unless they are somehow also relevant
to C++ (comp.lang.c is the place to ask), so I'll limit myself to the
question concerning "new".

Do you mean, contiguous across different calls to new? If so, the
answer is: not necessarily, because it is implementation-defined how
new obtains and utilizes its raw memory. I would say, no, because
unused blocks of memory released by calling delete are normally
recycled within the total memory used by the process taken from its
free store (i.e. heap memory) provided by the operating system. Often,
the memory is only returned to the operating system when the process
ends, but that is also an implementation-specific detail which can be
quite different on another compiler and operating system.

A class can also overload new and delete to use a different memory
allocation scheme.

Otherwise, I don't really understand your question. Allocations
performed by new allocate a single object of a given type. Allocations
performed by new[] allocate arrays of objects of the same type.
Although the memory occupied by the array is guaranteed to be
contiguous, and the memory occupied by any given object is contiguous,
it is implementation-defined how the compiler lays out the individual
members of the object in that memory. The implementation (i.e. your
compiler) is free to add padding bytes between the individual members
for purposes of alignment and can be influenced by different
optimization and alignment settings at compile-time (e.g., see the
documentation for "#pragma pack" if your compiler supports it).

Consider the following:

struct X {
short a;
char b;
int c;
};

What is sizeof X here? Depending on the compiler settings, you will
most likely get an additional byte of padding thrown in after the char
member. However, it is also possible that there are two extra bytes
after the short member as well. Switch around the order of the members
and see if you get different results.

Of course, an instance of a class can allocate additional memory and
store just the pointer to that memory. When querying the size of such
an object with sizeof(), only the memory occupied by the pointer would
count towards the total size of the object.

If you want more details, I suggest you get a copy of the C++ standard
at: http://www.ansi.org

--
Bob Hairgrove
No**********@Ho me.com
Dec 19 '05 #3
On Mon, 19 Dec 2005 12:26:41 -0500, Victor Bazarov
<v.********@com Acast.net> wrote:
di************ @gmail.com wrote:
No pun intended in the subject :)


memeroy?


Just a typo or misspelling.
I think the pun was with "new'd" and "nude" (groan...)

--
Bob Hairgrove
No**********@Ho me.com
Dec 19 '05 #4
Bob Hairgrove wrote:
On Mon, 19 Dec 2005 12:26:41 -0500, Victor Bazarov
<v.********@com Acast.net> wrote:

di*********** *@gmail.com wrote:
No pun intended in the subject :)


memeroy?

Just a typo or misspelling.
I think the pun was with "new'd" and "nude" (groan...)


Ah... OK. I was thinking of "hemorrhoid s".
Dec 19 '05 #5
di************@ gmail.com wrote:
No pun intended in the subject :)

Is dynamically allocated memory contiguous in C++?
Yes.
In C?
Yes.
Deails would be appreciated.


What details do you want? It's contiguous, that's how you have dynamic
arrays.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 19 '05 #6
Thank you all..
so, in:
int* x;
x = new int[MAX];

memory allocated is contiguous for how-so-ever-large MAX (till system
supports)?

Dec 19 '05 #7
>> Is dynamically allocated memory contiguous in C++? In C? Deails would
be appreciated.

Yes, it is. What details do you expect?


As someone pointed, asking about malloc/calloc is out of scope of this
newsgroup, so I will restrain myself.
Thanks anyways.

Dec 19 '05 #8
di************@ gmail.com wrote:
Is dynamically allocated memory contiguous in C++? In C? Deails would
be appreciated.

Yes, it is. What details do you expect?

As someone pointed, asking about malloc/calloc is out of scope of this
newsgroup, so I will restrain myself.


Actually, since C Standard Library circa 1990 is part of C++ Standard
Library, it's not out of scope to talk about malloc or calloc. Please
ask your library questions as you wish, it's absolutely _in_ the scope
of this newsgroup. C _language_ questions are better asked in
comp.lang.c, however.

V
Dec 19 '05 #9
On 19 Dec 2005 12:18:29 -0800, "di************ @gmail.com"
<di**********@g mail.com> wrote:
Thank you all..
so, in:
int* x;
x = new int[MAX];

memory allocated is contiguous for how-so-ever-large MAX (till system
supports)?


The limit on the array size (MAX) is not specified by the C++
standard. If the call to new succeeds, x will be a valid pointer to
that memory. Otherwise, an exception (std::bad_alloc ) is thrown.

If you need to allocate very large sections of RAM, e.g. for image
processing, you can also do it with operating-system specific means
and use placement new.

--
Bob Hairgrove
No**********@Ho me.com
Dec 19 '05 #10

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

Similar topics

822
29363
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical real-time applications. The majority of expert/people recommend Ada for safety critical real-time applications. I've many years of experience in C/C++ (and Delphi) but no Ada knowledge. May I ask if it is too difficult to move from C/C++ to Ada?...
9
7053
by: Olumide | last post by:
Thats the question. I know about virtual memory, and the MMU. I just wonder if array members guaranteed to be contiguous in physical memory (and if so, why). Thanks, Olumide
5
6650
by: Stefan Krah | last post by:
Hello, I am currently writing code where it is convenient to convert char to int . The conversion function relies on a character set with contiguous alphabets. int set_mesg(Key *key, char *s) { char *x;
38
5121
by: Peteroid | last post by:
I looked at the addresses in an 'array<>' during debug and noticed that the addresses were contiguous. Is this guaranteed, or just something it does if it can? PS = VS C++.NET 2005 Express using clr:/pure syntax
9
12366
by: divya_rathore_ | last post by:
Consider the dynamic allocation of a 2D array: int **temp; temp = new int*; for (y=0; y<height; y++) temp = new int; I have 2 Questions: Does new initializes memory to 0?
7
1332
by: MikeB | last post by:
I realize that arrays have worked this way for ages, but why do we have to continue with the old ways? It would really be nice if you could add a new row (element) to an array one at a time (as required). In some cases, the programmer doesn't know how many objects that will be loaded into an array until runtime. I know you can process the source to find out how large it is and use this to update a variable that was used to initialize...
22
2611
by: Jack | last post by:
The following code can be compiled. But When I run it, it causes "Segmentation fault". int main(){ char **c1; *c1 = "HOW"; // LINE1 ++(*c1); *c1 = "ARE";
9
2055
by: jmcgill | last post by:
Saw this used as an example. Compiles without warnings. "Works." Kosher or not? Why or why not? #include <stdio.h> int main(int argc, char **argv){ int i,j,k,l,m; int *p; i=2; j=4; k=8; l=32; m=64; /*any values*/ p = &i;
0
8339
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.