What scope have dynamically allocated memory

T

Tony Johansson

Hello Experts!!

When you instansiate varaibles(object) you can do so in four different scops
which are.
Within a block which is called Local or block scope .
Within a function which is called function scope.
Whithin a class which is called class scope.
Outside a function which is called global or file scope.

Now to my question when you allocate memory dynamically you can't say
anything about the scope because as long as you have the pointer you can
access the allocated memory anytime. I'm I right?

many thanks!
//Tony
 
M

Mark P

Tony said:
Hello Experts!!

When you instansiate varaibles(object) you can do so in four different scops
which are.
Within a block which is called Local or block scope .
Within a function which is called function scope.
Whithin a class which is called class scope.
Outside a function which is called global or file scope.

Now to my question when you allocate memory dynamically you can't say
anything about the scope because as long as you have the pointer you can
access the allocated memory anytime. I'm I right?

many thanks!
//Tony

No, scope does not define from where you may access memory. All of your
examples refer to the scope of a _name_, that is, where in the program
it's legal to refer to some named entity. If you assign a name to some
dynamically allocated memory, then the usual scope rules apply to the
name, irrespective of what happens to the memory. The name may go out
of scope while the memory persists (a potential source of memory leaks)
or the name may remain in scope after the memory has been freed (a
potential source of undefined behavior).

On a related note, there's nothing special about accessing dynamically
allocated memory. You can use the address of (&) operator to take the
address of stack objects too, but you do need to be mindful of the
temporary nature of such objects if you plan to do anything useful with
the contents of that memory.
 
R

Ron Natalie

Tony said:
Hello Experts!!

When you instansiate varaibles(object) you can do so in four different scops
which are.
Within a block which is called Local or block scope .
Within a function which is called function scope.
Whithin a class which is called class scope.
Outside a function which is called global or file scope.

Now to my question when you allocate memory dynamically you can't say
anything about the scope because as long as you have the pointer you can
access the allocated memory anytime. I'm I right?

You're confusing two concepts: scope and storage duration.
Scope applies to variable names.
Storage duration applies to object lifetime.

While these are roughly coincident for local (function, block) variables
of automatic storage duration, it falls apart quickly from there...

You can't have "dynamic" scope. The dynamic memory doesn't have a name.
It's lifetime exists from the time it is allcoated until it is deleted.
 
L

Larry Brasfield

Ron Natalie said:
You're confusing two concepts: scope and storage duration.
Scope applies to variable names.
Storage duration applies to object lifetime.

While these are roughly coincident for local (function, block) variables
of automatic storage duration, it falls apart quickly from there...

I agree with your answer insofar as it relates to C++.
You can't have "dynamic" scope. The dynamic memory doesn't have a name.
It's lifetime exists from the time it is allcoated until it is deleted.

True for C++. There is a least one language, Perl,
which does have something properly called "dynamic
scope". It is a source of much confusion and blessedly
made unnecessary by a later introduced static scoping
mechanism, so one could argue it should not exist. But
it does. A name in a block can refer to differenc objects
depending on what scopes were visible when the block
was entered.

I mention this not only to honor the grand Usenet
nitpicking tradition, but for the amusement of those
who can appreciate the strange facts behind the
"never say never" injunction. Also, to help all you
C++ fans appreciate static scoping some more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top