By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

What scope have dynamically allocated memory

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tony Johansson wrote:
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.

Jul 23 '05 #2

P: n/a
Tony Johansson wrote:
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.
Jul 23 '05 #3

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message
news:42**********************@news.newshosting.com ...
Tony Johansson wrote:
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...


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.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.