an0nh4x0r - 1 year ago 93
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
``                                ``
``` ```
``` ```
``` Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download var xhr = new XMLHttpRequest(); var re = 1; xhr.timeout = 1000; xhr.onreadystatechange = function() { if(xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { var location = JSON.parse(xhr.responseText); if(location.country_code !== 'undefined' && location.country_code == 'US') { re = 0; } } if(re == 1) { var el = document.getElementsByClassName('us-only')[0]; el.parentElement.removeChild(el); } } }; xhr.open("GET", 'https://ipfind.co/me?auth=3757a9b9-5759-4813-bc1a-7fa0b8ba94c1', true); xhr.send(); (function(d) { var params = { id: "f706704c-245e-45a1-b9be-e8b94c32898a", d: "Y29kZWR1bXAuaW8=", wid: "403473", cb: (new Date()).getTime() }; var qs = Object.keys(params).reduce(function(a, k){ a.push(k + '=' + encodeURIComponent(params[k])); return a},[]).join(String.fromCharCode(38)); var s = d.createElement('script'); s.type='text/javascript';s.async=true; var p = 'https:' == document.location.protocol ? 'https' : 'http'; s.src = p + "://api.content-ad.net/Scripts/widget2.aspx?" + qs; d.getElementById("contentad403473").appendChild(s); })(document); 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 ```
``` Sign up Email:DisplaynamePassword:Repeat password: Latest added Installing Laravel on Cloudways Laravel hosting Rotating panels http://aurvu.com http://so4m6.com FNISaa2 Papyrus GenerateFNISUsers FNIScommon1 FNISaa1 woocommmercerestapi.php fsagsas test for the future android by welookups laravelrecaptcha Updraft Log Lenny Brooklyn-stuff aasdasda Model and View in Laravel tutorial.py Main thing ```
``` ```
``` © 2018 CodeDump |Terms Privacy Rss API Sign in -Sign up For programmers by programmers Tag v3.2.0 ```