.dp-highlighter { font-family: "Consolas", "Monaco", "Courier New", Courier, monospace; font-size: 12px; background-color: #E7E5DC; width: 99%; overflow: auto; margin: 18px 0 18px 0 !important; padding-top: 1px; /* adds a little border on top when controls are hidden */ } /* clear styles */ .dp-highlighter ol, .dp-highlighter ol li, .dp-highlighter ol li span { margin: 0; padding: 0; border: none; } .dp-highlighter a, .dp-highlighter a:hover { background: none; border: none; padding: 0; margin: 0; } .dp-highlighter .bar { padding-left: 45px; } .dp-highlighter.collapsed .bar, .dp-highlighter.nogutter .bar { padding-left: 0px; } .dp-highlighter ol { list-style: decimal; /* for ie */ background-color: #fff; margin: 0px 0px 1px 45px !important; /* 1px bottom margin seems to fix occasional Firefox scrolling */ padding: 0px; color: #5C5C5C; } .dp-highlighter.nogutter ol, .dp-highlighter.nogutter ol li { list-style: none !important; margin-left: 0px !important; } .dp-highlighter ol li, .dp-highlighter .columns div { list-style: decimal-leading-zero; /* better look for others, override cascade from OL */ list-style-position: outside !important; border-left: 3px solid #6CE26C; background-color: #F8F8F8; color: #5C5C5C; padding: 0 3px 0 10px !important; margin: 0 !important; line-height: 14px; } .dp-highlighter.nogutter ol li, .dp-highlighter.nogutter .columns div { border: 0; } .dp-highlighter .columns { background-color: #F8F8F8; color: gray; overflow: hidden; width: 100%; } .dp-highlighter .columns div { padding-bottom: 5px; } .dp-highlighter ol li.alt { background-color: #FFF; color: inherit; } .dp-highlighter ol li span { color: black; background-color: inherit; } /* Adjust some properties when collapsed */ .dp-highlighter.collapsed ol { margin: 0px; } .dp-highlighter.collapsed ol li { display: none; } /* Additional modifications when in print-view */ .dp-highlighter.printing { border: none; } .dp-highlighter.printing .tools { display: none !important; } .dp-highlighter.printing li { display: list-item !important; } /* Styles for the tools */ .dp-highlighter .tools { padding: 3px 8px 3px 10px; font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left: 3px solid #6CE26C; } .dp-highlighter.nogutter .tools { border-left: 0; } .dp-highlighter.collapsed .tools { border-bottom: 0; } .dp-highlighter .tools a { font-size: 9px; color: #a0a0a0; background-color: inherit; text-decoration: none; margin-right: 10px; } .dp-highlighter .tools a:hover { color: red; background-color: inherit; text-decoration: underline; } /* About dialog styles */ .dp-about { background-color: #fff; color: #333; margin: 0px; padding: 0px; } .dp-about table { width: 100%; height: 100%; font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; } .dp-about td { padding: 10px; vertical-align: top; } .dp-about .copy { border-bottom: 1px solid #ACA899; height: 95%; } .dp-about .title { color: red; background-color: inherit; font-weight: bold; } .dp-about .para { margin: 0 0 4px 0; } .dp-about .footer { background-color: #ECEADB; color: #333; border-top: 1px solid #fff; text-align: right; } .dp-about .close { font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; background-color: #ECEADB; color: #333; width: 60px; height: 22px; } /* Language specific styles */ .dp-highlighter .comment, .dp-highlighter .comments { color: #008200; background-color: inherit; } .dp-highlighter .string { color: blue; background-color: inherit; } .dp-highlighter .keyword { color: #069; font-weight: bold; background-color: inherit; } .dp-highlighter .preprocessor { color: gray; background-color: inherit; }

Saturday, June 7, 2008

Boost interview questions

- What is boost? And why is it useful?
- What is a shared pointer?
- What is the difference between shared pointer, auto pointer and intrusive pointer?
- How is shared pointer implemented internally?
- How would you convert from string to integer using boost?

#include
#include
#include

/*
Implement as simple a class as possible to pass the test case below.
If possible,
- use only a single container
- do not use a void pointer
- use any stl/boost/mpl algorithms that are of use
*/


class cNamedVariableContainer
{
public:
template
void manage(const std::string& name, T& varRef)
{
}

public:
void setValue(const std::string& namedVariable, const std::string& value)
{
}
};



int main()
{
cNamedVariableContainer nvc;

int myInt(0);
double myDouble(0.);
std::string myString("");

nvc.manage("int", myInt);
nvc.manage("dbl", myDouble);
nvc.manage("str", myString);

nvc.setValue("int", "43");
nvc.setValue("dbl", "123.8");
nvc.setValue("str", "hello");

std::cout << myInt << std::endl;
std::cout << myDouble << std::endl;
std::cout << myString << std::endl;

assert(myInt==43);
assert(myDouble==123.8);
assert(myString=="hello");

return 0;
}

Friday, June 6, 2008

Interview Questions -- London Finance IT

I have been interviewing for a C++ software developer position at some of the UK finest investment banks and hedge funds. Here I'll share some of the questions that I was asked in a series of posts. But to be fair, I won't mention the names of those companies.

Those are actual questions I have been asked:

C++ Questions:

1- When would you prefer passing a reference to a function than a pointer?
2- Where is the const keyword used in C++ and what does it mean in each of those places it is used?
3- What is a mutable variable? Why might we need one?
4- What is a dynamic cast? What is RTTI?
5- What is an exception? When would you use exceptions? And why are they better than retyrning error codes?
6- How are exceptions implemented in C++?
7- How can a function guarantee that its resouces are released if an exception is thrown in the middle? -- Use RAII.
8- What is an exception safe function? What are the three types of exception safety?
9- What is a dangling pointer? And how does it usually happen?
10- What is a memory leak? How would you go about fixing it?
11- What is polymorphism? Virtual functions? Virtual tables?
12- When do you need a virtual destructor? Why is it not a good idea to always use virtual destructors?
13- Why is it a bad idea to throw exceptions from a destructor?
14- What is a pure virutal function? Why might we need to use it?
15- When do we absolutely need a dynamic cast? What alternatives can we have to dynamic casts?
16- Can pure virtual functions have an implementation? Is it a good idea to give them default implementation? Why?
17- What is a design pattern?
18- Explain Observer, Singleton and Bridge design pattern?
19- What is an Agile development?
20- Why do we need operator overloading? What concerns might we have with overloading?
21- Assume we have four istances of the same class, A, B, C, and D. What is the minimum number of operators called for this operation A = B * C + D?
22- Which could be potentially faster: accessing elements of a 2D array column-wise or row-wise? i.e., traversing columns or rows?
23- How do you prevent an object from being copyable? When might you need to do that? And in this case, how do you prevent methods of this object from calling its copying functions? What errors might arise?
24- How does C++ name mangling work?
26- How is polymorphism implemented in C++?
27- Can we have static virtual methods? Why?
....

rest to follow as I remember them.

Thursday, April 3, 2008

كان زمان

جلس بوقاره وشيخوخته يروي لنا قصة حياته من بدايتها... كيف أنه كان طفلاً صغير لا يستطيع فراق أمه أو الإنفصال عنها للحظات، وظن حينها أنها جزء من حياته وستظل كذلك إلى الأبد... لكنه كبر وشب عن الطوق، وقضى الله على أمه الموت فماتت، لكن كان له عوضه بإخوة أقرب إليه من قلبه بين ضلعيه، كأنه معهم روح واحدة في أجساد متفرقة... أحبهم وأحبوه وظن أن لن يستطيع الحياة بدونهم، لكنهم أيضاً شبوا وتزوجوا وكبروا، ثم أخذ كل منهم وجهته وسافر بعيدا عن بلدته، وبدأ حياته الجديده مع أسرته وأبناءه... ثم جاء دوره هو فتزوج وأنجب من الأبناء والبنين خمسة كالبدور، رباهم هو وزوجته وظن أن هذه هي حياته إذا، وهذه هي السعادة بعينهاً... ماذا يريد أكثر مما أنعم الله عليه به؟ بيت يقيه برد الشتاء وقيض الصيف، ويستره وأهله، وزوجة مخلصة محبه وأولاد طائعين ، وبسطة في العيش والمال... لكنهم، أيضا، شبوا وكبروا كما شب وكبر هو قبلاً... جاء للبنات من يرضين دينهم وخلقهم فزوجهن جميعاً وذهبن من بين يديه، ثم تزوج أبناءه أيضاً وغادرو العش الذي مافكر يوماً واحدا في أن أيا من ساكنية يستطيع التنفس خارجه... غادروه ليبدأوا هم أيضا حياتهم الجديده... لم يطل الزمان على عشه الجديد، فقضى الموت على حبيبة قلبه وشريكة عمره فماتت، وبقي هو ينتظر رحمة الله ودوره في المغادرة إلى عالم أكثر نقاء وخلوداً، عالم تدوم فيه اللذات...

ترى في عينيه خبرة في الحياة وحقيقتها، وتحس من كلامه قناعته بحياته وبما أنجر وعذره لنفسه في ما قصر، وإستعداده للموت، بينما أنت الشاب تفزع من مجرد ذكر هذه الكلمة المخيفة المريبه، غير مدرك بأنه كان أيضا شابا مثلك وفي سنك يخاف الموت ويخشاه ويريد من حياته أكثر ما أعطته، لكنه اليوم قنع بعد أن عارك الحياة... أنت تخاف النهاية لأن لديك من العمر فسحة، ومن العمل والمتستقبل الكثير إن كتب الله لك أن تعيش كما عاش، لكنه قد تلذذ بكل لذات الحياة، ثم تجرع مرارة فقدها، وعارك الحياة ليعلم في النهاية أن لكل بداية نهاية، فأصبح قلبه خالياً من كل شئ إلا من رحمة الله.

هكذا هي سنة الحياة، لا شئ يدوم، جميلاً كان أو قبيحاً... وحياتنا لا تقاس بالأنفاس التي نجترها، والتي يجترها كل حي، وإنما بأعمالنا التي نقدمها لإصلاح دنيانا كخلفاء لله في أرضه، وبين يدي آخرانا... لا تجعل من عمرك عبئأ على عاتقك، تريد الخلاص منه بأي وسيلة، فعمرك رأس مالك، فلتكن أكثر حرصا أين وكيف ولمن وعلى من تصرفه، ثم بعد هذا كله فكر في إستمثار جزء كبير منه لرحلة طويلة وسفر شاق، لحياة الخلود... ومع هذا فلا تنس نصيبك من الدنيا ولكن قدر نعمتي الصحة الصحة والفراغ التي أسداها الله عليك...

إن تنعمت بلذة فإعلم أن لها نهاية فأحسن إستخدامها، وإن أصبت بمصيبة فإعلم أن لها نهاية أيضا فهون عليك.

هاقد ولى رمضان، تاركا ألسنة البعض معقودة من شدة الحزن، وألسنة آخرين تقول (رمضان ولى هاتها ياساقي ... مشتاقة تسعى إلى مشتاق)، وكيفما كان حالي أو حالك، لتكن هذه بداية جديدة لحياة أكثر عطاءً وحيوية، ولننظر إلى حياتنا كفرص على ظهر الأرض لفعل شئ بدلا من النظر إليها كعبء ثقيل نشعر بالسعادة مع كل جزء نقتله منها.. إن كنت طالباً فإجعل من تعليمك جهاداً و إملا قلبك وعقلك بالعلم، وإن كنت موظفاً أو عاملا فلتكن حياتك نموذجاً للعالم الذي تحلم به وتريد أن تراه... إن كان لديك وقت فراغ فلا تفكر بطريقة لقضاءه والتخلص منه، بل إنظر إليه ككنز يمكنك إستخدامه وإضافته إلى رصيد حياتك الذي يتناقص مع شروق كل يوم جديد، مالم تضف إليها أنت رصيدا آخر يجعل من الوقت الباقي لحياتك أحد المقاييس لحياتك وليس المقياس الوحيد.

أتمنى من الله أن نكون ممن غادرنا شهر رمضان بلا ذنب، وعيدكم مبارك

Friday, February 15, 2008

أن تحترق

أن تحترق هو أن تحس بحرارة في أحشائك، وبرد في أعضائك وأطرافك، وأن تعالج غصة في حلقك لا تقوى على الخلاص منها... أن تحترق هو أن تعد الثواني وتستمع لعقارب الساعة تقرع شوكاتها على الجدار، هو أن يطول الليل ويظلم كنفسك التي بين جنبيك.... أن تحترق هو أن يبلغ بك اليأس درجة تصبح فيها حياتك عبئا ثقيلاً عليك تتمنى الخلاص منه، وعندها تتمنى للحظة ما لو يقف كل شئ، الحركة والسيارات والضوء والضجيج والحيوانات والحشرات وأن يقف الكون... تتمنى أن لا تدور الأرض... أن تحترق هو أن تتمنى أن تغمض عينيك ثم تفتحها وترى نفسك في عالم آخر غير عالمك الذي تعيشة... أن تحترق هو أن ترى الزمان كذبة كبرى، لأن دقيقتك ليست كدقيقة الناس وساعتك تمر عليك كدهر بائس... لو يقاس الدهر بعذابات المتألمين اليائسين الكادحين أمثالك لإبيضت رءوسهم قبل المشيب



ما أتعسك وأشقاك وما أشد عناءك أيها المسكين. هل وصل بك اليأس حد تمنى الموت؟ أنت وحدك جزيرة لا يعيش بها إلا أنت فأعمرها بما شئت، أعمرها باليأس تعش حياتك يائسا أو أعمرها بالياسمين إن شئت تعش حياة حبورا ونشوانا في غبطة وسرور دائمين.... أن نور نفسك هو النور الوحيد في ظلمتها، هو نهارك، فقليلا من الأمل يبلغك شط الآمان، النفس يتبعه آخر والدقيقة تتلوها أخرى وما حياتك وحياة كل مخلوق على ظهر البسيطة إلا هرولة نحو مصير محتوم، كلنا سيجري وكلنا سيقز القفز تلك إلى المجهول، الموت لكن هناك من يهرول نحو النهاية مدركا تمام الإدراك أنه واصل لا محالة فيجعل من رحلته سفرا جميلا يعيشه ولا يستثقله، وهناك من يتعثر في الطريق من شدة اليأس ومن علمه بالنهاية فيتمنى لو تهرول النهاية إليه قبل أن يصل إليها فلا تكن من الصنف الثاني



حياتي فقدت كل معانها، لم أعد أعرف سببا واحدا للإستيقاظ كل صباح، أراني أكرر ما عملت كل يوم، وأفعل ما يفعل الآخرون علي أجد يوما تعريفا لنفسي أو ماهيتي أو لماذا أنا؟ بل لعلي أعرف يوما ما من أنا ومن أكون؟ أرى كل يوم يجعلني أقل حيوية، لا أطيق النظرإلى ما يستمتع الناس بالنظر إليه.... لا أطيق الأطفال... منظر الطفولة والبراءة يقززني وليس له أي معنى في قاموسي، كما أني لا أرى ما يراه الآخرون من جمال الحياة؟ كيف يكون الجمال داخلي كما تحدثني؟ إن كان الجمال في أنفسنا فلم نبحث عنه في الزهرة الأنيقة، والمرأة الجميله والمنظر الجذاب واللحن العذب؟ لم لا نجلس مع أنفسنا مثلا في غرفتنا، نغلق كل نوافذها وفي الظلمة الحالكة نجعل شعاع النور في أنفسنا وجمالنا الداخلي يضئ حياتنا؟ كذب كل ما يقولون... حياتنا ليست جميله بل كارثية بكل ما تحمل الكلمة من معنى، رحلة معاناة من المهد الى اللحد، نعمل ونكد أنفسنا وعقولنا في تمثلية يلعب كل من دوره ببراعة لأننا لا نصدق للحظة واحدة أننا ممثلون، لكنك إن كنت مكاني وتحترق مثل ما أحترق لرأيت قبسا من الحقيقة: حياتنا كذبة كبرى، وتراجيدية لا تنتهي، والفرق الوحيد بيني وبينك هو أنني أدرك ذلك بينما تنكره أنت



بل حياتك أنت وحدك كذبة كبرى وحياة كل إنسان هي ما يعيشه... أتظن شخصا تحت جهاز التنفس الإصطناعي لسنوات حي؟ الحياة ياصديقي لا تقاس بعدد الأنفاس التي نتجرها أو السنوات التي نعيشها وإنما ماذا نفعل بها... لكي تدرك لحياتك معنى يجب عليك أولا فهم ماهيتها وماذا تكون: حياتك وحياة كل إنسان على وجه الأرض هي ما نفعله في فترتنا القصيرة عليها، فإجعل من حياتك سمفونية تطرب الأسماع، أو شمعة تحترف لتضئ للآخرين دروب حياتهم... ثم ليكن هدفك الأسمى في هذه الحياة مرتبط بالعلوي، بالله، وليس بشخصك أنت، فروحك التي بين جنبيك ماهي إلا وديعة عندك إلى حين.... الجمال الخالد والمعنى الحقيقي لحياة الإنسان يجب أن لا يرتبط معنىً دنيوي ينتهي مع حياة الشخص، كالمال والجمال والشهرة والنجاح، بل يكون مرتبطا بالله الذي له الخلود أبد الدهر، عندها يكون معنى حياتك خالدا... لن تشعر بطمأنينة القلب وراحة البال إن كان سببها دنيوي لأن كل دنيوي زائل، لكنك إن غرست في ذاتك حب الله، وإشتشعرت عظمته، وخفت غضبه ورجوت رحمته فإن عندئذ تكون قد قنعت بأن حياتك على ظهر هذه البسيطة ماهي إلا مقدمة وإختبار لحياة أكثر خلودا، وستعمل حينها على بلوغ معنى الحياة اللازائل بعد موتك، ولن تلهث نحو النهاية بعدها، بل ستخطو خطوات واثق نحو البداية الحقيقه...

Tuesday, January 1, 2008

صناعة القائد: وأما من جائك يسعى

روي أن رسول اللّه صلى اللّه عليه وسلم كان يوماً يخاطب بعض عظماء قريش، وقد طمع في إسلامه، فبينما هو يخاطبه ويناجيه إذ أقبل عبدلله ابن أم مكتوم، وكان رجلاً أعمى ممن أسلم قديماً، فجعل يسأل رسول اللّه صلى اللّه عليه وسلم عن شيء ويلح عليه، وود النبي صلى اللّه عليه وسلم أن لو كف ساعته تلك، ليتمكن من ذلك الرجل طمعاً ورغبة في هدايته وعبس في وجه ابن أم مكتوم وأعرض عنه، وأقبل على الأخر، فعاتبه الله تعالى بقوله: (‏عبس وتولى أن جاءه الأعمى * وما يدريك لعله يزكى)... أدبه ربه بأحسن أدب وأعظمه فأحسن تأديبه، وجعله مفخرة الإنسانية جمعاء، ومثالها الأعلى في القيادة والريادة، . كان صلى الله عليه وسلم لا يفرق بين سيد قرشي وعبد حبشي. كلهم سواء في الإسلام فزوج متبناه زيد بن حارثه، من سيدة قريش زينب بنت جحش، وأسدى على سلمان الفارسي المللوك في الجاهلية هوية (سلمان منا أهل البيت)! القادة الحقيقيون لا يفرقون بين تابعيهم أو يحقروا شأنهم ولا يقفون عند مجرد إصدار الأوامر والنصح للآخرين بل ويتجاوزونه إلى حد الإعتقاد بخيريتهم و العفو عن خطاياهم والإشفاق عليهم منها وتحسس مواقع الخير في نفوسهم وتنميتها.

غالبا ما تنتشر بين بعض القادة ثقافة الطهرانية لذواتهم ودونية الآخرين وتخلفهم، فهم المتعلمون والمتنورون وغيرهم رعاع جاهلون ، عالة على المجتمع، وسلم يستعمله هؤلاء "القادة" (إن صحت تسميتهم بذلك) للتسلق نحو إرضاء كبرياءهم وإعلاء شأن أنفسهم... عندما نؤمن بخيريتنا وشيطانية الآخرين، أو عدم إحترامهم كأنداد لنا، فلن نكون قادة لأنه ببساطة لن يتبعنا أحد. وعندما نعتزل الناس لأننا أطهر منهم روحا، أو أذكى منهم عقلا، فإننا نكون قد إخترنا لأنفسها أسهل الطرق للهروب من واجبات القيادة وأيسرها مؤنة...

ألاحظ كثيراً بين طبقة مثقفينا ألسنة سليطة على أوضاعنا في بلادنا حتى أنها لتبدو ظاهرة متأصلة في تراثنا أن نفخر بكوننا أفضل من بني جلدتنا وأطهر منهم روحا وأطيب قلبا. بل ويتجاوز ذلك إلى حد توصيف الداء والدواء وتشخيص الأخرين بأبشع العلل والأمراض، فهم (الثعابين) ونحن الراقصين على رؤوسهم، وهم (شعب جاهل ومتخلف) ونحن (ذوي العقول التقدميه والمتنوره)، وهم (رعاع الناس) ونحن فخرهم وزينتهم، وما ننساه أو نتناساه دائما أن (أنفك منك ولو كان أجدع)، وأن لا أحد يملك الحق للحكم على أحد.... قد يكون القائد محقا بإيمانه بضرورة تغيير الآخرين وتثقيفهم والإرتقاء بهم إلى مستوى يمكنهم من رؤية ما يراه، لكن إحترام الناس والعطف على ضعفهم ونقصهم وأخطائهم هو الطريق الأوحد لتأليف القلوب حول القائد... كونك قائداً لا يعني أنك أفضل ممن يتبعونك، فقد يكون منهم من هو أذكى منك وأصلح للقيادة، وإن كان الله قد عاتب نبيه وحبيبه على الإعراض للحظات عن رجل أعمى، فكيف بمن يحقرون أممهم ويزعمون بعدها أنهم قادة مشفقون عليها من عللها ومخلصون في أعمالهم للرقاء بها. إن كان الله قد أنعم علينا بالمال أو الجاه أو السلطان أو العلم، فيجب أن لا ننسى أننا جزء من نسيج أمتنا، بحياتها نحيا ومن كل خلية وعضو فيها نستمد طاقتنا وقوتنا.

القائد العظيم يشعر كل من حوله أن قضيته قضيتهم، وهمه همهم، ليس لأنه قرأ ذلك في كتاب أو تعلمه من أستاذ بل لأنه يؤمن به إيمانا عميقا متأصلا في نفسه، فيعامل الآخرين سواء، مهما كانت منازلهم في نفسه، وعندها يشعر كل من حوله أنهم ليسو مجرد آداة في آلة لا يفهمون فيها شيئا، بل أنهم جزء لا يتجزأ من جسد حي إذا إشتكى منه عضو تداعى له سائر الجسد بالسهر والحمى...

ولكي يكون الطرح أكثر واقعية، فأحب أن أختم بهذه النقاط التي لن أتبعك (أو يتبعك غيري) بدونها...
لكي أتبعك:
* عاملني بإحترام
* علمني ما لا أعلم
* أقل عثرتي وتجاوز عن هفواتي وأخطائي
* أصغ إلي عندما أتحدث وتكلم معي
* قل لي الحقيقة، وعاملني كند، ولتكن أفعالك إنعكاسا لكلماتك
* كن كريما بوقتك، وخبرتك، ومعارفك، عندما أحتاج أيا منها عندها فقط أتبعك بعقلي ونفسي وأضحي من أجلك ومن أجل ما تدعوا إليه.

Wednesday, December 26, 2007

OS/Architecture notes...

1- What is a segmentation fault, and how does it occur?


First, a word about memory protection. Modern operating systems provide the means to control access to the memory system. A user process should not be allowed to modify its read-only text section. It should not be allowed to read or modify any of the code and data structures in the kernel. It should not be allowed to read or write the private memory of other processes. And it should not be allowed to modify any virtual pages that are shared with other processes unless all parties explicitly allow it (via calls to explicit interprocess communication system calls). Each process usually has its own private virtual memory space, which translates to disjoint or safely shareable physical memory pages.

Remember that in virtual memory systems, we have a Page Table, which contains Page Table Entries (PTEs). A page table entry contains the base address of a physical page in DRAM, or an allocated page on the desk.

Enough of Virtual Memory. The operating system adds three permission bits to PTE entries, called SUP (supervisor), Read, Write bits, and they correspond to root, read, and write privliges respectively. If during the address translation the calling process violates any of those priviliges, it'll generate a protection fault (i.e., segmentation fault)....

Take this example

int main()
{
char* p;
char c;

p = (char*) 0;
c = *p; //generates seg fault
--(1)

}

The line at (1) generates a seg fault because it is trying to read location zero, and when the OS checks, it can't find a page allocated at location 0, and generates a segmentation fault.

p.s. This was a really crude descrition, so don't rely on it completely.... Should allow you to google it better, at the very least.

2- How do you detect if an overflow happened in adding two unsigned integers?

If the result is less than any of the operands, an overflow has occured in the case of unsigned addition. If both x and y less than 2^w (where w is the number of bits, we have no overflow). But s = x + y ==> s >= x (or y). So if s is less than x, or s is less than y, then we have an overflow. This is because in case of an overflow, s = x + y - 2^w (wraps around). THis is because we had a carry on of one, which we can't store, so we decrement the sum by 2^w. And since y < 2^w, y - 2^w < 0, which means s < x, hence overflow occured.

3- I get a stack overflow error at run time, but I can't spot any large stack allocations?

C++ allows you to delcare variables anywhere in the code (almost), unlike C which only allows you to delcare variables at the beginning of a block of instructions. So check large array allocations in the middle of your function/block. For example:


void myFunction(int
iTinyFourByteInteger)
{
int tinyInt1;
int tinyInt2;

int tinyInt3;
....
....
....
//after tens of lines of code
int largeArray[65536];

}

What the compiler did was to inspect your code first, push all local variables on the stack, regardless of where they were declared, then start the executing the first instruction in the function. If you're stepping through a debugger, you won't be able to spot the problem because it'll crash at the opening brace.

Sunday, December 23, 2007

Networking Interview Questions Cont.

These are some of the commonly asked, and not very well answered, questions:

What are the main differences between TCP/UDP?

  • UDP has no connection establishment. TCP has three way handshaking (send SYN, receive SYN-ACK, send ACK), while UDP has no connection establishment, hence called connectionless transport protocol
  • TCP keeps Connection state at the end systems, UDP doesn't. The This connection state includes receive and send buffers, congestion control parameters, and sequence and acknowledgment number parameters.
  • TCP has a 20-byte segment header overhead, while UDP has 8 segment header overhead.
  • TCP has a regulated send-rate. TCP has a congestion control mechanism that throttles the sender when one or more links between sender and receiver becomes excessively congested. This throttling can have a severe impact on real-time applications, which can tolerate some packet loss but require a minimum send rate. On the other hand, the speed at which UDP sends data is only constrained by the rate at which the application generates data, the capabilities of the source (CPU, clock rate, etc.) and the access bandwidth to the Internet.

2- What is Connected UDP?

Usually when creating a UDP socket we say:

socket = socket(PF_INET, SOCK_DGRAM, 0); //socket(family, type, proto)

Then to send using this socket we use:

ssize_t sendto(sock_fd,
void* buf, size_t nBytes,
int flags,
const struct sockaddr* to,
socklen_t len); //ssize_t is how much data the OS accepted to send.

If we use a udp socket in a call to connect, we create what is called connected UDP. This simply tells the OS the address of the peer; no handshaking is necessary and no data is sent to establish that the peer exist (i.e., no data is sent as a result of calling connect() on a udp socket. Why use it? If we communicating with a single peer, this might be useful since:

  1. We can call sendto() with null destination address
  2. We can use write() and send()
  3. We can use read() and recv(), which will return only data from the remote peer we connect()ed to.