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

Usage of RAM related to C++ programming

I have always indirectly know how RAM is used when a C++ program is
run, but I wanted to be sure I got it right:

First the OS copies the whole program to RAM. Every statement of the
kind int x; would make the OS to look for free RAM to storage the value
of x. Every statement like cout << something would cause the OS to
store "something" in RAM so it can be outputed. The OS is ready to free
memory as it moves outside the scope of variables.

So when a program says it has a minimun requirement of X RAM it means
that the most RAM used at any given time is never more than X.

Is this an accurate picture?

Feb 6 '06 #1
8 1657
Gaijinco wrote:
I have always indirectly know how RAM is used when a C++ program is
run, but I wanted to be sure I got it right:
It depends on your target platform there is nothing in the C++ standard
concerning RAM or it's use.
First the OS copies the whole program to RAM. Every statement of the
kind int x; would make the OS to look for free RAM to storage the value
of x. Every statement like cout << something would cause the OS to
store "something" in RAM so it can be outputed. The OS is ready to free
memory as it moves outside the scope of variables.
You'd be better asking on a newsgroup for your OS, but the above isn't a
very good description of any environment I know...
So when a program says it has a minimun requirement of X RAM it means
that the most RAM used at any given time is never more than X.

Probably.

--
Ian Collins.
Feb 7 '06 #2
Gaijinco wrote:
I have always indirectly know how RAM is used when a C++ program is
run, but I wanted to be sure I got it right:

First the OS copies the whole program to RAM. Every statement of the
kind int x; would make the OS to look for free RAM to storage the value
of x.
Not really. The statement

int x;

allocates memory for x either from static memory or stack memory. See
below for more explanations.
Every statement like cout << something would cause the OS to
store "something" in RAM so it can be outputed.
When you have a statement such as:

cout << x;

You are calling an overloaded operator to do the output for you. You
know that either the value or the address of x is copied to the stack
frame of the routine (operator <<) being invoked. What the routine does
is up to the implementation. Usually, the routine converts the value of
x to a text string and sends the string to the OS for output.
The OS is ready to free
memory as it moves outside the scope of variables.
The OS does not free memory for you. Your program frees memory for
itself. That said, when your program frees a piece of dynamic memory, it
typically does request the OS to "unplug" the memory from the program's
memory space. See below for details.

So when a program says it has a minimun requirement of X RAM it means
that the most RAM used at any given time is never more than X.

Is this an accurate picture?


Forget about what you said. C++ basically deals with 3 kinds of memory:
static memory, stack memory, and dynamic memory.

Static memory is used to store global variables, static variables and
some constants. They can be used through out the program. This memory is
typically part of the program image so it is loaded along with the
program because the compiler always know the exact size of this piece of
memory. In addition, they are loaded to a different segment than the
program texts.

Stack memory is used to store local variables and data necessary to
facilitate function invocation (such as arguments, return address and
return value.) All local variables are deallocated in the reverse order
of allocation once they are out of scope. This memory is usually not
part of the on-disk program image and is created by the OS when the
program is loaded into RAM. Typically, this memory is loaded to the
stack segment with a fixed size. The stack memory is usually large
enough for most operations, but you can overflow it if not careful.

Dynamic memory is used to store "free-store" variables, such as those
created by operator new. The dynamic memory is not part of the program
image, nor is it allocated at load time. Rather, it is brought into the
program's memory space by the OS upon runtime requests (such as calling
operator new().)

For example consider the following program under a typical
implementation and platform:

int a = 1;

int main(void)
{
int b = 2;
int* c = new int(3);

delete c;
}

The global variable a is allocated statically. b is allocated in main's
stack frame when the flow of control enters main. The pointer c is also
allocated in main's stack frame, but the content it points to is
allocated in the dynamic memory by operator new.

If you wish to know more you should consider reading on assembly
language because that's the language that explains all of the memory
arrangements.

Regards,
Ben
Feb 7 '06 #3
On Tue, 07 Feb 2006 13:43:54 +1100, benben <be******@yahoo.com.au>
wrote:
If you wish to know more you should consider reading on assembly
language because that's the language that explains all of the memory
arrangements.

Regards,
Ben


Well done. Kudos to you sir.

That was a "blast from the past." And that's a Good Thing.

"People who never get carried away should be." - Malcolm Forbes :-)
Feb 7 '06 #4
Thanks a lot for the information!
Stack memory is used to store local variables and data necessary to
facilitate function invocation (such as arguments, return address and
return value.) All local variables are deallocated in the reverse order
of allocation once they are out of scope. This memory is usually not
part of the on-disk program image and is created by the OS when the
program is loaded into RAM. Typically, this memory is loaded to the
stack segment with a fixed size. The stack memory is usually large
enough for most operations, but you can overflow it if not careful.


Do you mean that Stack Memory is always given the same size or is just
calculated once the program is compiled?

By overflowing you mean something like creating a class like:

class Person{
char name[1000];
char last_name[1000];
int telephone[1000];
};

and then creating with a loop 1'000.000 Person objects?

Also, and to be sure I understood you right, Static, Stack and Dynamic
Memory are part of the RAM, right? So there's no chance of trying to
write to L1 or L2 cache without Assembler?

