Tag: Programming (page 2 of 2)

HTML And CSS: Padding HTML Element Without Resizing

For sometime now Ive been making my elements actual size subtracted by a certain percentage to compensate for the resizing triggered by padding and add a border that is the same color as the element to emulate a margin. This is really an ugly approach since depending on the device I might have over/under compensate the percentage but I found a css code that will make the padding grow inside the element size instead of growing its actual size. Below is the css code:

As you can see we have 3 different codes for different browser. With this when you use padding your html element will not auto re-size to accommodate the padding.

South Database Migration Tool

Update: South migration tool is now pulled into Django core as of Django 1.7. So if you are starting out on Django 1.7 then you don’t need to install South anymore because it has now it’s own implementation of a database migration tool.

For sometime now Ive been playing with Django. Making small applications that is not worth mentioning. When I first touched Django Ive read somewhere that it would help me greatly if I used South, a database migration tool but I just brushed it off too early at that time since I didnt want to learn something on top of Django. Now that Ive tried it Ive realized that Ive truly made a big mistake of not trying it earlier.

South is a tool for database migration. What it does is help you sync your database tables with your models. Youre really going to appreciate it when you have a live production setup and then you find yourself needing to change some tables, like add columns or change the default values. Imagine if you have made a lot of changes and when you need to put your project into the live setup youre going to write a bunch of SQL scripts to patch the live setup. With South you wont need to create your own SQL scripts to patch your tables.

Like all Django application you will need to add it in your settings.py. It also creates it own tables in your database. For now I dont really care what it adds to my project since it doesnt seem to get in my way. It is really a great app/tool to have specially for me since Im only learning Django and web development so I always ends up modifying my models too often.

You can get South from here. Installation and a tutorial is also in their website to help you get started quickly.

Difference of C++ operator delete and delete[]

The difference between “delete” and “delete []” is the former is used for a pointer only while the latter is used for pointers to an array. Take for example the code below:

if you use “delete” on a pointer to an array it will cause a memory leak, so make sure you use the correct “delete”.

5 Simple Tips In C++

Always Initialize Variables
Since C++ doesnt give variables a default value, you should initialize it to avoid problems. That is, instead of just saying:

You should instead:

Always Use Smart Pointers
Smart pointers were made to make your life easier, so why not use it? You can use boost or the one from TR1. Also smart pointers are now included in C++11.

Dont use “using namespace” in a global scope
Frankly speaking I dont really use “using namespace” even on a local scope. Namespace was made to avoid name conflicts so why take them out of their namespace and transfer them into the global space? If you really are feeling lazy to type lengthy namespaces like the one from Boost you should just use an alias:

Use “const” Modifier
“const” will convey to the user that you dont have an intention to alter any data member or function arguments. This can also prevent some of your teammates to create codes that can affect data inside it in the future. Think of it like a contract that any parameters with const will not be changed.

Use assert() For Debugging
assert() is a handy macro for debugging. It is also easy to remove once your building for release, just define “NDEBUG”. Some do prefer unit testing but if youre maintaining a project that was made earlier by another person that doesnt have a unit test ready . Then adding asserts for the new added codes is not as difficult as making unit tests for it.20

Make Your Django Project Movable

For every Django projects we have a settings.py that contains all the settings for a project but some of these must have an absolute path. So moving a Django project to another directory can be a pain. Luckily we have the os module. These module can and will help us make our projects easy to move.

Just add these line in your settings.py

Now for every settings that needs an absolute path just prepend the path with APP_PATH

C++ Boost Smart Pointers

C++ Boost Smart Pointers, these are template classes that implements a garbage collection system in C++, isnt that pretty neat? Now this means that you dont need to bother remembering to free all those pointers that you created because these templates will be the one making sure they are freed. You can download Boost library from their website download page.

These are the different templates for boost smart pointers:

  • scoped_ptr
    The same with std::auto_ptr that is noncopyable
  • scoped_array
    The same with std::auto_ptr that is noncopyable but for arrays, pointer created using new[]
  • shared_ptr
    Pointers can be shared with this.
  • shared_array
    Same as shared_ptr but for arrays
  • intrusive_ptr
    these templates will not release the pointers but will call functions when its constructor and destructor will be called

scoped_ptr and scoped_array
As what their name suggests these templates are used for encapsulating pointers that will only live for the current scope. This is also much like std::auto_ptr in which they are also not copyable. Example usage

shared_ptr and shared_array
These templates has a reference counter for the pointer they hold. Every copy of this will add to the reference counter and every call on the shared_ptr destructor will subtract on the reference counter. If this reference counter reach 0 the pointer will be automatically freed. Example usage

weak_ptr
weak_ptr these templates will point to the pointer pointed by shared_ptr but doesnt own it. These means that what it is pointing might be already freed by shared_ptr. Example usage:

Note: make sure you first call expired() to check if the pointer is still valid. Then call lock() this will return a shared_ptr, so that it will not be freed while you are accessing it.

intrusive_ptr
The application must have a function called intrusive_ptr_add_ref and intrusive_ptr_release with template arguments. These functions will be called by intrusive_ptr on its creation and destruction respectively. Since intrusive_ptr doenst release the pointer, intrusive_ptr_release shall be tasked of releasing the pointer once the counter reaches 0.

Example usage:


With these 5 template classes for memory management you can now be a pointer maniac creating pointer every where without worrie
Newer posts

© 2019 James Baltar

Theme by Anders NorenUp ↑