Saturday, April 17, 2010

Difference Between New & malloc / Delete & free

One should be careful while combining the C and C++ code together. There are some mistakes that the developers do and it's really hard to find the point of mistake. One such mistake could be to use malloc and delete function in the C++ code. This a lot of difference in malloc and new as well as delete and free which is very important for a coder to know.

Some Difference:
1. Operator new automatically calculates the size of the object that it constructs. Conversely, with malloc(), the programmer has to specify explicitly the number of bytes that have to be allocated.

2. In addition, malloc() returns void *, which has to be explicitly cast to the desired type. This is both tedious and dangerous. Operator new returns a pointer to the desired type, so no explicit typecast is required.

3. When new has an object, space for the object is not only allocated but the object's constructor is called. And similarly when delete as an object, the object's destructor is called before the memory is released. If malloc and free are used, the destructor and constructor do not get called respectively and obviously, this simply won't do in C++ except in certain very rare situations where classes are present without any specific destructor/constructors.

new and delete automatically construct and destroy objects. malloc() and free(), on the other hand, merely allocate and deallocate raw memory from the heap.

delete can be overloaded, free cannot. delete invokes the destructor of the object to be deallocated, free does not do this.

The difference could be qouted as -

1. Operator new constructs an object (calls constructor of object), malloc does not.

2. Operator new is an operator, malloc is a function.

3. Operator new can be overloaded, malloc cannot be overloaded.

4. Operator new throws an exception if there is not enough memory, malloc returns a NULL.

5. Operator new[] requires to specify the number of objects to allocate, malloc requires to specify the total number of bytes to allocate.

6. malloc() returns void *, which has to be explicitly cast to the desired type but new returns the proper type.

7. Operator new/new[] must be matched with operator delete/delete[] to deallocate memory, malloc() must be matched with free() to deallocate memory.

8. The new/delete couple does not have a realloc alternative that is available when malloc/free pair is used. realloc is used to resize the length of an array or a memory block dynamically.


Reference:

Friday, April 16, 2010

XHTML

What is XHTML?

XHTML is a combination of HTML and XML (EXtensible Markup Language).

XHTML consists of all the elements in HTML 4.01, combined with the strict syntax of XML.

XML is designed to describe data, and HTML is designed to display data.

Difference between HTML & XHTML

The Most Important Differences:

  • XHTML elements must be properly nested
  • XHTML elements must always be closed
  • XHTML elements must be in lowercase
  • XHTML documents must have one root element

Wednesday, April 7, 2010

GDB Tutorial


How to run the gdb ?
> gdb executable_name
(then u will see (gdb) )

How to set break points ?
(gdb)b file_name: line_number
or
(gdb)break file_name:line_number
( You can either use break or b)

Executing next line in the program
(gdb)next
or
(gdb)n
How to run the executable?
(gdb)run

How to display value of particular variable?
(gdb) p variable_name
or
(gdb)print variable_name
or
(gdb)display variable_name
or
(gdb)d variable_name

How to Quit Gdb?
(gdb)quit
or
(gdb)q

Reference :