Link to home
Start Free TrialLog in
Avatar of m a
m a

asked on

Dynamic and static variable

hi friends ,i am a beginner in c++ and i have some questions and i will be thankful if you help me to find its answers
1-a)a dynamic local variable has been declared in a function (method)
as below:
Work * p = new Work (p);

at the end of the function will this variable be released and erased (without making a call to its destroyer)?
b)if so: and if we call the destructor what will change?
------
2-And if we declare in its place a static variable:
Work p;

at the end of the function will this variable be released and erased?

-------------------------------------------------- -------------------------------------------------- ------------------
3-why do we prefer to work with a dynamic variable (and not a static variable) as a local variable?
ASKER CERTIFIED SOLUTION
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

>> Work * p = new Work (p);
 This statement is written incorrectly .
 See below link for code example on how to write it correctly.
http://www.cplusplus.com/reference/new/operator%20new/
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> why do we prefer to work with a dynamic variable (and not a static variable) as a local variable?
 
 If you do not know the dimensions of an array or matrix that you need to use as a local variable , then you can use a dynamic variable to allocate the right amount of memory . The dimensions might be passed then as arguments to assumption , or maybe computed within the function .
 If you do not know the dimensions of an array or matrix that you need to use as a local variable , then you can use a dynamic variable to allocate the right amount of memory . The dimensions might be passed then as arguments to assumption , or maybe computed within the function .
What do you think std::vector are for ?
I thought about mentioning vector, but chose not to. In the OP: "i am a beginner in c++" and the question is partly about benefits of dynamic and new. Based on the OP, I didn't want to introduce a new topic that might confuse the author.
@phoffric:
Honestly, vectors as easy to manipulate as C-style array, plus they provide many benefits:
- Manage memory for you, so you can focus on application logic rather than system managment.
- Auto expand.
- Thread safe.
- Embeded size.
- Ranged for loop enabled.
- Iterators enabled.
- STL algorythms enabled ect ect ...

Don't be scared by the template declaration syntax, vectors are variables like any other.
@fabrice,
I strongly recommend that you stick to the OP's question and not introduce non beginner topics such as iterators, threads, etc. !!
Avatar of m a

ASKER

@Fabrice Lambert @phoffric,
Thanks for your clarifications, :)
and in fact , i have an idea about vectors and all that ,but i had a little confusion about destructors  and now it's  gone of course ;) .
@phoffric:
I admit that iterators and threads arn't beginners concerns.

But, speaking about memory allocation without mentionning exceptions handling, memory leak risks and all the problems inherant to raw pointers (like validity, ownership ect ...) is giving an incomplete (thus wrong) answer.

C++ STL library offer more than enough tools to stay away from memory managment, better take advantage of it.
Memory managment should not be a beginner concern, they should focus on application logic.
Solutions provided