Thursday, February 19, 2009

Calling a web-method from a hyperlink

I've observed this trend in tech-industry, we will find many articles on difficult topics but its very difficult to find answers to simple questions.
I once faced such a question and figured it out after some experimenting.
The question is how to call a web method (a method exposed by a web service) from a plain HTML web page??? In other words how to call webmethods directly??? Well, folks its really simple to call a web-method from a hyperlink... The HTML goes like this...

<html>
<body>
<p>
<a href = "http://localhost/abcGateway/xgateway.asmx/dostartRecording?recchannel=19">
Start Recording
</a>
</p>
</body>
</html>









Where I have a web-service whose main page is xgateway.asmx, and it exposes a web method "dostartRecording", this web-method takes a parameter "recchannel".
 Hi ho now you can enjoy calling a web method directly from your web page.

Difference between Framework and API


Software Framework:

It is a re-usable design for a software system (or subsystem). A software framework may include programs, code libraries, a scripting language, other software to help develop and glue together the different components of a software project. Various parts of the framework maybe exposed through an API.
dot_net_framework
Microsoft's .Net framework is a very good example of a software framework. It contains compilers, class libraries, an IDE(Visual Studio), and a runtime. Another example is OpenCog: A Software Framework for Integrative Artificial General Intelligence.

API (Application Programming Interface):

It is a set of routines( AKA methods or functions), data structures, object classes, and/or protocols provided by libraries and/or operating system services in order to support the building of applications.
Good examples of API are Microsoft TAPI(Telephony API), or MySQL connectivity API, or Google MAPS API.










In Microsoft's .Net Framework, only the Base Class Library or BCL are the API. The framework is a bigger thing. API is a part, framework is a whole. Although, API can exist on its own, without a framework as well.
A friend was telling me yesterday "I interviewed someone, he had no idea about difference between Framework and an API". I replied"Owwww that's dumb". In fact, had no idea about the difference either.
Well, I think this much is enough to illustrate the difference between Software Framework and API, or Software Framework vs API.



Another example of software platform is pictured below, Google's Android Mobile Platform, 


android-system-architecture


You see this platform contains many things, a kernel for low level tasks, certain libraries for OS level chores, Delvik Virtual Machine (an enriched clone of Java Virtual Machine), and the "Application Framework" on on the "third story". Any software we write for Android smart phones will reside on 4th floor, which is labelled "Applications" here.
Some might search this topic as API vs framework, this blog entry will be equally helpful for those friends as well.

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app.

https://www.odesk.com/users/~012d73aa92fad47188

Wednesday, February 18, 2009

Working with a std::vector inside an std::map

Suppose we have a map which contains std::string keys and std::vector values, it will be defined as following

std::map<std::string,std::vector> map_str_vector;

I was doing something like this:

std::vector vec_active_channels;
vec_active_channels.push_back("Some string");

map_str_vector["Some_Key"] = vec_active_channels;

Later in the code, I was erasing an element from the vector like this:

std::vector vec_temp = map_str_vector["Some_Key"];
vec_temp.erase(0);

But you know what, later on when I examined the vector, its size won't be changed.
The reason is when I did:
std::vector vec_temp = map_str_vector["Some_Key"];

A new object of std::vector was created and placed in vec_temp, and the call
vec_temp.erase(0);
was erasing first element from the new object.

The right way to remove a std::vector from a std::map value field is given below

map_str_vector["Some_Key"].erase(0);

We can put an if statement behind this erase operation, to check whether this key exists in the map or not.

C/C++ Common interview questions

Tech Interview C++

I got 2 years of experience and looking for a new job these days. I thought to write down the most commonly asked technical questions, they asked me.
Questions
  1. What is the difference between new/delete and malloc(), free()?
  2. What if we allocate memory using new and release it with free()?
  3. Why do we need virtual destructors?
  4. C++ compiler provides a copy costructor and overloaded assignment operator by default, why do we need to write our own copy constructors?
  5. Have you ever used STL, why do we use STL, if yes then what containers?
  6. What are function pointers?
  7. What happens when a callback funtion is called?
  8. Differences between C and C++?
  9. Write a program in C language which prints "Hello World!" on screen, without using even a single semi colon ';'.
  10. Swap two integers without using a third one.
  11. If we have a struct struct ABC{int n; char c;}; Suppose an integer takes 4 bytes. How many space in memory will be occupied by an instance of this structure?
  12. Why do we need pure virtual functions?
  13. What are friend functions?


