learning C++...

Monday, April 19, 2010

new operator in C++

Allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.
[::] new [placement] new-type-name [new-initializer]
[::] new [placement] ( type-name ) [new-initializer]
If unsuccessful, new returns zero or throws an exception.
When new is used to allocate memory for a C++ class object, the object's constructor is called after the memory is allocated.
Use the delete operator to deallocate the memory allocated with the new operator.

The following example allocates and then frees a two-dimensional array of characters of size dim by 10. When allocating a multidimensional array, all dimensions except the first must be constant expressions that evaluate to positive values; the leftmost array dimension can be any expression that evaluates to a positive value. When allocating an array using the new operator, the first dimension can be zero — the new operator returns a unique pointer.
char (*pchar)[10] = new char[dim][10];
delete [] pchar;

The type-name cannot contain const, volatile, class declarations, or enumeration declarations. The new operator does not allocate reference types. The new operator cannot be used to allocate a function, but it can be used to allocate pointers to functions.

Dynamic memory allocation

Last month, I presented in a job interview. One question posted to me about the dynamic memory allocation, I have totally no idea. The question is if you don't use "malloc", what-else can help you get the address, below was the differences about functions in dynamic memory allocation.




















FunctionTask
mallocAllocates memory requests size of bytes and returns a pointer to the Ist byte of allocated space
callocAllocates space for an array of elements initializes them to zero and returns a pointer to the memory
freeFrees previously allocated space
reallocModifies the size of previously allocated space.

Memory allocations process:

According to the conceptual view the program instructions and global and static variable in a permanent storage area and local area variables are stored in stacks. The memory space that is located between these two regions in available for dynamic allocation during the execution of the program. The free memory region is called the heap. The size of heap keeps changing when program is executed due to creation and death of variables that are local for functions and blocks. Therefore it is possible to encounter memory overflow during dynamic allocation process. In such situations, the memory allocation functions mentioned above will return a null pointer.

Allocating a block of memory:

A block mf memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form:

ptr=(cast-type*)malloc(byte-size);

ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with size byte-size.

Example:

x=(int*)malloc(100*sizeof(int));

On successful execution of this statement a memory equivalent to 100 times the area of int bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int

Allocating multiple blocks of memory:

Calloc is another memory allocation function that is normally used to request multiple blocks of storage each of the same size and then sets all bytes to zero. The general form of calloc is:

ptr=(cast-type*) calloc(n,elem-size);

The above statement allocates contiguous space for n blocks each size of elements size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space a null pointer is returned.

Releasing the used space:

Compile time storage of a variable is allocated and released by the system in accordance with its storage class. With the dynamic runtime allocation, it is our responsibility to release the space when it is not required. The release of storage space becomes important when the storage is limited. When we no longer need the data we stored in a block of memory and we do not intend to use that block for storing any other information, we may release that block of memory for future use, using the free function.

free(ptr);

ptr is a pointer that has been created by using malloc or calloc.

To alter the size of allocated memory:

The memory allocated by using calloc or malloc might be insufficient or excess sometimes in both the situations we can change the memory size already allocated with the help of the function realloc. This process is called reallocation of memory. The general statement of reallocation of memory is :

ptr=realloc(ptr,newsize);

Saturday, March 13, 2010

My solution for Queue

Here is my solution for Queue which we are asked to be finished by Fardad in this week class.

//queue.h
class Queue;
class Node{
int data;
Node* next;
Node(int data , Node* next = (Node*)0);
friend class Queue;
};

class Queue{
Node* head;
Node* tail;
public:
Queue(); // empty list
void add(int data); // adds to tail of the list
int remove(); // removes from head
bool IsEmpty();// true if empty
~Queue();
};

//queue.cpp
#include "queue.h"
Node::Node(int data , Node* next){
this->data = data;
this->next = next;
}
Queue::Queue(){
head = (Node*)0;
tail = (Node*)0;
}
void Queue::add(int data){
if (head==tail && !head){
Node* temp = new Node(data, tail);
head = temp;
tail = temp;
}
else{
Node* temp = new Node(data, tail->next);
tail->next = temp;
tail = temp;
}
}
int Queue::remove(){
int data = head->data;
Node* ToDel = head;
head = head->next;
delete ToDel;
return data;
}
bool Queue::IsEmpty(){
return !head;
}
Queue::~Queue(){
while(!IsEmpty()){
remove();
}
}
//queuemain.cpp
#include
using namespace std;
#include "queue.h"

