472,952 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

What scope have dynamically allocated memory

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
3 3563
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
7
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
4
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public...
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
4
by: dominic.connor | last post by:
Ok, pX = new Stuff N]; Is dynamically created. what about Stuff x ; ? To me saying it's statically allocated is wrong, because that goes to static variables. I can't call it stack...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.