Answers









  1. malloc() and free() are C language functions used to allocate and free memory. These funtions simply allocate memory for a variable. new/delete are C++ language operators, new operator allocates memory needed by an object and then calls the constructor function of that object to perform any initializations. Applying delete to an object causes a call to the destructor function of that object, and afterwards a release of memory. 
  2. The memory occupied by the object will be returned to Operating System, but the destructor of that object won't be called.
  3. Suppose you use delete with a base class pointer to a derived class object to destroy the derived-class object. If the base-class destructor is not virtual, then delete, like a normal member function, calls the destructor for the base class, not the destructor for the derived class. This will cause only the base part of the object to be destroyed.
  4. C++ default copy constructor and assignment operator perform a member to member copy of the instance variables contained by an object. If we have instance variables of pointer types in an object say object1, and we perform a straight member to member copy to object2. The pointer type instance variables in both object1 and object2 will be pointing to same memory locations. And if we delete one object, the pointers in other object will become invalid without that object ever knowing. This will cause a program crash.
  5. Standard Template Library is a standard peer revised and optimazed collection of containers and algorithms. Because it is standard, it is cross platform and any compiler supporting C++ standards must work with STL. Using STL cuts developement time, and increases program reliablity. I have personally  used vector, queue, and map containers.
  6. Callback funtions are also known as delegates. Callbacks are used when we want a method to be called from some other thread of execution. For example the boost::regex_grep function, its parameters are a large(or maybe small) string, a regular expression, and a call back funtion. The string can be very very long and finding the matches may take too much time, so whenever this boost::regex_grep funtion finds a match satisfying the supplied regular expression it will call the funtion in our code specified in callback argument.
  7. A callback funtion is basically a pointer to the starting location of some code, AKA funtion pointer. When a callback funciton is called, its parameters are pushed to stack. Then the control is transferred to the funtion pointer location and then the control resumes from the function pointer location.
  8. C is a functions based language, and it does not provide anything to group data with funtions which will manipulate that data. C++ is an Object Oriented language, and it provides rich constructs to group data with funtions which will manipulate it (AKA Data Encapsulation). It is possible to implement solutions close to real world using C++, by implementing heirarchical designs. C++ saves developement time by code resue through Inheritance as well.
  9. void main(void){if(printf("Hello World!")){}}
  10. int a = 10; int b = 20; a = a+b; b = a - b; a = a - b;
  11. An instance of this struct will take 8 bytes, because the C++ language compiler performs memory padding at every 4 bytes.It means, that a variable occupies at least 4 bytes in memory. 4 bytes is the basic unit of allocation in memory. This struct will take 4 + 1 = 5 bytes in memory, but the compiler will scale it up to 8 bytes by memory padding.
  12. If a class includes a pure virtual function, it can't be instantiated. A class which contains a pure virtual function is an abstract class. Abstract classes are also known as interfaces. In situations where we are defining interfaces, which will be used to contain pointers to child class objects only there must be some way to stop people from creating objects of these interfaces. Pure virtual functions server the purpose. Because an it compiler will stop us from creating an object of an Abstract class.
  13. Friend funtions are funtions which are not the members of a class, but can access the private members of an instance of that class.
Bonus:

A few unanswered questions as well, go figure!
1. Write a function such that if we input it 8, it returns 3 and if we input it 3 it returns 8. Don't use conditional statements.

2. Reverse a singly linked list in O(n) time and O(C) space complexity.

3. Find the third largest integer from an array, time constraints are O(n) and space complexity must be O(c).

4. INTERVIEW QUESTION: Malloc: Write an aligned malloc and free function that supports allocating memory such that the memory address returned is divisible by a specific power of two. CURRENT ANSWER: attached QUESTION FROM READER: I believe line 8 is wrong. It is assigning p2 equal to a (void **) and returning p2, but the function returns void *. I believe the corrected line should be p2[0] = (void * )(((size_t)(p1) + offset) & ~(alignment - 1)); line 10 should be return p2[0]; or could be return *p2;

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app.

https://www.odesk.com/users/~012d73aa92fad47188