Feb 7 '06 #5
Gaijinco wrote:
Stack memory is used to store local variables and data necessary to
facilitate function invocation (such as arguments, return address and
return value.) All local variables are deallocated in the reverse order
of allocation once they are out of scope. This memory is usually not
part of the on-disk program image and is created by the OS when the
program is loaded into RAM. Typically, this memory is loaded to the
stack segment with a fixed size. The stack memory is usually large
enough for most operations, but you can overflow it if not careful.

Do you mean that Stack Memory is always given the same size or is just
calculated once the program is compiled?

Stack size (assuming a stack) is (typicaly) determined by the operating
environment, not the application.
By overflowing you mean something like creating a class like:

class Person{
char name[1000];
char last_name[1000];
int telephone[1000];
};

and then creating with a loop 1'000.000 Person objects?
Yes, if a stack has finite size, it can be overflowed.
Also, and to be sure I understood you right, Static, Stack and Dynamic
Memory are part of the RAM, right? So there's no chance of trying to
write to L1 or L2 cache without Assembler?

Who said anything about a cache? This is totally implementation specific.

--
Ian Collins.
Feb 7 '06 #6
> Do you mean that Stack Memory is always given the same size or is just
calculated once the program is compiled?
It really depends on your compiler/linker and your operating system.
Note that the maximum stack size is extremely difficult, if not
impossible, to calculate. Usually your compiler/linker/OS would give a
size good for the majority of applications.

For example, on Windows each program gets 4Mb of stack memory, although
there are APIs to up-size it.

By overflowing you mean something like creating a class like:

class Person{
char name[1000];
char last_name[1000];
int telephone[1000];
};

and then creating with a loop 1'000.000 Person objects?
A size and a loop doesn't take up much stack space. This is because the
local objects within the loop are deallocated every iteration.

That said, you can easily overflow your stack in two ways:

1. Huge local array.
2. Recursive calls.

(Note: more effective if the above two are combined.)

Also, and to be sure I understood you right, Static, Stack and Dynamic
Memory are part of the RAM, right? So there's no chance of trying to
write to L1 or L2 cache without Assembler?


They are all "memory", which means Primary Memory, which means memory
that the CPU can directly access and operate on. Whether it includes
anything else is up to the context.

Yes you need an assembler to do low-level coding like writing to CPU
cache and I/O.

Regards,
Ben
Feb 7 '06 #7
benben wrote:
<somebody else>
Also, and to be sure I understood you right, Static, Stack and Dynamic
Memory are part of the RAM, right? So there's no chance of trying to
write to L1 or L2 cache without Assembler?


They are all "memory", which means Primary Memory, which means memory
that the CPU can directly access and operate on. Whether it includes
anything else is up to the context.

Yes you need an assembler to do low-level coding like writing to CPU
cache and I/O.


Can you write directly to cache? Hardcore :)

That you need assembler to write to write to IO is surely misleading.
I've used C to access (memory mapped) IO directly on at least 3
microcontrollers (8051, C167, TriCore).

You would need an assembler to write directly to specific CPU registers,
but that doesn't stop the C compiler taking your code and organising
what should go in what specific register.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 7 '06 #8
> Can you write directly to cache? Hardcore :)

Some hardware do have software layers.

That you need assembler to write to write to IO is surely misleading.
I've used C to access (memory mapped) IO directly on at least 3
microcontrollers (8051, C167, TriCore).
Otherwise you need specific CPU instruction and interrupt handling.

You would need an assembler to write directly to specific CPU registers,
but that doesn't stop the C compiler taking your code and organising
what should go in what specific register.
....But not in control of the programmer.

Ben Pope


Regards,
Ben
Feb 7 '06 #9

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

Similar topics

0
by: Ronen | last post by:
Hi All, I am running a client against a web application installed on a remote server written in C# which involve infragistics for GUI . The .NET framework I use is from version 1.1.4322 and we...
11
by: Rajeev | last post by:
Dear friends I am conducting a survey on Relational Database usage and would like your help. The study is part of my MBA Dissertation. Could you kindly spare 5 minutes to take part in this...
7
by: George Gre | last post by:
Hi, I wrote a c# programme that listens to incoming TCP requests and services them. This programme is meant to be running as long as the server its installed on is running. So we assume for...
26
by: Bruno Jouhier [MVP] | last post by:
I'm currently experiencing a strange phenomenon: At my Office, Visual Studio takes a very long time to compile our solution (more than 1 minute for the first project). At home, Visual Studio...
5
by: SDS | last post by:
I am writing an ASP.NET application (in C#) that, as part of a particular response, populates a MemoryStream object with binary data that is being collected from a Process object's StandardOutput. ...
72
by: Robin Tucker | last post by:
I need to find some documents/research for my manager about VB.NET v C# use. I've noticed that there are many more people using C# than VB.NET, that there seem to be more job vacancies specifying...
22
by: Paul | last post by:
Hi I am having a real issue with CPU usage by SQL Server, and it is not related to a poor query. I have a clients database that I am currently investigating some issues with. After I perform...
9
by: clintonG | last post by:
The Barnes & Noble stores in and around Milwaukee, Wisconsin have removed or are removing all of the Microsoft platform programming and support magazines from their shelves. Not one single magazine...
2
by: Ciur Eugen | last post by:
Hi ! If I will make char* pHugeMemory = (char*)malloc( huge_number_of_bytes ); in some place of my program, and after some time my program will jump to other processing place, that won't...
16
by: jayapal | last post by:
hi all, what is the differrence b/w the usage or return and the exit in the C programming.. thanks, jay
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.