an0nh4x0r - 2 months ago 4x
C++ Question

# Pointers, sizeof() and address in C++

This is the program

``````int main() {
cout << sizeof(int) << endl;        // for int its 4 in g++ compiler

int *p;
int a = 5;
p = &a;
cout << "The value of p is: " << p << endl;
cout << "The value of p + integer is: " << p + 0 << endl;

// lets take the size of individual 1, 2, 3
cout << "The sizeof(0) is: " << sizeof(0) << endl;   // 4
cout << "The sizeof(1) is: " << sizeof(1) << endl;   // 4
cout << "The sizeof(2) is: " << sizeof(2) << endl;   // 4

cout << "The value of p + 0 is: " << p + 0 << endl;
cout << "The value of p + 1 is: " << p + 1 << endl;
cout << "The value of p + 2 is: " << p + 2 << endl;

return 0;
}
``````

The
`sizeof()`
function in C++ gives
`sizeof(int)`
4 bytes, in
`g++`
compiler. So I printed the
`sizeof(1)`
,
`sizeof(2)`
,
`sizeof(0)`
to terminal and I got 4 bytes.

So I tried some pointer arithmetic in the program in above link. I added 1 to a pointer variable. Let's say
`int *p; int a = 10;`
. Now I assigned
`p = &a;`
. Now when I printed
`p`
it gives
`0x24fe04`
and when I printed
`p + 0`
it's the same. But when I tried adding
`p + 1`
and
`p + 2`
it gives different output like this:
`0x24fe08`
,
`0x24fe0c`
`p+1`
,
`p+2`
is not equal as in address it's contributing the same 4 bytes.

