tag:blogger.com,1999:blog-16544436737535374702023-06-20T21:38:27.765-07:00c++, as seen by meIts all about my personal learning in C++Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-1654443673753537470.post-69981274206180730462012-04-23T11:52:00.002-07:002012-04-23T11:52:28.562-07:00size zero ..<div dir="ltr" style="text-align: left;" trbidi="on">
*Type - <i>Finding and observation</i><br />
<br />
sizeof operator, is a common way to know about the types in C/C++. Ever thought of seeing something which is making sizeof return 0. Yes, I just discovered a type which returns 0 for its variables.<br />
<br />
What did this code give in your machine ..<br />
<span style="font-family: 'Courier New', Courier, monospace;">char ch[0];</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">printf("%d",sizeof(ch));</span><br />
<br />
doesnt matter much though ;)<br />
<br />
It printed 0 in mine- gcc/ Ubuntu 11.04/ 32 bit machine ..</div>Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-65737807962092900192011-11-29T01:46:00.001-08:002011-11-29T01:53:47.877-08:00"..never believe that "const" is some guarantee that the memory under the pointer doesn't change.." - linus torvaldus<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>I think a lot of this is bogging down in a couple of different areas;
people are arguing past each other over:
- What does const mean?
- What could const mean?
- What should const mean?
Many people will produce different answers for these three.
In fact, the answer to the first, in terms of the Standard, is
`almost nothing at all'. For instance:
const int c = 3;
...
printf("%d\n", c);
could print 42, if c somehow changes despite its being declared
const. No strictly conforming program can change `c' directly,
but the object is usually subject to change through external factors
(e.g., direct writes on systems that lack memory protection, or
memory corruption from alpha particles, or whatnot). All the
Standard gets you is these two things:
- An object that is defined with `const' may not be modified
in any way by a strictly conforming program (so since `c'
above is const, it may not be modified; if it *is* modified,
the behavior is undefined).
- An lvalue with the `const'-qualifier may not be assigned-to.
This mainly buys you some compile-time typo/braino checking,
e.g., a diagnostic if you write `c = 7' instead of `v = 7'
(note that c and v are next to each other on many keyboards).
The answer to the second (`what could const mean') is quite open
ended: there are plenty of possible meanings.
The answer to the third, `what should const mean', depends on the
person asked.
Note also that the following strictly conformant program *must* print
"3, 7":
#include <stdio.h>
int v;
int *p;
void f(const int *);
int main(void) {
p = &v;
f(&v);
return 0;
}
void f(const int *vp) {
int i, j;
i = *vp;
*p = 7;
j = *vp;
printf("%d, %d\n", i, j);
}
The compiler *cannot* assume that i and j are equal, despite the
fact that *vp is const-qualified, because vp can (and does) point
to a modifiable lvalue and the assignment to *p can (and does)
modify the lvalue to which the const-qualified `vp' pointer points.
As this example illustrates, `const' does *not* mean `constant'.</stdio.h></pre>
<br />
reference : http://yarchive.net/comp/const.html [is place where i copied], it also contains the similar and more advance discussions including mail written by torvaldus throwing some insights..</div>Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-6758885824748741392011-10-25T02:29:00.000-07:002011-10-25T02:44:08.421-07:00Demystifying complex C declarations.<div dir="ltr" style="text-align: left;" trbidi="on">
I have seen many times people complaining of interviewer giving them shit looking declaration and ask us to tell them what it is..<br />
Ex. :<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">int * p :</span><br />
pointer to an integer :<br />
[:D]..<br />
okay.. how about this..<br />
<pre></pre>
<pre> <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">void (*signal(int, void (*fp)(int)))(int); </span></pre>
<pre></pre>
<pre><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">
</span></pre>
<pre><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">To some even this may look easy but to me I put down my pencil and got back to facebook to relieve from tension.</span></pre>
<pre><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Following is the illustration of one simple method I stumbled across and felt it worked for any kind of declaration.</span></pre>
<pre><span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre><span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;">If there is/are any exception(s) to the following, please feel free to discuss.</span></pre>
<pre><span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre><span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;">
</span></pre>
<pre><span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;">[This was posted to comp.lang.c by its author, David Anderson, on 1994-05-06.].. and I read on c-facts ;)<h1>
The ``Clockwise/Spiral Rule''</h1>
<h2>
By David Anderson</h2>
There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to parse in their head any C declaration!
There are three simple steps to follow:
<ol>
<li>Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements:
<dl>
<dt>[X] or []</dt>
<dd>=> Array X size of... or Array undefined size of...</dd>
<dt>(type1, type2)</dt>
<dd>=> function passing type1 and type2 returning...</dd>
<dt>*</dt>
<dd>=> pointer(s) to...</dd></dl>
</li>
<li>Keep doing this in a spiral/clockwise direction until all tokens have been covered.</li>
<li>Always resolve anything in parenthesis first!</li>
</ol>
<h3>
Example #1: Simple declaration</h3>
<pre> +-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+
</pre>
Question we ask ourselves: What is str?<blockquote>
``str is an...</blockquote>
<ul>
<li>We move in a spiral clockwise direction starting with `str' and the first character we see is a `[' so, that means we have an array, so...<blockquote>
``str is an array 10 of...</blockquote>
</li>
<li>Continue in a spiral clockwise direction, and the next thing we encounter is the `*' so, that means we have pointers, so...<blockquote>
``str is an array 10 of pointers to...</blockquote>
</li>
<li>Continue in a spiral direction and we see the end of the line (the `;'), so keep going and we get to the type `char', so...<blockquote>
``str is an array 10 of pointers to char''</blockquote>
</li>
<li>We have now ``visited'' every token; therefore we are done!</li>
</ul>
<h3>
Example #2: Pointer to Function declaration</h3>
<pre> +--------------------+
| +---+ |
| |+-+| |
| |^ || |
char *(*fp)( int, float *);
^ ^ ^ || |
| | +--+| |
| +-----+ |
+------------------------+
</pre>
Question we ask ourselves: What is fp?<blockquote>
``fp is a...</blockquote>
<ul>
<li>Moving in a spiral clockwise direction, the first thing we see is a `)'; therefore, fp is inside parenthesis, so we continue the spiral inside the parenthesis and the next character seen is the `*', so...<blockquote>
``fp is a pointer to...</blockquote>
</li>
<li>We are now out of the parenthesis and continuing in a spiral clockwise direction, we see the `('; therefore, we have a function, so...<blockquote>
``fp is a pointer to a function passing an int and a pointer to float returning...</blockquote>
</li>
<li>Continuing in a spiral fashion, we then see the `*' character, so...<blockquote>
``fp is a pointer to a function passing an int and a pointer to float returning a pointer to...</blockquote>
</li>
<li>Continuing in a spiral fashion we see the `;', but we haven't visited all tokens, so we continue and finally get to the type `char', so...<blockquote>
``fp is a pointer to a function passing an int and a pointer to float returning a pointer to a char''</blockquote>
</li>
</ul>
<h3>
Example #3: The ``Ultimate''</h3>
<pre> +-----------------------------+
| +---+ |
| +---+ |+-+| |
| ^ | |^ || |
void (*signal(int, void (*fp)(int)))(int);
^ ^ | ^ ^ || |
| +------+ | +--+| |
| +--------+ |
+----------------------------------+
</pre>
Question we ask ourselves: What is `signal'?
Notice that signal is <em>inside</em> parenthesis, so we must resolve this first!
<ul>
<li>Moving in a <em>clockwise</em> direction we see `(' so we have...<blockquote>
``signal is a function passing an int and a...</blockquote>
</li>
<li>Hmmm, we can use this same rule on `fp', so... What is fp? fp is also inside parenthesis so continuing we see an `*', so...<blockquote>
fp is a pointer to...</blockquote>
</li>
<li>Continue in a spiral clockwise direction and we get to `(', so...<blockquote>
``fp is a pointer to a function passing int returning...''</blockquote>
</li>
<li>Now we continue out of the function parenthesis and we see void, so...<blockquote>
``fp is a pointer to a function passing int returning nothing (void)''</blockquote>
</li>
<li>We have finished with fp so let's catch up with `signal', we now have...<blockquote>
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning...</blockquote>
</li>
<li>We are still inside parenthesis so the next character seen is a `*', so...<blockquote>
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to...</blockquote>
</li>
<li>We have now resolved the items within parenthesis, so continuing clockwise, we then see another `(', so...<blockquote>
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to a function passing an int returning...</blockquote>
</li>
<li><em>Finally</em> we continue and the only thing left is the word `void', so the final complete definition for signal is:<blockquote>
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to a function passing an int returning nothing (void)''</blockquote>
</li>
</ul>
The same rule is applied for const and volatile. For Example:
<pre> const char *chptr;
</pre>
<ul>
<li>Now, what is chptr??<blockquote>
``chptr is a pointer to a char constant''</blockquote>
</li>
</ul>
How about this one:
<pre> char * const chptr;
</pre>
<ul>
<li>Now, what is chptr??<blockquote>
``chptr is a constant pointer to char''</blockquote>
</li>
</ul>
Finally:
<pre> volatile char * const chptr;
</pre>
<ul>
<li>Now, what is chptr??<blockquote>
``chptr is a constant pointer to a char volatile.''</blockquote>
</li>
</ul>
Practice this rule with the examples found in K&R II on page 122.
<hr />
Copyright © 1993,1994 David AndersonThis article may be freely distributed as long as the author's name and this notice are retained.
<div>
</div>
</span></pre>
<br />
<br /></div>Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-3678794525196024802011-10-21T06:55:00.000-07:002011-10-21T07:27:58.983-07:00Open Question : Reason for restriction in conversion of pointers to reference of pointer to const<div dir="ltr" style="text-align: left;" trbidi="on">
OS : Win XP<br />
Env: VC++ 2008<br />
<br />
<br />
void foo_ptr(const char * & ptr) //reference to a pointer to a constant character<br />
{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return;<br />
}<br />
<br />
<br />
//allowed<br />
void foo_char(const char & p_Char) //reference to a constant character<br />
{<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return;<br />
}<br />
int main()<br />
{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>char ch = 'd';<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>char *ptr = "anuh";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
foo_char(ch);<br />
<b><br /></b><br />
<b>!<span class="Apple-tab-span" style="white-space: pre;"> </span>//foo_ptr(ptr); //NOT ALLOWED syntax error, vc++, 2008</b><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return 0;<br />
}<br />
<br />
I am interested in knowing the reason of syntax error when we call foo_ptr. When foo_char is allowed why not foo_ptr.<br />
<br />
just to keep updated the same question has been asked <a href="http://stackoverflow.com/questions/7850843/c-need-reason-for-error-cannot-convert-parameter-1-from-char-to-const">http://stackoverflow.com/questions/7850843/c-need-reason-for-error-cannot-convert-parameter-1-from-char-to-const</a><br />
<br />
<br /></div>Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com1tag:blogger.com,1999:blog-1654443673753537470.post-52552146853645820802011-10-06T23:06:00.000-07:002011-10-06T23:20:54.023-07:00Why size(Structure) differ from sum of actual sizes of individual members<div dir="ltr" style="text-align: left;" trbidi="on">
Same Data Members : Different Sizes<br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">struct MyStructA {</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> char a;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> char b;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> int c;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">};</span><br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">struct MyStructB {</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> char a;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> int c;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> char b;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">};</span><br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">int main(void) {</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> int sizeA = sizeof(struct MyStructA);</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> int sizeB = sizeof(struct MyStructB);</span><br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> printf("A = %d\n", sizeA);//->8</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> printf("B = %d\n", sizeB);//->12 in arch x86, DevCPP Compiler</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> getch();</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> return 0;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">}</span><br />
<div>
(via : <a href="http://bytes.com/topic/c/answers/543879-what-structure-padding">forum</a>)</div>
<br />
<br />
(from Wiki)<br />
<span class="Apple-style-span" style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"></span><br />
<div class="mw-geshi" dir="ltr" style="background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; color: black; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em; text-align: left;">
<div class="c source-c" style="font-family: monospace; line-height: normal;">
<pre class="de1" style="background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; border-bottom-color: white; border-bottom-style: none; border-bottom-width: 0px; border-left-color: white; border-left-style: none; border-left-width: 0px; border-right-color: white; border-right-style: none; border-right-width: 0px; border-top-color: white; border-top-style: none; border-top-width: 0px; color: black; font-family: monospace, 'Courier New' !important; font: normal normal normal 1em/1.2em monospace; line-height: normal; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: top;"><span class="kw4" style="color: #993333;">struct</span> student<span class="br0" style="color: #009900;">{</span>
<span class="kw4" style="color: #993333;">char</span> grade<span class="sy0" style="color: #339933;">;</span> <span class="coMULTI" style="color: grey; font-style: italic;">/* char is 1 byte long */</span>
<span class="kw4" style="color: #993333;">int</span> age<span class="sy0" style="color: #339933;">;</span> <span class="coMULTI" style="color: grey; font-style: italic;">/* int is 4 bytes long */</span>
<span class="br0" style="color: #009900;">}</span><span class="sy0" style="color: #339933;">;</span>
<span class="kw3" style="color: #000066;">printf</span><span class="br0" style="color: #009900;">(</span><span class="st0" style="color: red;">"%zu"</span><span class="sy0" style="color: #339933;">,</span> <span class="kw4" style="color: #993333;">sizeof</span> <span class="br0" style="color: #009900;">(</span><span class="kw4" style="color: #993333;">struct</span> student<span class="br0" style="color: #009900;">)</span><span class="br0" style="color: #009900;">)</span><span class="sy0" style="color: #339933;">;</span>
</pre>
</div>
</div>
<div style="line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;">
The reason for this is that most compilers, by default, align complex data-structures to a <a class="mw-redirect" href="http://en.wikipedia.org/wiki/Word_(data_type)" style="background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: #0645ad; text-decoration: none;" title="Word (data type)">word</a> alignment boundary. In addition, the individual members are also aligned to their respective alignment boundaries. By this logic, the structure <code style="background-color: #f9f9f9; font-family: monospace, 'Courier New';">student</code> gets aligned on a word boundary and the variable <code style="background-color: #f9f9f9; font-family: monospace, 'Courier New';">age</code> within the structure is aligned with the next word address. This is accomplished by way of the compiler inserting "padding" space <i>between two members or to the end </i>of the structure to satisfy alignment requirements. This padding is inserted to align <code style="background-color: #f9f9f9; font-family: monospace, 'Courier New';">age</code> with a word boundary. (<u>Most <a href="http://en.wikipedia.org/wiki/Central_processing_unit" style="background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #0645ad;" title="Central processing unit">processors</a> can fetch an <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" style="background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #0645ad;" title="Data structure alignment">aligned word</a> faster than they can fetch a word value that straddles multiple words in memory, and some don't support the operation at all<sup class="reference" id="cite_ref-2" style="font-style: normal; line-height: 1em;"><a href="http://en.wikipedia.org/wiki/Sizeof#cite_note-2" style="background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #0645ad; white-space: nowrap;">[3]</a></sup>).</u></div>
<div style="line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;">
<u></u></div>
<div style="line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;">
<u><br /></u></div>
<div style="line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;">
This also has roots into the bus size, (which is word size), making the size of structure integral multiple of bus size helps processor fetching the data faster.</div>
<div style="line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;">
How ?? : > i l update as soon as i get crude answer.</div>
</div>
Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-45257618501445142342011-10-05T07:51:00.000-07:002011-10-05T07:51:24.850-07:00C/C++ : free or delete will NOT release memory back to OS<div dir="ltr" style="text-align: left;" trbidi="on">
following is what i found really useful for knowledge sake and would also help in case we are planning to build large systems using C/C++. I have read about this on post by <span class="Apple-style-span" style="background-color: #fdfdfd; color: #777777; font-family: Arial; font-size: 11px;"><a class="jiveTT-hover-user jive-username-link" data-avatarid="10816" data-externalid="" data-username="mmallina" href="https://engage.alcatel-lucent.com/people/mmallina" id="jive-900912895731581482507" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #666666; font-size: 11px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;">Murali MALLINA</a>, Sr. Soft E in Alcatel Lucent on one of the internal forums.</span><br />
<div>
<span class="Apple-style-span" style="background-color: #fdfdfd; color: #777777; font-family: Arial; font-size: 11px;"><br /></span></div>
<div>
<span class="Apple-style-span" style="background-color: #fdfdfd; color: #777777; font-family: Arial; font-size: 11px;">he said as </span></div>
<span class="Apple-style-span" style="background-color: #fdfdfd; font-family: Arial; font-size: 12px;"></span><br />
<div id="_mcePaste" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span class="Apple-style-span" style="background-color: #fdfdfd; font-family: Arial; font-size: 12px;">When you call free() or delete(), it will NOT really release any memory back to OS. Instead, that memory is kept with the same process until it is terminated. However, this memory can be reused for any future allocations by the same process. This freed memory is never released to OS, even when the system is out of memory and other processes need memory. That means, a process is stuck with the largest level of memory allocation throughout its life time.</span></div>
<span class="Apple-style-span" style="background-color: #fdfdfd; font-family: Arial; font-size: 12px;">
<div style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 1em; height: 8pt; list-style-image: initial; list-style-position: initial; list-style-type: none; min-height: 8pt; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div id="_mcePaste" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
</div>
<div id="_mcePaste" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
This is big concern for most processes that are expected to run (literally) forever without a restart.</div>
<div style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
</div>
<div style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 1em; height: 8pt; list-style-image: initial; list-style-position: initial; list-style-type: none; min-height: 8pt; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
</div>
<div id="_mcePaste" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
</div>
<div id="_mcePaste" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
There is an alternative, by linking to mapmalloc library, that uses map/unmap instead of sbrk for memory allocation. When linked with this library, memory could be really released to operating system when free/delete is called. However, memory allocation could be potentially 5 times slower. One heck of a trade off!</div>
<div style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
</div>
<div style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 1em; height: 8pt; list-style-image: initial; list-style-position: initial; list-style-type: none; min-height: 8pt; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<br /></div>
<div id="_mcePaste" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-collapse: collapse; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 12px; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
</div>
</span><br />
<div>
<span class="Apple-style-span" style="background-color: #fdfdfd; font-family: Arial; font-size: 12px;">Hope this information will help in your designs.</span><span class="Apple-style-span" style="background-color: #fdfdfd; color: #777777; font-family: Arial; font-size: 11px;"> </span> </div>
<div>
<br /></div>
<div>
:-o :-o</div>
</div>
Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-39213772785982847342011-09-30T02:30:00.000-07:002011-09-30T02:32:39.878-07:00trees and data structure : skipping double pointer<div dir="ltr" style="text-align: left;" trbidi="on">
many times it is felt to pass head pointers as parameter to functions...<br />
<br />
v simple solution is to pass by reference..<br />
this way u dnt have to change internal syntaxes in side your insert functions and all other functions where modification in the data structure is required.<br />
<br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue;">void </span>insert ( <u>struct T * & node</u>, <span class="Apple-style-span" style="color: blue;">int </span>data)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-style-span" style="color: blue;">if </span>(!node)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{<span class="Apple-tab-span" style="white-space: pre;"> </span>node = (struct T * )malloc(<span class="Apple-style-span" style="color: blue;">sizeof</span>(struct T));</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>node->data = data;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>node->left = node->right = <span class="Apple-style-span" style="color: blue;">NULL</span>;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">}</span></div>
Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-50260924340950863152011-09-19T07:22:00.000-07:002011-09-19T07:38:16.092-07:00Sample Code : Constant functions and temporary objects | Return Optimization<div dir="ltr" style="text-align: left;" trbidi="on">
1. Constant funtions with temporary objects :<br />
it tells that temporary objects are by default constant in nature, that their values cannot be changed. and so only constant functions can be called using temporary objects.<br />
<br />
<br />
2. bruce eckel says about return value optimization : that returning by values just by making an explicit constructor call.<br />
<br />
This doesnt create a local object (if i were use Integer a; return a;) and so<br />
no call of copy constructor at the time of return object<br />
<br />
its a direct creation of temp object in the return address and compiler knows that u r creating this object only for return purpose and nothing else.<br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<span class="Apple-style-span" style="font-family: inherit;"><i>Ref : Sections : </i></span><br />
<span class="Apple-style-span" style="font-family: inherit;"><i>1. Return by value as constants, Pg 533</i></span><br />
<span class="Apple-style-span" style="font-family: inherit;"><i>2. The return optimization, pg 534</i></span><br />
<span class="Apple-style-span" style="font-family: inherit;"><i>Chp 12 Operator Overloading, : <span class="Apple-style-span" style="color: #222222; font-size: 13px; line-height: 18px;"><a href="http://www.lib.ru.ac.th/download/e-books/TIC2Vone.pdf" style="color: #5dc2c0; text-decoration: underline;">Thinking in C++, second edition, volume 1</a></span></i></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">#<span class="Apple-style-span" style="color: blue;">include</span><iostream></iostream></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue;">using</span> <span class="Apple-style-span" style="color: blue;">namespace</span> std;</span><br />
<span class="Apple-style-span" style="background-color: #cccccc; font-family: 'Courier New', Courier, monospace;">#<span class="Apple-style-span" style="color: blue;">define</span> <b>about</b> \</span><br />
<span class="Apple-style-span" style="background-color: #cccccc; font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>"this program demonstrates \</span><br />
<span class="Apple-style-span" style="background-color: #cccccc; font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>\n<span class="Apple-tab-span" style="white-space: pre;"> </span>1. that only constant functions should be and can be called from temporary objects \</span><br />
<span class="Apple-style-span" style="background-color: #cccccc; font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>\n<span class="Apple-tab-span" style="white-space: pre;"> </span>2. return value optimization : while returning object by value using operator+, \</span><br />
<span class="Apple-style-span" style="background-color: #cccccc; font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>\n<span class="Apple-tab-span" style="white-space: pre;"> </span>this way doesnt call any form of constructor"</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue;">class</span> Integer</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-style-span" style="color: blue;">int</span> i;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-style-span" style="color: blue;">public</span>:</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Integer(<span class="Apple-style-span" style="color: blue;">int</span> i):i(i){}</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>~Integer(){</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cout<<"\ndestructor called:"<<i<<endl;< font=""></i<<endl;<></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-style-span" style="color: blue;">const</span> Integer operator+(const Integer & right)</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>return Integer(i + right.i);</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-style-span" style="color: blue;">void</span> g()</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cout<<"gOnce called g-one"<<endl;< font=""></endl;<></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-style-span" style="color: blue;">int</span> getValue()</span><span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">const</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{<span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-style-span" style="color: blue;">return</span> i;</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> </span></span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">};</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">int</span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> main()</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">{</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Integer a(2), b(4);</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>// ! (a+b).g(); <b>//this line is a compile time error</b> because g() in a non const function</span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cout <<about<<endl;< font=""></about<<endl;<></span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>cout<<(a+b).getValue()<<endl;< font=""></endl;<></span></span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>return 0;</span></span></span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">}</span></span></span></span></span></div>
Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-29452795296874166762011-09-16T01:10:00.000-07:002011-09-16T01:10:49.428-07:00Confused Constants : C and C++<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Following code is erroneous in C++ and is works fine in C (with warning)<br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">int </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">*p;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue;">const</span> <span class="Apple-style-span" style="color: blue;">int</span> i =3;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">p = &i;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<span class="Apple-style-span" style="font-family: inherit;">trying to point out a non constant pointer to a constant variable. </span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
</div>
Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-73686721101012221922011-09-15T08:13:00.000-07:002011-09-15T09:00:28.882-07:00confused constants<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
my understanding says constant checking is done in symbol table level .. as<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue;">const int</span> i =90;</span><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">int</span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> *p= & i;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">*p = 0;</span><br />
is allowed, had it been on memory level, it would have said no.<br />
<br />
<br /></div>
<br />
<iframe allowfullscreen="" frameborder="0" height="315" src="http://www.youtube.com/embed/MOwNI2mtZ9Y" width="420"></iframe><br />
<br />
Question : Search in progress: what is the use of constant return type in fuctions<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: #0b5394;">const int</span> foo()</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: #0b5394;">return</span><span class="Apple-style-span" style="color: #073763;"> </span>3;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">}</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">Answer : page 366, Chapter 8,</span><span class="Apple-style-span" style="font-family: 'Courier New';"> <a href="http://www.lib.ru.ac.th/download/e-books/TIC2Vone.pdf">Thinking in C++, second edition, volume 1</a></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<br /></div>
Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0tag:blogger.com,1999:blog-1654443673753537470.post-26403524250907672572011-09-14T23:53:00.000-07:002011-09-14T23:53:04.893-07:00dynamic memory allocation without using heap<div dir="ltr" style="text-align: left;" trbidi="on">
Mind Mapping :<br />
Operator Overloading, Thinking in C++ Vol 1( Bruce Eckel) -> doubts in Constant functions -><br />
browser -> times of india -> early enterpeuner -><br />
<a href="http://www.sourcebits.com/">www.sourcebits.com</a> (MBBS, MD tunred into a techie, started as one man company and now a leading mobile app firms with one of its app went to top 3 in 3 days), Impressed by the graphic design, I gave a pause, world is progressing in a accelerated way and i m shitting here on Operator Overloading.<br />
<br />
Before turning page in Ops Overloading, I first googled to see if c++ still in the market compared the new up coming technologies. There I found c++ recently updated it self this month Sept, 2011.
first look <a href="http://en.wikipedia.org/wiki/C%2B%2B11">link</a>
Under division <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Generalized_constant_expressions"> constant expression</a>, I figured out introduction of new keyword <span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">constexpr.</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="color: blue;"><br /></span></span></span><br />
<span class="Apple-style-span" style="font-family: inherit;">Wiki started as</span><br />
<span class="Apple-style-span" style="font-family: monospace, 'Courier New'; font-size: 13px; line-height: 15px; white-space: pre;"><span class="kw4" style="color: blue;">int</span> get_five<span class="br0" style="color: green;">(</span><span class="br0" style="color: green;">)</span> <span class="br0" style="color: green;">{</span><span class="kw1" style="color: blue;">return</span> <span class="nu0" style="color: #0000dd;">5</span><span class="sy4" style="color: teal;">;</span><span class="br0" style="color: green;">}</span></span><br />
<span class="Apple-style-span" style="font-family: monospace, 'Courier New'; font-size: 13px;"><pre class="de1" style="background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; border-bottom-color: white; border-bottom-style: none; border-bottom-width: 0px; border-left-color: white; border-left-style: none; border-left-width: 0px; border-right-color: white; border-right-style: none; border-right-width: 0px; border-top-color: white; border-top-style: none; border-top-width: 0px; font-family: monospace, 'Courier New' !important; font: normal normal normal 1em/1.2em monospace; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: top;">
<span class="kw4" style="color: blue;">int</span> some_value<span class="br0" style="color: green;">[</span>get_five<span class="br0" style="color: green;">(</span><span class="br0" style="color: green;">)</span> <span class="sy2" style="color: #000040;">+</span> <span class="nu0" style="color: #0000dd;">7</span><span class="br0" style="color: green;">]</span><span class="sy4" style="color: teal;">;</span> <span class="co1" style="color: #666666;">//create an array of 12 integers. Illegal C++</span></pre>
</span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="color: blue;"><br /></span></span></span><br />
<span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="font-family: inherit;">and c++ version 11 has a solution</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="color: blue;"><br /></span></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="color: blue;">
constexpr </span></span><span class="Apple-style-span" style="color: blue;">int</span> get_five() {<span class="Apple-style-span" style="color: blue;">return</span> 5;}</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br /></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><span class="Apple-style-span" style="color: blue;">int</span> some_value[get_five() + 7];</span><br />
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span class="Apple-style-span" style="font-family: inherit;">As per my understanding after spending 15 minutes to this section I realized similar things can be done using const keyword. Which in turn allowed me to do dynamic memory allocation without using malloc.</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue;">const int</span> getSize(<span class="Apple-style-span" style="color: blue;">int</span> i)</span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">{<span class="Apple-style-span" style="color: blue;">return</span> i;}</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">int </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">main(<span class="Apple-style-span" style="color: blue;">int</span> argc, <span class="Apple-style-span" style="color: blue;">char</span> ** argv)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<div style="text-align: left;">
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="color: blue;">int</span> a[ 1 + getSize( atoi(argv[1]) ) ];</span></div>
<div style="text-align: left;">
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">cout<<sizeof(a);< font=""></sizeof(a);<></span></div>
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">return 0;</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">}</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: inherit;">I was amazed.</span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br /></span></span></div>
Anubhavhttp://www.blogger.com/profile/08889246372872013644noreply@blogger.com0