473,320 Members | 1,902 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.

Some advice about references on memory management in C

I can comprehend the basics of that subject (pointers, memory
allocation, heap, stack etc), but I am interested to have
references with more details. I have some C/C++ books
and lots of URLs "bookmarked", but all of them don't go too
far on memory management as I would like. One of my main issues
is whether function "main" and its local variables are allocated
on the stack area as other functions.

Does anybody recommend a good book or site with a good text
on memory management in C/C++?

In comp.lang.c++ someone recommended "C++ Pointers and Dynamic
Memory Management", by Michael C. Daconta. Is it good?

Does anybody know the book "Memory as a programming
concept in C and C++" (Cambridge Press - link below)?
(http://titles.cambridge.org/catalogu...sbn=0521520436)
There are some resources here
"http://www.cas.mcmaster.ca/~franek/books.html".
However, I'd like to have some advice about the whole book.

Regards and TIA,
Rafael Charnovscki
Nov 14 '05 #1
9 1990

"Rafael Charnovscki" <rc*@unesc.net> wrote in message
news:cd*************************@posting.google.co m...
I can comprehend the basics of that subject (pointers, memory
allocation, heap, stack etc), but I am interested to have
references with more details. I have some C/C++ books
and lots of URLs "bookmarked", but all of them don't go too
far on memory management as I would like. One of my main issues
is whether function "main" and its local variables are allocated
on the stack area as other functions.


It's really not that hard.

As a rule of thumb:

All local variables are located on stack, as are parameters to functions,
global variables are located in the .data (or equivalent)segment and
everything you alloc() on the heap.

The .data segment (that's what they call it in UNIX, forgive me) is loaded
with the rest of your program (memory is allocated by your OS) so you can
'initialize' that data by just assigning a value to it.

int global_a = 42;

void foobar(int parameter_b)
{
void* local_c;

local_c = malloc(0x0100)

...
}

then global_a would be in your .data segment, parameter_b and local_c would
be on stack and *local_c would be in the heap.

I cannot give any advice on books, but I could reccomend a decent debugger
and a simple sample program so you can check for yourself.

Regards,

dandelion
Nov 14 '05 #2
Hi there,
dandelion wrote:
"Rafael Charnovscki" <rc*@unesc.net> wrote in message
news:cd*************************@posting.google.co m...
I can comprehend the basics of that subject (pointers, memory
allocation, heap, stack etc), but I am interested to have
references with more details. I have some C/C++ books
and lots of URLs "bookmarked", but all of them don't go too
far on memory management as I would like. One of my main issues
is whether function "main" and its local variables are allocated
on the stack area as other functions.

It's really not that hard.

As a rule of thumb:

All local variables are located on stack, as are parameters to functions,
global variables are located in the .data (or equivalent)segment and
everything you alloc() on the heap.

The .data segment (that's what they call it in UNIX, forgive me) is loaded
with the rest of your program (memory is allocated by your OS) so you can
'initialize' that data by just assigning a value to it.

int global_a = 42;

void foobar(int parameter_b)
{
void* local_c;

local_c = malloc(0x0100)

...
}

then global_a would be in your .data segment, parameter_b and local_c would
be on stack and *local_c would be in the heap.

I cannot give any advice on books, but I could reccomend a decent debugger
and a simple sample program so you can check for yourself.


Note, however, that the C standard does not say anything about heap or
stack, let alone .data. That is, one cannot rely on things being like
described, even though the description gives you a good idea of
how things are done on a specific platform.

Another book which can give you a good idea of how addresses are mapped
and so on, is "Linkers and Loaders". I do not have the link right
now, but there is an alpha version of the book out there for free
(legally).

Once again: This information does not hold for all standard compliant
C environments. If you have a certain system and a certain compiler
in mind, you may want to ask in the respective newsgroups.
Cheers,
Michael

Nov 14 '05 #3
rc*@unesc.net (Rafael Charnovscki) wrote in message news:<cd*************************@posting.google.c om>...
I can comprehend the basics of that subject (pointers, memory
allocation, heap, stack etc), but I am interested to have
references with more details. I have some C/C++ books
and lots of URLs "bookmarked", but all of them don't go too
far on memory management as I would like. One of my main issues
is whether function "main" and its local variables are allocated
on the stack area as other functions.

The problem is that not all machines are stack-based. A good number
of them are, but the C language doesn't require it.

AFAIK, main() is treated like any other function as far as how it's
data is managed, but the details may vary based on the platform. You
might be better off asking this kind of question in a newsgroup
devoted to your particular architecture.
Does anybody recommend a good book or site with a good text
on memory management in C/C++?

In comp.lang.c++ someone recommended "C++ Pointers and Dynamic
Memory Management", by Michael C. Daconta. Is it good?

Does anybody know the book "Memory as a programming
concept in C and C++" (Cambridge Press - link below)?
(http://titles.cambridge.org/catalogu...sbn=0521520436)
There are some resources here
"http://www.cas.mcmaster.ca/~franek/books.html".
However, I'd like to have some advice about the whole book.

Regards and TIA,
Rafael Charnovscki

Nov 14 '05 #4
On Thu, 7 Oct 2004 16:56:20 +0200, "dandelion" <so*****@meadow.net>
wrote in comp.lang.c:

"Rafael Charnovscki" <rc*@unesc.net> wrote in message
news:cd*************************@posting.google.co m...
I can comprehend the basics of that subject (pointers, memory
allocation, heap, stack etc), but I am interested to have
references with more details. I have some C/C++ books
and lots of URLs "bookmarked", but all of them don't go too
far on memory management as I would like. One of my main issues
is whether function "main" and its local variables are allocated
on the stack area as other functions.
It's really not that hard.

As a rule of thumb:

All local variables are located on stack, as are parameters to functions,


....unless of course you are using an efficient compiler for a modern
processor architecture, and then at least some of the parameters and
local variables are entirely in registers. Possibly all of them,
depending on how many there are.
global variables are located in the .data (or equivalent)segment and
everything you alloc() on the heap.
....unless of course they are default initialized, in which case they
are more likely to be in another section.
The .data segment (that's what they call it in UNIX, forgive me) is loaded
with the rest of your program (memory is allocated by your OS) so you can
'initialize' that data by just assigning a value to it.


You seem to have forgotten about file scope and block scope values
with static storage duration. Not to mention string literals and
constant objects.

The true answer is that there is no rule of the thumb. If it made
sense for there to be a rule of the thumb, it would be in the C
standard, which it most certainly is not.

The only answer to a question like this, from a language stand point,
is to direct the OP to a group that specialized in the particular
OS/compiler combination that interests him. It may be quite a bit
different from others, and from the model you are familiar with.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
In article <cd*************************@posting.google.com> ,
Rafael Charnovscki <rc*@unesc.net> wrote:
One of my main issues
is whether function "main" and its local variables are allocated
on the stack area as other functions.


In typical implementations on general-purpose computers, the function
main() is called in the usual way from some other function, which also
handles calling exit() if main() returns.

-- Richard
Nov 14 '05 #6

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:op********************************@4ax.com...
On Thu, 7 Oct 2004 16:56:20 +0200, "dandelion" <so*****@meadow.net>
wrote in comp.lang.c:

"Rafael Charnovscki" <rc*@unesc.net> wrote in message
news:cd*************************@posting.google.co m...
I can comprehend the basics of that subject (pointers, memory
allocation, heap, stack etc), but I am interested to have
references with more details. I have some C/C++ books
and lots of URLs "bookmarked", but all of them don't go too
far on memory management as I would like. One of my main issues
is whether function "main" and its local variables are allocated
on the stack area as other functions.
It's really not that hard.

As a rule of thumb:

All local variables are located on stack, as are parameters to functions,
...unless of course you are using an efficient compiler for a modern
processor architecture, and then at least some of the parameters and
local variables are entirely in registers. Possibly all of them,
depending on how many there are.
global variables are located in the .data (or equivalent)segment and
everything you alloc() on the heap.
...unless of course they are default initialized, in which case they
are more likely to be in another section.


Hence "(or equivalent)".
The .data segment (that's what they call it in UNIX, forgive me) is loaded with the rest of your program (memory is allocated by your OS) so you can 'initialize' that data by just assigning a value to it.


You seem to have forgotten about file scope and block scope values
with static storage duration. Not to mention string literals and
constant objects.


Ummm...

Scoping (in this case and to my knowledge) is dermined by the kind of record
generated by the compiler and is ultimately delt with by the linker.

You seem to have forgotten the little "As a rule of thumb" I put in front of
my remarks.
The true answer is that there is no rule of the thumb.
Not one that is generally valid or refelcts exactly what the standard says,
but rules of thumb (i.e. inaxact, but workable) do exist. The above works
well wen explaining stuff to (relative) newbies.
If it made
sense for there to be a rule of the thumb, it would be in the C
standard, which it most certainly is not.
Standards and "rules of thumb" are two extremey different things, one seeks
to define exactly and exhaustively, the other seeks to give general rules,
which are easy to work with, eventhough they are not always and exactly
true.

I would be very disappointed if standards contained "rules of thumb".
Standards are intended for those who write compilers, rules of thumb for
newbies and regular users who want a rough idea without worrying about the
exact details.
The only answer to a question like this, from a language stand point,
is to direct the OP to a group that specialized in the particular
OS/compiler combination that interests him.
Perhaps. In that case you can just sit back, delegate all practical
questions to some other n.g. and lock yourself in an ivory tower of language
abstractions and formulations in The Standard, I presume.

I can see how that would be attractive. I, personally, prefer to give
answers (to specific questions) people can work with, eventhough the details
may be less than exact.
It may be quite a bit different from others, and from the model you are

familiar with.

That's quite a lot of models, platforms and languages and dialects, from
assemby to Prolog, from Sun Supersparc and PowerPC to (refurbished) 68000,
Z80, AVR, PIC and 68HC11.

Regards,

dandelion.
Nov 14 '05 #7

"John Bode" <jo*******@my-deja.com> wrote in message
news:43**************************@posting.google.c om...

<snip>
The problem is that not all machines are stack-based. A good number
of them are, but the C language doesn't require it.


Noted. However, I am still pondering the architecture in use today which
does not. I know about IBM 360, but those are ancient (in computer terms).
Nov 14 '05 #8
On Mon, 11 Oct 2004 12:25:49 +0200
"dandelion" <so*****@meadow.net> wrote:
"John Bode" <jo*******@my-deja.com> wrote in message
news:43**************************@posting.google.c om...

<snip>
The problem is that not all machines are stack-based. A good number
of them are, but the C language doesn't require it.


Noted. However, I am still pondering the architecture in use today
which does not. I know about IBM 360, but those are ancient (in
computer terms).


Ancient in computer terms does *not* mean that it is no longer in use. I
don't know about the IBM 360 or other machines that are not stack based,
but I do know (from personal involvement) of systems based on the Z80
processor programmed in assembler being updated and sold to new a new
large customer in the late 90s and of another customer who signed for an
option of 20 years of support on another variation. So if we will have
Z80 based systems with an expected end of life after 2015 it is possible
you could find any system since the C89 standard still in use and
undergoing active maintenance. So it is important when learning that one
learns which assumptions cannot be made.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #9
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dandelion wrote:
"John Bode" <jo*******@my-deja.com> wrote in message
news:43**************************@posting.google.c om...

<snip>
The problem is that not all machines are stack-based. A good number
of them are, but the C language doesn't require it.

Noted. However, I am still pondering the architecture in use today which
does not. I know about IBM 360, but those are ancient (in computer terms).


System 360 /is/ ancient, but S/390 is only /old/, and zSeries is practically a
baby. *But*, you can compile and run a C program on each of these IBM
S/360-compatable processors without problems. No stack necessary.

And, the architecture may be ancient, but we still use it for a large (> 50%)
of big business computing. Your bank, your hospital, and your local government
all use these machine.
- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBauyWagVFX4UWr64RAvVgAKDUIN39+EG/9eqaHtA0gbYN0FuAOwCggoBu
m+uQKwxBic+CvwuYPhkGV58=
=MmS+
-----END PGP SIGNATURE-----
Nov 14 '05 #10

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

Similar topics

4
by: J-P | last post by:
Hi, I have a Python script interacting with a specialized C numerical library. The whole program does quite a lot of number crunching as should be running for a couple of hours. However, it...
0
by: Richard Jones | last post by:
Garbage Collection & Memory Management Summer School 20-21 July 2004, Canterbury, UK The performance of today's memory-hungry applications depends on efficient dynamic memory management,...
2
by: DANIEL BEAULIEU J | last post by:
Basically i am a student taking an operating systems course which is c++ intensive. Familiar with Java, and so not so familiar with memory management. Looking for suggestions of exercises or web...
5
by: Tony Johansson | last post by:
Hello! Is it possible to mix C++ code for example DLL developed with Visual Studio 6.0 with C++ code developed with Windows Forms Application(.NET)? Is it possible to mix C++ code for example...
8
by: Chad | last post by:
hello, i am losing memory each time i make this call to a C++ dll (I make frequent calls). 'in VB.Net Declare Function grab Lib "grabber.dll" _ (ByRef lpBuf As Byte, ByVal lnum As Integer)...
0
by: erez_acount | last post by:
***************************************************************************** Call For Papers The 2006 International Symposium on Memory Management (ISMM'06) Co-located with PLDI 2006 ...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
5
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...
2
by: Tom Shelton | last post by:
On 2008-04-15, DR <softwareengineer98037@yahoo.comwrote: Where are you seeing that? In the task manager? If so, then you are looking in the wrong place. Let me tell you a little something...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.