473,804 Members | 3,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Heap maintenance and wordsize

rd

Hi i'm uncertain about foll. things. Thanks in advance for any help to
get me out of this..

Each process, with their own stack, will they have their own heap or is it global one(common for all process)? if local,
Any Limit on heap size like stack?

if global,
what if u dont free()?
How heap is initialized and maintained?? (any reference/link for
heap/stack maintenace)

WORDSIZE:
what is the relation between sizeof(int) in C and wordsize of an OS?(will it be 4 bytes, if u run gcc now in older OSs like WIN95 (16
bit)..)
I dont understand the context clearly, and i myself build lot of ambiguous things, when it is said 32 bit os, 32 bit compiler, wordsize,... help me to get out of it.

MALLOC:

(this one lasted for 40 mins..) Consider following sample..


char *arr=malloc(50) ;

it is not guarenteed that memory for 'arr' will be contigous.. and
when you use

for(i=0;i<50;i+ +) {.. arr[i++]; ... }

how the indexing will be actually reflected in physical memory..? we
just increment like arr+25,arr+26,. . and what if memory is not
contigous?

Thanks
Deva

Nov 14 '05 #1
4 1740

"rd" <ma*********@gm ail.com> wrote
Each process, with their own stack, will they have their own heap or is
it >>>global one(common for all process)? if local,
Any Limit on heap size like stack?

if global,
what if u dont free()?

Depends on your memory manager incorporated into the OS and how effectively
it is used by a compiler. A decent system will have a pool of memory on
which all programs can draw and return on demand, but prevent any one
program from hogging the whole lot. Bad systems often give programs memory
on demand, but then don't let them return it until they terminate.
A terrible system may tie up unfreed memory until reboot. You've got to
either be very unlucky or be using a hacked together OS for a small system
for this to happen.
How heap is initialized and maintained?? (any reference/link for
heap/stack maintenace)
Totally system dependent. Normally the OS keeps track of "pages" which it
gives to each program. The malloc() routine then stitches those pages
together to provide a nice flat memory space for the caller.
char *arr=malloc(50) ;

it is not guarenteed that memory for 'arr' will be contigous.. and
when you use

for(i=0;i<50;i+ +) {.. arr[i++]; ... }

how the indexing will be actually reflected in physical memory..? we
just increment like arr+25,arr+26,. . and what if memory is not
contigous?

Normally there would be hardware support for a lookup system. In the olden
days the bits in a pointer usually reflected the physical signals sent on
the pins to the memory chip. Nowadays they will usually go through several
layers of indirection, so arr[25] and arr[26] might be located on entirely
different chips, but the pointer values are contiguous.
Nov 14 '05 #2
rd wrote:
char *arr=malloc(50) ;

it is not guarenteed that memory for 'arr' will be contigous.. and
when you use
It's guaranteed on the abstract machine
and that's as guaranteed as anything gets in C.
for(i=0;i<50;i+ +) {.. arr[i++]; ... }

how the indexing will be actually reflected in physical memory..?


In whatever way it takes to prevent a C program
form being able to determine noncontiguity.

--
pete
Nov 14 '05 #3
# >>Each process, with their own stack, will they have their own heap or is it global one(common for all process)?
# if local,
# Any Limit on heap size like stack?
#
# if global,
# what if u dont free()?

Alot depends on what a 'process' is. On something like Unix, each process
has its own address space that cannot be easily modified by any other process.
On something Macintosh System 7 processes clobberring other processes's
memory is a frequent problem. So this depends on the underlying system and
cannot be answerred definitively.

There is some limit to heap size because machines have finite address space
and finite real memory and finite disc space. How much of those fundamental
limitations you can exploit depends on your system.

Resource scavenging on process exit depends on the system: some do,
some don't.

# How heap is initialized and maintained?? (any reference/link for
# heap/stack maintenace)

If you use the stdlib.h functions like malloc and free, the heap is
automagically initialised and maintained for you. Pay no attention to
the library behind the curtain.

# WORDSIZE:
#
# >> what is the relation between sizeof(int) in C and wordsize of an
# OS?(will it be 4 bytes, if u run gcc now in older OSs like WIN95 (16
# bit)..)

