Posts Tagged ‘java’

A major revision for my publication about Object-Oriented Memory Management

Friday, April 10th, 2009

It took about a year for a major revision of my document about memory management in object oriented programming languages.

This major revision adds C++ in addition to Java.

The paper is about a model for memory management during the execution of programs written in Java and C++.

The number of pages grew from 17 to 28.

You can see more information and download the pdf on this page.

I hope you will find it useful to model your programs and know how memory is handled.

  • Share/Save/Bookmark

Heap vs. Stack in C++

Friday, March 20th, 2009

After the study of pointers versus references, the second natural question that comes in head of a ex Java developer turning to C++ is:

"What are the difference between static and dynamic memory allocation in C++?"

which can be translated as:

"When should I use the stack and when do I have to use the heap in C++?"

that can be further simplified to:

"When should I use the new operator in C++?"

I could simply summarize the answer to: "Use stack when possible", but I think that this time there is the need of more explanations. Let's have the following model for a process in the system:

A simplified model for a process

A simplified model for a process

I'm not really interested in an real representation of a process (see Modern Operating Systems by A. S. Tanenbaum for a very good explanation on processes), but focus on the stack and on the heap.
In reality the heap is a software abstraction but you can also imagine it like the stack.
In C++ programs there are also several other memory areas in which objects and non-object values may be stored (see this article on GotW for further details).

Why then choose between stack or heap? Quoting my publication on object-oriented memory management in Java:

Stack-based variables have their extent determined by their scope, so the former is constrained by the structure of the code at compile-time .

Sometimes there is a need for the variables with unconstrained extent in order to cope with
problems where lifetime of a variable can only be known at run-time.
In this case heap-based variables, whose extent is strictly under control of the programmer, are used. [..]

I promise that I will update my 17 pages about OO-memory management to cover also C++ by the end of June. By the way, following some forums, wikipedia, my publication and GotW, I also summarized pros and cons of stack and heap use in C++:

Stack Heap
Its size is determined at compile-time Size determined at run-time
Therefore, it is less expensive and quick Therefore, it is more expensive and slower than stack
The preferred way to store objects and variables if their size is limited. To be used only if needed: the amount of memory needed is variable and unknown, and may increase rapidly.
There is an AUTOMATIC CLEANUP of objects when they go out of their scope Objects STAY IN MEMORY even when you don't use them anymore.
Programmers don't have to bother to free resources Therefore, programmers HAVE TO CLEAN memory manually. However, all modern OS free the resources when the program exits.

Update 2009-07-03
After 4 months of heavy GUI and Database C++ programming, here are my thoughts: if you are planning to write a program with something more that a couple of objects interacting, using associations and therefore, objects as attributes, use the heap. Every serious program, even if not really big, uses heap for object allocation. Just take care to delete the objects when you don't need them anymore. Objects on the heap are dynamically allocated and it is more comfortable to pass them through other objects using pointers. The use of the heap assures the live of objects even if the method that generated them runs out of scope ( =dies ). If you also plan to write GUIs, solid toolkit like QT recommend the use of heap to create graphical objects.

In some Operating Systems, stack is also very limited while heap is usually not.

Here are listed the sources I used for writing this article:

  • http://www.velocityreviews.com/forums/t278261-stack-vs-heap.html
  • http://www.computing.net/answers/programming/stackheap-c/2293.html
  • http://www.codeguru.com/forum/showthread.php?p=1186307#post1186307
  • http://www.codeguru.com/forum/showthread.php?t=350945
  • http://www.gotw.ca/gotw/009.htm
  • http://en.wikipedia.org/wiki/Process_(computing)#Representation
  • http://bd-things.net/object-oriented-memory-management/

Hope that this article helped you to clearly understand the differences between stack and heap allocation in C++, write me if there are other issues or you need more explanations!

  • Share/Save/Bookmark

Reference vs. Pointer

Wednesday, March 18th, 2009

In Software Engineering Project course we need to learn some C++ to develop the project. A question that some programmers have when passing from Java to C++ (like me) is "Which are the differences between pointers and references?". You can find a lot of answers around developers forum, but I summarized them in the following table. Glad if you found it useful!

