Archive

Posts Tagged ‘php’

Heap vs. Stack in C++

March 20th, 2009 bodom_lx 4 comments

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 than 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 toolkits 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://task3.cc/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!

Related posts

BD-theme released!

December 8th, 2008 bodom_lx No comments

I’m very pleased to announce the final version of BD-theme! For those who don’t know, BD-theme is a widget ready, two columns Wordpress theme. It is written using the default Wordpress theme as php/XHTML base.
The theme is mostly inspired by TorrentFreak rounded layout.
BD-theme exists just because I wanted to write my own Wordpress theme.
You should probably see this theme in action on my blog, or on the testing site .
Anyway, here is a screenshot:

BD-theme 1.0

BD-theme 1.0


BD-theme is valid XHTML 1.0 Strict as well as valid CSS 2.1 (check the testing site against W3C validators)
It has been checked with all the most popular browsers: Internet Explorer 6, Internet Explorer 7, Firefox 2.x and Firefox 3.0.x, Google Chrome, Opera 9.62.
The theme is free software and released under the Creative Commons Attribution-Noncommercial 3.0 License. You are free to use and/or modify the theme under the conditions of the license.
You can obtain the theme on its project page:
http://task3.cc/projects/bd-theme/

Related posts

Announcing incollo.com service!

September 30th, 2008 bodom_lx No comments

When I announced BD-incollo 6 days ago, I also mentioned that I would have launched the service today. I really did that, and I’m very proud to announce the first site that runs BD-incollo.
http://incollo.com! Very easy :-)
Incollo.com is a collaborative debugging tool like Pastebin or other similar services, but it’s slightly different from it. And it’s different from other Pastebin clones even written using Rails or Django.
Here are the most exciting features:

  • It’s Fast. Very Fast
  • Written thinking about usability
  • A very clean interface, a minimalist design that gives space to the code (as it should always be)
  • It’s possible to search through pastes, like in a forum
  • A Paste is not deleted after 30 days or something similar. A paste is deleted after it is no more interesting! It’s deleted after 60 days of no visualizations
  • The system is anonymous. It won’t store your information! Paste whatever you want but please use your brain! A Paste may be reported to the administrator!
  • You don’t really have to play with options and there are no required field other than the Paste itself. You may paste a text and directly hit the submit button
  • Quite every page is XHTML 1.0 compatible
  • It works well and has nice urls, thanks to Django
  • Compatible with every browser (tested with Internet Explorer 6,7,8, Mozilla Firefox 3, Apple Safari, Google Chrome)
  • Resolution friendly! Liquid design that adapts to every monitor resolution (tests from 1024×768)
  • Developer friendly! Every functionality of incollo.com can be used with max 2 mouse clicks and without a mouse scroll!
  • Tested with lots of pastes, quite every source code should be perfectly viewed (this does not happen with every pastebin clones I’ve tried)
  • Uses Pygments for code highlighting
  • Languages supported: ActionScript, Assembly (various), Boo, Befunge, BrainFuck, C, C++, C#, Common Lisp, D, Delphi, Dylan, Erlang, Haskell (incl. Literate Haskell), Java, JavaScript, Lua, MiniD, MooCode, MuPad, OCaml, PHP, Perl, Python (incl. console sessions and tracebacks), Redcode, Ruby (incl. irb sessions), Scheme, Visual Basic.NET, Django/Jinja templates, ERB (Ruby templating), Genshi (the Trac template language), Myghty (the HTML::Mason based framework), Mako (the Myghty successor), Smarty templates (PHP templating), JSP (Java Server Pages), , Other markup, , Apache config files, Bash shell scripts, BBCode, CSS, Debian control files, Diff files, Gettext catalogs, Groff markup, HTML, INI-style config files, IRC logs (irssi style), Makefiles, MoinMoin/Trac Wiki markup, Redcode, ReST, SQL, also MySQL, Squid configuration, TeX, Vim Script, Windows batch files, XML

This is an example of Paste with Incollo.com:
http://incollo.com/7dca5011

You are really welcome to report any bugs or leave a feedback! Remember that this is my very first Django project, and I created it in about 6 days!

Of course, I’m already beginning to think about new features :D

Related posts

BD-incollo

September 24th, 2008 bodom_lx No comments

BD-incollo is a collaborative debugging tool like pastebin but written using Django.
Why did I write another pastebin clone?
Pastebin is (sometimes) slow, and I’m not (yet) interested to categorize pastes or to associate them to a user. Pastebin is free and the code is available, but is a single php file, and not a Django project!
There are already other services there, even written using Django, but no one is free software. BD-incollo IS free software. Well, BD-incollo has been written after those Django pastebin clones, so i could learn from their mistakes and write a better one! Everybody is welcome to grab the code and modify it, or even to submit suggestions!
Report feedbacks, feature requests and bugs on the issue tracker.

Quick Jump:

Features

Download

License

Features

  • Copy, Paste and store a text / source code to the system
  • NEW! Give other people the possibility to discover your Paste (make a Paste either public or private)
  • Decide to colorize the syntax of the Paste
  • Share it using its URL
  • NEW! Enhance Pastes! Create a Paste starting from an old one
  • NEW! View differences! Makes use of the powerful diff-match-patch by Neil Fraser to see differences between two Pastes
  • NEW! Antispam protection using Akismet and akismet.py by Michael Foord
  • Download it as plain text
  • View it as plain text
  • Search something interesting through other pastes!
  • Report abuses to site admins

Interesting Stuffs

  • Fast, really fast!
  • It works well and has nice urls, thanks to Django
  • A very clean interface, a minimalist design that gives space to the code (as it should always be)
  • Compatible with every browser (tested with Internet Explorer 6,7,8, Mozilla Firefox 3, Apple Safari, Google Chrome)
  • Resolution friendly! Liquid design that adapts to every monitor resolution (tests from 1024×768)
  • Developer friendly! Every functionality of BD-incollo can be done with max 2 mouse clicks and without a mouse scroll!
  • Tested with lots of pastes, quite every source code should be perfectly viewed (this does not happen with every pastebin clones I’ve tried)
  • Uses Pygments for code highlighting

Download

License

BD-incollo is released under The Gnu Affero GPL version 3! This is different from the license of the contents of the blog

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see < http ://www.gnu.org/licenses/ >.

Related posts

Installare OpenGEU su Macbook

September 21st, 2008 bodom_lx No comments

Ho appena pubblicato sul Wiki italiano di OpenGeu una guida su come installare OpenGEU su un Macbook.
Estratto dall’introduzione:

Questa guida vi aiuterà ad installare OpenGEU e qualsiasi altra distribuzione basata su Ubuntu sul vostro Macbook / Macbook Pro. La stessa Ubuntu è installabile seguendo questa guida.
L’how-to è mirato a come partizionare l’hard disk ed avviare correttamente il processo di boot dopo l’installazione. Per un’ottimale configurazione post-installazione è preferibile visionare il wiki di Ubuntu.

Focalizza sui problemi dell’installazione di Grub sul Macbook, in sostanza.
La potete leggere su
http://opengeuwiki-it.intilinux.com/index.php?title=Macbook

Related posts

How to install OpenGEU on Macbook

September 16th, 2008 bodom_lx 1 comment

Introduction

This guide will help you to install OpenGEU and every other Ubuntu based linux distribution on your Macbook (either “normal” or pro). Even Ubuntu will work with this how-to.
The tutorial is aimed on how to succesfully partition the hard disk and to correctly boot the distribution. For a better post-install configuration I suggest you to follow the Ubuntu Wiki.

This tutorial is also posted on the OpenGEU Wiki
Read more…

Related posts