Month: October 2012

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:

int main( int argc, char* argv[] ) {
 //declare a pointer to an array of char named as a
 char *a = new char[64];
 //declare a pointer to a char named as b
 char *b = new char;
 
 //since a is a pointer to an array of char the use of delete[] is a must
 delete [] a;
 //for b which is only a pointer to a char use delete
 delete b;
 
 return 0;
}

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

Samba Client in Ubuntu Command Line

Samba Installation

Check if Samba is installed

apt-cache policy samba

You should see something like this if it is installed:

 Samba:
 Installed: 2:3.4.7~dfsg-1ubuntu3.7
 Candidate: 2:3.4.7~dfsg-1ubuntu3.10

otherwise “Installed” on the top has a blank value. To install type in:

sudo apt-get install samba

After successfully installing Samba you can now connect to a shared folder on the network.

Connecting

Suppose you want to connect to “192.168.0.114” shared folder named “Public” . You just need to type:

smbclient //192.168.0.114/Public -user [Your_User_Name]

“-user [Your_User_Name]” argument is optional. If no “-user” indicated you will be defaulted to the default user. After pressing enter with the above command, the terminal is going to ask for your password. After that the terminal should look like this:

smb: \>

You are now successfully log-in in the server computer and ready to write to or copy from it.

Writing Files

To write a file in the server using Samba client use the command:

puts [local name]
  • [local name] is the filename of the file you want to transfer on the current directory before you connect in samba client. This can also be a filepath relative to the current directory or an absolute path.
  • is optional. If no is specified the filename of [local name] will be used.

Suppose we have a file named “test.txt”. We can transfer it by:

smb: \>puts test.txt

If we want it to be named “test2.txt” on the server. Type in:

smb: \>puts test.txt test2.txt

Make sure you have write access to the folder or else youll get an error like this “NT_STATUS_ACCESS_DENIED opening remote file ”

Copying Files

You can copy files from the server with Samba client using the command:

get  [local name]

and [local name] are the same as the the “put” command above.

To exit the Samba client just type “exit”.

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:

int a;
double b;

You should instead:

int a = 0;
double b = 0.0

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:

namespace foo3 = foo1::foo2::foo3;

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

import os
APP_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + "/"

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

#for static root
STATIC_ROOT = APP_PATH+static/
#For templates
TEMPLATE_DIRS = (
 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
 # Always use forward slashes, even on Windows.
 # Dont forget to use absolute paths, not relative paths.
 APP_PATH+template,
)

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

void foo( void ) {
 boost::scoped_ptr< int > i1(new int);
 *i1 = 5;
 if ( *i1 == 5 ) {
 boost::scoped_ptr< int > i2( new int );
 *i2 = 5;
 //i2 will only live up to here.
 //scoped pointer will automatically release the memory for i2
 }
 //i1 will live up to here
 //scoped pointer will automatically release the memory for i1
}

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

#include <iostream>
#include <boost/shared_ptr.hpp>
 
class foo {
 public:
 foo() {}
 
 boost::shared_ptr< int > num;
};
 
int main( int argc, char* argv[] ) {
 foo f;
 
 if ( argc > 1 ) {
 boost::shared_ptr< int > num( new int );
 std::cout << "current count: " << num.use_count() << std::endl;
 *num = 4;
 f.num = num; //num reference count will increase to 2
 std::cout << "current count: " << num.use_count() << std::endl;
 }
 else {
 std::cout << "Expected 1 argument";
 }
 std::cout << "current count: " << f.num.use_count() << std::endl; //reference count will be 1
 return 0;
}

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:

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
 
class foo {
 public:
 foo() num( new int ) {}
 
 boost::weak_ptr get_num( void ) const {
 return num;
 }
 private:
 boost::shared_ptr< int > num;
}
 
int main( int argc, char* argv[] ) {
 boost::weak_ptr< int > weak;
 {
 foo f;
 weak = f.get_num();
 if ( !weak.expired() ) {
 std::cout << *weak.lock() << std::endl;
 }
 //since f will be destroyed here and so does its member num
 //weak will reset and its expired() method will return true
 }
 std::cout << weak.expired() << std::endl;
 return 0;
}

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:

#include <iostream>
#include <boost/intrusive_ptr.hpp>
 
template <class T>
void intrusive_ptr_add_ref( T *t ) {
 t->addCount();
}
template <class T>
void intrusive_ptr_release( T *t ) {
 t->subCount();
 if ( t->count() == 0 ) {
 delete t;
 }
}
 
class foo {
 long refCount;
 public:
 foo() : refCount(0) {}
 
 void addCount(void) { refCount++; }
 void subCount(void) { refCount--; }
 long count(void) const { return refCount; }
};
 
int main( int argc, char* argv[] ){
 boost::intrusive_ptr<foo> a(new foo); //intrusive_ptr_add_ref will be called here
 return 0; //intrusive_ptr_release will be called here, that will cause to release foo
}

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

First Blog Post

Who am I?

Im James Baltar a professional programmer. You could say my main programming language is C++. I just woke up one day thinking I want to have my own website with a blog, portfolio and some “how to” in programming. So here it is, my website. Currently it only has blog but Ill include a portfolio soon.

What to expect from this website?

Well what Im currently planning is to post some “How to” in programming, Linux and maybe some Windows. Sometimes Ill also blog my opinions on popular issues. For the future Ill include a portfolio that will showcase algorithms that is widely used in game development and if time allows I will also create a tutorials mainly on C++ and its popular APIs and frameworks.
To summarize it this is my priority list:

  1. Blog
    • Post “How to”
    • Post Opinion
    • Post other topics
  2. Create a portfolio
  3. Create tutorials

© 2024 James Baltar

Theme by Anders NorenUp ↑