When you are saying `p + 1`, meaning that `p + 1 * sizeof(int)`. As I've mentioned here, the formula is
``                                ``
``` <!--//<![CDATA[ document.MAX_ct0 =''; var m3_u = (location.protocol=='https:'?'https://cas.criteo.com/delivery/ajs.php?':'http://cas.criteo.com/delivery/ajs.php?'); var m3_r = Math.floor(Math.random()*99999999999); document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u); document.write ("zoneid=408991");document.write("&amp;nodis=1"); document.write ('&amp;cb=' + m3_r); if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used); document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : '')); document.write ("&amp;loc=" + escape(window.location).substring(0,1600)); if (document.context) document.write ("&context=" + escape(document.context)); if ((typeof(document.MAX_ct0) != 'undefined') && (document.MAX_ct0.substring(0,4) == 'http')) { document.write ("&amp;ct0=" + escape(document.MAX_ct0)); } if (document.mmm_fo) document.write ("&amp;mmm_fo=1"); document.write ("&amp;publisherurl=" + escape(location.hostname)); document.write ("'></scr"+"ipt>"); //]]>--> ```
``` ```
``` Source (Stackoverflow) http://stackoverflow.com/questions/39055671/pointers-sizeof-and-address-in-c Criteo.DisplayAd( {'zoneid' : 475569, 'containerid' : 'crt-475569', 'height' : 250, 'layout' : {"version":"1.0","configuration":{"rowCount":1,"columnCount":3,"border":{"top":1,"left":1,"bottom":1,"right":1},"borderColor":"#ffffff","rowSeparatorsHeight":1,"columnSeparatorsWidth":0,"spacing":15,"padding":{"top":10,"left":10,"bottom":10,"right":10},"productSection":{"imagePosition":"Top","imageSize":50,"imageMargin":{"top":5,"left":5,"bottom":5,"right":5},"textSection":{"title":{"fontSize":14,"alignment":"Center","fontFamily":["Arial","Verdana"],"normal":{"color":"#000000","bold":true,"italic":false,"underlined":false},"onHover":{"color":"#000000","bold":true,"italic":false,"underlined":false},"margin":{"top":1,"left":1,"bottom":1,"right":1}},"description":{"fontSize":12,"alignment":"Left","fontFamily":["Arial"],"normal":{"color":"#000000","bold":false,"italic":false,"underlined":false},"onHover":{"color":"#000000","bold":false,"italic":false,"underlined":false},"margin":{"top":1,"left":1,"bottom":1,"right":1}},"price":{"fontSize":14,"alignment":"Center","fontFamily":["Arial"],"normal":{"color":"#000000","bold":false,"italic":false,"underlined":false},"onHover":{"color":"#000000","bold":false,"italic":false,"underlined":false},"margin":{"top":1,"left":1,"bottom":1,"right":1}},"rrp":{"fontSize":14,"alignment":"Right","fontFamily":["Arial"],"normal":{"color":"#000000","bold":false,"italic":true,"underlined":false},"onHover":{"color":"#000000","bold":false,"italic":true,"underlined":false},"margin":{"top":1,"left":1,"bottom":1,"right":1}},"cta":{"fontSize":15,"alignment":"Center","fontFamily":["Verdana"],"normal":{"color":"#000000","bold":false,"italic":false,"underlined":false},"onHover":{"color":"#000000","bold":true,"italic":false,"underlined":false},"margin":{"top":1,"left":1,"bottom":1,"right":1}}},"backgroundColor":"#FFFFFF","padding":{"top":10,"left":10,"bottom":10,"right":10}},"runningElement":{"runningType":"Header","runningBorder":{"top":0,"left":0,"bottom":1,"right":0},"height":14,"backgroundColor":"#FFFFFF","content":{"fontSize":10,"alignment":"Left","fontFamily":["Verdana"],"normal":{"color":"#000000","bold":false,"italic":false,"underlined":false},"onHover":{"color":"#000000","bold":false,"italic":false,"underlined":false},"margin":{"top":1,"left":0,"bottom":1,"right":1}},"staticText":"Ads by"}}}, 'passbackCode' : function(){ document.getElementById('crt-475569').style.display = "none"; } }); Comments /* * * CONFIGURATION VARIABLES * * */ var disqus_shortname = 'codedumpio'; /* * * DON'T EDIT BELOW THIS LINE * * */ (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); Please enable JavaScript to view the comments powered by Disqus. Email codedump link for Pointers, sizeof() and address in C++ Email has been send. To emailaddress: To name: From name: Extra information in the email body (optional): Email: I am sending you the codedump of Pointers, sizeof() and address in C++ that you can see here: https://codedump.io/share/TvdvwKROgwO1/1 ```
``` (adsbygoogle = window.adsbygoogle || []).push({}); Share <!--//<![CDATA[ document.MAX_ct0 =''; var m3_u = (location.protocol=='https:'?'https://cas.criteo.com/delivery/ajs.php?':'http://cas.criteo.com/delivery/ajs.php?'); var m3_r = Math.floor(Math.random()*99999999999); document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u); document.write ("zoneid=408072");document.write("&amp;nodis=1"); document.write ('&amp;cb=' + m3_r); if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used); document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : '')); document.write ("&amp;loc=" + escape(window.location).substring(0,1600)); if (document.context) document.write ("&context=" + escape(document.context)); if ((typeof(document.MAX_ct0) != 'undefined') && (document.MAX_ct0.substring(0,4) == 'http')) { document.write ("&amp;ct0=" + escape(document.MAX_ct0)); } if (document.mmm_fo) document.write ("&amp;mmm_fo=1"); document.write ("&amp;publisherurl=" + escape(location.hostname)); document.write ("'></scr"+"ipt>"); //]]>--> Sign up Email: Displayname Password: Repeat password: <!--//<![CDATA[ document.MAX_ct0 =''; var m3_u = (location.protocol=='https:'?'https://cas.criteo.com/delivery/ajs.php?':'http://cas.criteo.com/delivery/ajs.php?'); var m3_r = Math.floor(Math.random()*99999999999); document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u); document.write ("zoneid=407234");document.write("&amp;nodis=1"); document.write ('&amp;cb=' + m3_r); if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used); document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : '')); document.write ("&amp;loc=" + escape(window.location).substring(0,1600)); if (document.context) document.write ("&context=" + escape(document.context)); if ((typeof(document.MAX_ct0) != 'undefined') && (document.MAX_ct0.substring(0,4) == 'http')) { document.write ("&amp;ct0=" + escape(document.MAX_ct0)); } if (document.mmm_fo) document.write ("&amp;mmm_fo=1"); document.write ("&amp;publisherurl=" + escape(location.hostname)); document.write ("'></scr"+"ipt>"); //]]>--> Tags c++ pointers sizeof pointer-arithmetic (adsbygoogle = window.adsbygoogle || []).push({}); Latest added AASDASD Array Linked List Memory Pool FOSUserBundle command to add user to ROLE_ADMIN CategoricalPredictor <!--//<![CDATA[ document.MAX_ct0 =''; var m3_u = (location.protocol=='https:'?'https://cas.criteo.com/delivery/ajs.php?':'http://cas.criteo.com/delivery/ajs.php?'); var m3_r = Math.floor(Math.random()*99999999999); document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u); document.write ("zoneid=407773");document.write("&amp;nodis=1"); document.write ('&amp;cb=' + m3_r); if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used); document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : '')); document.write ("&amp;loc=" + escape(window.location).substring(0,1600)); if (document.context) document.write ("&context=" + escape(document.context)); if ((typeof(document.MAX_ct0) != 'undefined') && (document.MAX_ct0.substring(0,4) == 'http')) { document.write ("&amp;ct0=" + escape(document.MAX_ct0)); } if (document.mmm_fo) document.write ("&amp;mmm_fo=1"); document.write ("&amp;publisherurl=" + escape(location.hostname)); document.write ("'></scr"+"ipt>"); //]]>--> (adsbygoogle = window.adsbygoogle || []).push({}); Criteo.DisplayAcceptableAdIfAdblocked( {'zoneid' : 475203, 'containerid' : 'crt-475203', 'collapseContainerIfNotAdblocked' : false, 'callIfNotAdblocked' : function(){ Criteo.DisplayAd( {'zoneid' : 407771, 'containerid' : 'crt-475203'}); } }); ```
``` ```
``` © 2016 CodeDump |Terms Privacy Rss API Sign in -Sign up For programmers by programmers Tag v2.2.4 \$('#language').select2(); \$("#select2-language-container").addClass("form-control"); ```