473,486 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessment of dynamic variables.

Parul Bagadia
188 New Member
The space allocated for dynamic variables can be accessed only through pointers... this is a statemant i got in book of data structure..
Why is it so?
One more doubt is; whenever we are using scanf...
scanf("%format specifier",&variable); is it not a dynamic allocation?
If yes how?, if not how?
Apr 2 '08 #1
6 1583
weaknessforcats
9,208 Recognized Expert Moderator Expert
Dynamic allocations are identified by address. That means you will be using pointers whenever you use a dynamic allocation.


The scanf() functions require that you pass in the address of the variable where the data is to go. That means you either a) pass in the address ofg a local variable or b) dynamically allocate memory for the variable yourself and pass in the address of the allocation. scanf() istself does not do any allocating.

This applies to all of the scanf()-type functions.
Apr 2 '08 #2
Parul Bagadia
188 New Member
Dynamic allocations are identified by address. That means you will be using pointers whenever you use a dynamic allocation.


This applies to all of the scanf()-type functions.
Thanx for explaining me that; but i want to know that is it only for dynamic memeory allocations?
As in, in case of static memory allocations; we access it using its address ; and address is nothing but a pointer.

The scanf() functions require that you pass in the address of the variable where the data is to go. That means you either a) pass in the address ofg a local variable or b) dynamically allocate memory for the variable yourself and pass in the address of the allocation. scanf() istself does not do any allocating.

Here my point is; in your first case; i.e. when ur passing the address of local variable; thats also a pointer right?........ So ; if is it true that means scanf accesses dynamic locations?
I dont know whether m i right or not.
But address of anything is nothing but a pointer.
and second case is itself of dynamic allocation.
Apr 2 '08 #3
Laharl
849 Recognized Expert Contributor
Just because you have a pointer doesn't mean you've dynamically allocated memory. The only dynamically allocated memory is memory allocated using new or malloc() (or any other *alloc() function). Anything else appears on the stack. The stack is part of memory, so you can still get the address of such data. To get more than a local copy of any memory, you use pointers. The difference is in scoping and when the memory is no longer considered 'safe' to use.
Apr 2 '08 #4
Parul Bagadia
188 New Member
Just because you have a pointer doesn't mean you've dynamically allocated memory. The only dynamically allocated memory is memory allocated using new or malloc() (or any other *alloc() function). Anything else appears on the stack. The stack is part of memory, so you can still get the address of such data. To get more than a local copy of any memory, you use pointers. The difference is in scoping and when the memory is no longer considered 'safe' to use.
Thanx for that.
But i didnt actually get ur last point; when the memory is not considered safe to use? when is that ? And what can happen if we use such unsafe memory?
And one more thing; like you said static memory is stored on stacks.
So , where the dynamic memory is stored?
Apr 3 '08 #5
Laharl
849 Recognized Expert Contributor
The dynamic memory is on 'the heap'. 'The stack' and 'the heap' are just chunks of memory set aside for programs, nothing more. They're just set up differently. Every function has a certain amount of space on the stack for its local variables, and when one function exits, its stack space is deallocated and the next function call will use that memory. Thus, that memory is not guaranteed to hold its value.
Apr 3 '08 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
Stack memory is managed by the compiler while the heap allocations are managed by you. There may be limits an how much stack memoryis available.

It does not matter where your data is. All that matters is whether you or the compiler is in control of it.

When you access memory by address you need a variable to contain that address. That variable is a pointer. So a scanf() has a pointer as an argument for where the data goes.

When you call it with &var, the stack pointer for scanf() is created with &var as the initial contents. At this point stack and heap are irrelevant.
Apr 3 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2345
by: propizzy | last post by:
Appreciate any help!!! PROBLEM: I have this form that allows the user to dynamically create additional fields (see javascript code bellow). I am trying to retrieve the values entered into these...
2
8396
by: Tommy Lang | last post by:
Hi everybody! I am trying to learn the basics of C++ myself and have a hard time understanding some stuff like pointers and references etc. I have created a small program that adds two numbers...
1
2230
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy ...
4
13041
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
1
17624
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
12
6211
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE...
2
2923
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
2
6340
by: JWL | last post by:
Hi I need to create a bunch of sites with slightly dynamic CSS. Basically, all the image paths in the CSS need to be dynamic, depending on the values of certain ASP variables. I can think of...
3
1934
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.