Usually they are the same. Whether they are two bytes or four or eight
or something else varies system by system. The situation is confusing
because the computer world is in flux between 32 and 64 bit words
and 8 and 16 bit characters.

# MALLOC:
#
#
# >> (this one lasted for 40 mins..) Consider following sample..
#
# char *arr=malloc(50) ;
#
# it is not guarenteed that memory for 'arr' will be contigous.. and
# when you use

The addresses you use are consecutive. Whether that is mapped into
noncontiguous physical addresses somewhere underneath is not the concern
of the C program; any such mapping must be invisible to your code such
that

# for(i=0;i<50;i+ +) {.. arr[i++]; ... }

works the same on all platforms.

If you do something like
typedef struct {char a,b,c;} abc;
abc *arr = malloc(50*sizeo f(abc));
arr might be allocated 150 or 200 (or elsewise) bytes. The compiler is
allowed to pad out the struct to a more convenient boundary, perhaps
adding one more invisible of char. Each array element is at consecutive
indexes, arr[0], arr[1], arr[2], ..., arr[49], but doesn't mean all
150 chars are contiguous.

That means if you want do something like zeroing out the array, you
should use sizeof
memset(arr,0,50 *sizeof(abc))
instead of trying to outguess the compiler
memset(arr,0,15 0)

--
SM Ryan http://www.rawbw.com/~wyrmwif/
The whole world's against us.
Nov 14 '05 #4
On Sat, 18 Jun 2005 10:14:53 GMT, pete
<pf*****@mindsp ring.com> wrote:
rd wrote:
char *arr=malloc(50) ;

it is not guarenteed that memory for 'arr' will be contigous.. and
when you use


It's guaranteed on the abstract machine
and that's as guaranteed as anything gets in C.


It depends what you mean by 'memory'. The C standard gauarantees that a
C program can access the memory as though it were contiguous, but since
it describes an "abstract machine" it doesn't say anything about where
that memory 'really' is (it could be split over adjacent -r separated
tracks on a drum, for instance, or across several delay lines -- or on
machines separated by thousands of miles).
for(i=0;i<50;i+ +) {.. arr[i++]; ... }

how the indexing will be actually reflected in physical memory..?


In whatever way it takes to prevent a C program
form being able to determine noncontiguity.


There is no way that a C program can know where the 'physical' memory is
'actually' located. Or whether it is even still there when it isn't
being accessed. All it knows is that as far as it is concerned an
allocated area of memory behaves as if it is contiguous.

(In some operating systems there are of course ways to 'lock' logical
memory to physical memory, but those are off-topic for c.l.c as they are
outside te standard.)

Chris C
Nov 14 '05 #5

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

Similar topics

0
5881
by: ANt | last post by:
Hi, we have some major GC issues at present with a system we're trying to put live. It's a live calculation engine that's distributed across about 30 Java server processes. A set of processes called objectservers sit between the core infra and a Sybase DB. Two of the main objectservers are set up with 2Gb Heaps as they need to cache large amounts of objects and accept 500-600 incoming RMI connections when heavily loaded. We're running the...
14
30103
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
17
5054
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
2
7530
by: mordac | last post by:
Hello, I was wondering if I could get some opinions on how best to handle printing in a max heap data structure. Right now my heap struct looks as thus: typedef struct heapStruct { int* heapArray; int heapSize; int arraySize; int maxSize; } heapStruct; arraySize holds the current number of elements in the array.
16
4451
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
10
5616
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
0
13329
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. Examples of two main problem scenarios are like this: 1) an array containing a first name and another array containing a last name and possibly a third array containing the age of a person; how to sort the
5
24835
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
4
9595
by: ggoubb | last post by:
The purpose of the Insert function is to add a new integer in the Heap assuming that it is not already full. If Heap capacity has been reached, it attempts to double the current capacity. If capacity cannot be doubled, it throws FullHeap. Here is the Heap.h file const int MAXSIZE = 4; // Default maximum heap size class Heap // Smart Heap ADT as an array { private: int* ptr; ...
0
9584
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
10583
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
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
6854
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();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
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.