Re: show disassembly



"Alex Blekhman" wrote:

You should get yourself a decent C++ textbook. Getting answers to
specific questions on Usenet is OK, but it cannot substitute a
proper learning.

Yes I am asking a lot of questions. And I feel somehow that I might abuse of
your patience (I recognize that you're rally patient cause I never had in any
forum such immedate and exhaustive answers). So thank you for the patience so
far.

"Fil" wrote:
I read that the memory of variables is freed once a function is
finished.
Is that the same for pointers?

C++ defines three types of storage: static, dynamic and automatic.
Each type of storage has different rules reagrding its lifetime
and allocation/deallocation.

Static storage type is for constants, global and static variables,
which are exist for the lifetime of a program.

Dynamic storage type obtained via `operator new' (and/or `malloc'
CRT function) and exists untill explicitly released with `operator
delete' (and/or `free' CRT function).

Automatic storage for local variables is allocated on stack by the
compiler and released automatically (hence the name "automatic")
upon exit from current scope block.

Pointer variable is not any different than any other variable and
will cease to exist if allocated on stack. However, the memory
that the pointer points to is the subject to its storage type
rules.

For example:

{
int a = 42; // regular automatic variable
int* p1 = &a; // pointer to some int
int* p2 = new int[10]; // another pointer
}

Here all variables are local and automatic. The memory they occupy
will be reclaimed once execution flow leaves the scope block.
However, the memory that `p2' points to will outlive the scope
block. You should take care of the value that `p2' holds in order
to free allocated memory later. Otherwise you'll leak it:

int* pOut = NULL;
{
// ...
int* p2 = new int[10];

pOut = p2;

} // p2 is destroyed, but pOut holds the address


// Free allocated memory elsewhere.
delete[] pOut;
pOut = NULL;

But I can understand than it is useful to free some memory if we
are in the middle of a function and there's a long way to go
before the end and we can run short of memory.

The whole point of dynamic storage is that you want it to outlive
current function and pass allocated memory to the caller.
Otherwise you could happily use local automatic variables for
storage.

Dynamic storage is my only way to create an array whose size I can't predict
when I write the program because it will depend on a variable. In your above
paragraph, are you saing that, since in my case I don't care about having a
scope longer than my function, I could use local automatic variables?

Does pret is still pointing to it before you run the next
assignement (do we find in pret the address of the first slot of
memory which it was pointing to even after delete before the
assignement to NULL - I will try that by myself)?

`pret' itself will not change after `operator delete'. But the
memory it pointed to will no longer be valid. Memory manager is
free to rearrange memory blocks as it sees fit. Accessing a memory
after freeing it leads to undefined behaviour. ("Undefined
behaviour" is C++ term that means that the outcome of the
operation is undefined. You may get away with it, but may crash
your system as likely.)

That's why it's a good habbit to zero unused pointer immediately
after deallocation in order to prevent accessing freed memory.

let say we also want to free the memory occupied by the
variables we're not going to use anymore. Can we undeclare them?

You cannot undeclare them, of course, but you can introduce
another scope block:

void foo()
{
int a = 5;

{
int b = 10;
} // variable b is destroyed here

} // variable a is destroyed here

At that point I also could think of functions. I am runing my
main, I used a function and I know I won't use it anymore. It
probably takes some space. Can I free it or is it automatically
done?

It's done automatically with the end of a program. Function's code
lives in static storage and cannot be explicitly deallocated.

HTH
Alex

Thanks a lot
.



Relevant Pages

  • Re: show disassembly
    ... C++ defines three types of storage: ... The memory they occupy ... int* pOut = NULL; ... // Free allocated memory elsewhere. ...
    (microsoft.public.vc.language)
  • Re: REGION=0M and LSQA
    ... At the time, memory was very expensive, and we ... remaining storage and always issued a return code zero. ... programs actually worked in production, ... all of the resources used by the job up until that point ...
    (bit.listserv.ibm-main)
  • Re: main memory vs. storage memory on 128 meg samsung 1 730 ppc phone
    ... This type is commonly used in WM devices (and storage cards). ... OS would copy itself to RAM taking out some of it. ... > How fast is the main memory compared to the flash ram memory in ... >> Fake storage card composed of flash is relatively slow, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Red-black trees?
    ... significant way on the amount of storage requested. ... assigned memory the first program released. ... You're mixing up two different issues, initializing space used by ... Some storage allocators initialize allocated storage, ...
    (comp.programming)
  • Re: Campagnolo components and the Tour...
    ... >That sounds like my first computer (Radio Shack Trash-80, ... >expansion interface allowing up to a whopping 48K of memory). ... >my storage was 100K on a 5.25" floppy. ... >Mark Hickey ...
    (rec.bicycles.tech)