Reference Pointer
is an object which IS AN ALIAS for another object is an object that CONTAINS THE ADRRESS IN MEMORY of another object
the preferred way of undirectly access objects you should use it just if you really need it, as it lets you to work in a lower level than a reference does
keeps your code clear the code is less clear but still understandable
it must be initialized when created you don't have to initialize it when declared
it references to the one object and only that one, therefore you can not modify the address referenced because it contains an address, it can point to many different objects during lifetime. The address can be manipulated
when used, the address is dereferenced without using any particular operator the address must be dereferenced using the * operator
  • Share/Save/Bookmark

Introduction to HTTP mind-map

Sunday, March 1st, 2009

As I promised about 4 hours ago, here is my introduction to Hyper Text Transfer Protocol in form of a mind map.
It is to be intended as a really short introduction to this protocol. Like the previous one about computer networks, the mindmap summarizes materials copyrighted by Tanenbaum and also material taken from Wikipedia.

The topics covered are:

  • Scope of the protocol
  • HTTP connection
    • HTTP/1.0
    • HTTP/1.1
  • HTTP request methods:
    • GET
    • HEAD
    • PUT
    • POST
    • DELETE
    • TRACE
    • CONNECT
    • OPTIONS
  • Message Headers
    • Request Headers - all
    • Response Headers - all
  • Status Codes:
    • 1xx Information
    • 2xx Success
    • 3xx Redirection
    • 4xx Client error
    • 5xx Server Error
  • Sessions:
    • Cookies
    • Server-Side sessions
  • Secure HTTP - HTTPS:
    • By URI scheme
    • HTTP Upgrade Header
    • SSL/TLS

You can browse an HTML version online.

You can download:

As always, you are free and encouraged to contact me in case of errors or anything else.
Hope you like it!

  • Share/Save/Bookmark

How to limit a field with javascript-prototype and display the counter

Wednesday, December 3rd, 2008

For job purposes I had to learn a lot of JavaScript, including AJAX related stuffs. Like every lazy (but intelligent) developer, I looked around for frameworks that could help me doing my work. I knew that prototype existed, but I never looked at it because of lack of interest. Well it's great, it's really great! Nowadays everybody should use frameworks, as their abstraction permits a rapid development without worrying about things like platform compatibility, in our case browsers. Prototype also has a very nice and clean syntax that overtakes functions not compatible with every browser.

In this post I'm going to report a very nice function I implemented for limiting Form input fields, like textarea and input of type text. You have to call it via onkeyup and onkeydown events. The function accepts 3 parameters: field, limit and counterDesired.
The first is the field object, you should use the keyword this for a value.
The second parameter is a limit value, the number of characters that the field should contain at maximum.
The third parameter is optional, false as default. It permits to add a visual counter after the field, like the one you see on YouTube, for example. You can either tell to the function to add the counter for you (put inside a <span> block) or to put it inside another block you've already defined. In the second case, the block must have an id of the form 'fieldID_counter'

This is the compact version, see below for some examples and the expanded, explained version:

<script type="text/javascript">
// < ![CDATA[
function limit_text(field, limit, counterDesired) {
    if (counterDesired == null)
        counterDesired = false;
    var length = $F(field).length;
    if (length > limit)
        $(field).value = $(field).value.substring(0, limit);
    if (counterDesired) {
        if ($($(field).id + '_counter')) {
            $($(field).id + '_counter').update($F(field).length + " / " + limit);
        } else {
            var counterText = new Element('span', {'id': $(field).id + '_counter'});
            counterText.update($F(field).length + " / " + limit);
            $(field).insert({'after': counterText});
        }
    }
}
// ]]>
</script>
 

(more...)

  • Share/Save/Bookmark

How to install MintMenu on Debian (lenny, sid)

Sunday, November 23rd, 2008

I really like (and miss) Linux Mint Menu (mintmenu), so I installed it on my Debian Sid box and here is how I managed it:

  • Install mintsystem and mintmenu deb packages, either by adding mint repository to yout sources.list file or by downloading them from mint packages or simplier, by grabbing them from my blog
  • Help it to recognize your applications by symlinking some files in /etc/xdg/menus (as root):

    ln -s gnome-applications.menu applications.menu
    ln -s gnome-preferences.menu preferences.menu
    ln -s gnome-settings.menu settings.menu

  • Add mintMenu to your Gnome panel..

et voilà! Here are the two deb files I'm using:
mintsystem_61_all
mintmenu_4.2_all

Here is an updated screenshot:
MintMenu complete under Debian Sid

MintMenu complete under Debian Sid

If you also want to enable beagle or tracker, be sure to add the correct search command in the preferences of mintMenu (see the screenshot)

  • Share/Save/Bookmark