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(API vs. Framework, Framework vs. API)


Software Framework vs. API

In this article, I will quickly explain what a software framework is, what is an API, and are key differences between an API and a software framework.


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, and other software to help develop and glue together the different components of a software project. An API(Application Programming Interface) may expose various framework parts.
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.
Now that you know what a framework is let me tell you what an API is so that you can answer the question Framework vs. API.

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 to support applications' building.
Good examples of API are Microsoft TAPI(Telephony API), MySQL connectivity API, and Google MAPS API.










In Microsoft's .Net Framework, only the Base Class Library or BCL is the API. The framework is a bigger thing. API is a part, and the framework is a whole. Although, API can exist on its own without a framework as well.
Now you know what's the answer to API vs. Framework :)


A friend told me yesterday "I interviewed someone; he had no idea about the 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 is enough to illustrate the difference between Software Framework and API, or Software Framework vs API.



Another example of a 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, Dalvik Virtual Machine (an enriched clone of Java Virtual Machine), and the "Application Framework" on the "third story." Any software we write for Android smartphones will reside on the 4th floor, labeled "Applications" here.
Some might search this topic as API vs framework; this blog entry will be equally helpful for those friends as well.

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(on the time of writing this blog post a decade ago, time flies!) and I am looking for a new programming job these days. I thought to write down the most commonly asked technical questions which are asked during a C++ job interview.

C++ Job Interview Questions
  1. What is the difference between new/delete and malloc(), free() method calls?
  2. What happens if we allocate memory using new and release it with free() in your C++ code?
  3. Why do we need virtual destructors in C++?
  4. C++ compiler provides a copy constructor and overloaded assignment operator by default, why do we need to write our own copy constructors?
  5. Have you ever used STL(Standard Template Library), why do we use STL, if yes then what containers?
  6. What are function pointers in C++?
  7. What happens when a callback function is called or invoded in your C++ code?
  8. What are the Differences between C and C++ programming language?
  9. Write a program in C language which prints "Hello World!" on screen, but twist is you must do it without using even a single semi colon ';'.
  10. How to Swap two int variables without using a third integer.
  11. If we have a struct defined as following struct ABC{int n; char c;}; Suppose an int takes 4 bytes of memory space. 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 to C++ Interview Questions









  1. malloc() and free() are C language functions used to allocate and free memory. These functions simply allocate memory for a variable. The 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 initialization. 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 optimized 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 development time, and increases program reliability. I have personally  used the std::vector, std::queue, and std::map containers.
  6. Callback functions are also known as delegates in C# world. 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 function. The string can be very very long and finding the matches may take too much time, so whenever this boost::regex_grep function finds a match satisfying the supplied regular expression it will call the function in our code specified in callback argument.
  7. A callback funtion is basically a pointer to the starting location of some code, AKA function pointer. When a callback funciton is called, its parameters are pushed to stack. Then the control is transferred to the function 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 functions which will manipulate that data. C++ is an Object Oriented language, and it provides rich constructs to group data with functions which will manipulate it (AKA Data Encapsulation). It is possible to implement solutions close to real world using C++, by implementing hierarchical designs. C++ saves development time by code reuse 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;