int main(){
Queue Q;
int i;
for(i=10;i<=50;i+=10){
cout<<"Add into a queue "< Q.add(i);
}
while(!Q.IsEmpty()){
cout< }
return 0;
}
output as:

Some concepts in C and C++

Those questions are found when I prepared for my job interview question.They might be simple questions but I've never think of them.Hope that I can understand better, and also for anyone are learning C and C++.

Why the constructor can't be virtual?
A: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. The reason is a constructor can not be virtual because at the time when the constructor is invoked the virtual table (vtable) would not be available in the memory.
Virtual allows us to call a function knowing only the interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the what you want to create exactly. Hence call to a constructor can't be virtual.

Difference between delete and destructor in c++?
Delete can deallocate the memory of those objects which are created by using the new operator whereas the destructor can delete any object once the life of the object ends.(this is normally when a program ends)

Differences between Structure and Class.
1.Structure cannot be inherited.
2.Structure is a value data type whereas Class is a reference type.
3.All declarations inside a structure are by default public whereas by default all the members inside the class are private.
4.Structure instances cannot be used as operand to arithmetic operators like +-*/ . But object (instances of classes) can be (using operator overloading).
5.A structure can't declare protected members,a class can.
6.We can't inherit a structue from another structure, which we can do in class.

Difference between "overloading" and "overriding"?
Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

There are many valuable comments on it.

Thursday, February 18, 2010

Challenges solution : MyPrint()

Here is my solution for MyPrint().

#include <stdio.h>
#include <conio.h>
#include <stdarg.h>
void print_int_hex(int val,int alg)
{
int i;
if (val==0)
{
_putch('0');
}
else
{
if (val/alg)
{
i = val % alg;
print_int_hex(val/alg,alg);
}
else
i = val % alg;
if (i>9)
{
i=i-10+'a';
_putch(i);
}
else
_putch(i?i+48:48);
}
}

void print_float(double val)
{
int temp = val *100;

if (val==0)
{
_putch('0');
}
else
{
print_int_hex(temp/100,10);
_putch('.');
print_int_hex(temp%100,10);
}
}

void MyPrint(char* pString,...)
{
int i=0;
int ch;
const char* str;
double f;

va_list args;
va_start(args, pString);
while (pString[i])
{
if (pString[i]=='%')
{
i++;
switch(pString[i])
{
case 'c':
ch = va_arg(args, int);
_putch(ch);
break;
case 's':
str = va_arg(args, const char*);
_cputs(str);
break;
case 'd':
ch = va_arg(args,int);
if (ch<0)
{
_putch('-');
ch=ch*(-1);
}
print_int_hex(ch,10);
break;
case 'x':
case 'X':
ch = va_arg(args, int);
print_int_hex(ch,16);
break;
case 'f':
f = va_arg(args, double);
print_float(f);
break;
}
i++;
}
_putch(pString[i]);
i++;
}
va_end(args);
}

int main()
{
MyPrint("int %d, char %c, string %s, hex %x, float %f", -102, 'A', "hello", 31, 12.34567);
return 0;
}

Result as:
int -102, char A, string hello, hex 1f, float 12.34
For float, I thought might be have better solution.

Wednesday, February 3, 2010

Optimizing bio_display()

Here is the source code:
void bio_display(const char *str, int row, int col, int len){
bio_move(row, col);
if(len <= 0){
bio_putstr(str);
}
else{
int i;
for(i=0; i< len && str[i];i++){
bio_putch(str[i]);
}
for(;i< len;i++){
bio_putch(' ');
}
}
}
To optimize it, I got this solution :
void bio_display(const char *str, int row, int col, int len) {
bio_move(row, col);
if( len <= 0)
bio_putstr(str);
else {
for(;len>0; len--) bio_putch(*str ? *str++ : ' ' );
}
}
From the same logic, it also can be shortened as:
void bio_display(const char *str, int row, int col, int len) {
for((bio_move(row, col), (len <= 0)?bio_putstr(str):0); len>0; len--)
bio_putch(*str ? *str++ : ' ' );
}
I tried to use bio_putstr() instead of bio_putch() which Farad mentioned in the class, but considered the length of the string I insisted to use bio_putch().

Thursday, January 28, 2010

week 3 challenge - bio_putint(int val)

My result as below, has been test.
void sep(int val)
{
    int i;
    if (val/10)
    {
        i = val % 10;
        sep(val/10);
    }
    else
        i = val % 10;
    _putch(i?i+48:48);
}
void bio_putint(int val)
{
    sep(val);
}