473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Memory Allocation

Hello,

I may be remembering incorrectly, but I'm sure that in C++ you could declare two variables of the same name where the second declaration is within it's own code block. E.g.

Expand|Select|Wrap|Line Numbers
  1. void myfunction()
  2. {
  3.   int x = 5;
  4.  
  5.   //some code
  6.  
  7.   {
  8.     int x = 3;
  9.  
  10.     //print x
  11.   }
  12.  
  13.   //print x
  14. }
  15.  
In this case, the first time x is printed, it will refer to the x declared within the inner block, and the second time it will refer to the x in the outer block.

If this is correct, my question refers to the compiler and stack allocation.

I found out the other day that Java allocates memory for all local variables within a function block, regardless of whether the code actually reaches those declarations. Apparently, this is to make the compiler/runtime easier to code and manage. I'm assuming this is the reason you can not have a situation similar to the above code example in Java, as you would have two local variables by the same reference being allocated to the stack at the same time.

I wondered if this was the way all languages handled memory allocation. If this is the case in C++, my question is how does C++ allow two variables to be allocated to the same named reference within a function block? Does C++ allocate to the stack each time it enters any kind of code block? Or do pseudo/mask references get used, whereby the first x is internally considered as x1 and the second as x2?

Or something...

I have not a lot of knowledge on this area of coding, but it's something I've recently started wondering about for the purposes of memory optimization in my coding.

Regards,
Rob.
Feb 12 '08 #1
5 1552
weaknessforcats
9,208 Expert Mod 8TB
In C++, the operative word is stack.

That means as variables are encountered they are added to the stack. Only in C are all of the variables created on the stack at the beginning ofr the function.


C has a very simple stack frame where these variables are placed. The C++ version allows you do add to the stack frame as the function progresses. Exactly how this is done is left to the compiler writer. It could be your C++ compiler works just like your Java example but it is not required to do so.
Feb 12 '08 #2
Ahh, cool.

I had always done my coding on the understanding of memory being allocated to the stack as the declarations were encountered, so you can imagine my surprise when I found out about Java.

Interesting that C does it the same way as well, that's a useful tip.

Would it be fair to say that the majority of compilers do it as we have said for C++, but that it is dependant upon the compiler implementation? I.e. some compilers may do it the same as C and Java, but most don't.

Regards,
Rob.
Feb 13 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
I have no idea what the common stack implementation is.

As a programmer, I just follow the scoping rules and let the compiler generate the proper code.

Also, I don't believe Java has a problem with two variables of the same name so long as they are in different scopes.
Feb 13 '08 #4
Fair enough.

With regard to Java, it definitely does (or at least in 1.6) have a problem with duplicate variable names within the same function block, even if in separate scope. The below is an example of the compiler message you get:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable results


Thanks for the info.

Rob.
Feb 14 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
If that's reaaly true, then that is one big hole in Java. Big enough to prevent me from ever considering it for a real application.
Feb 14 '08 #6

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

Similar topics

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...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.