memory-address

Passing an address to a class and from there to its “child-class” by reference in C++

偶尔善良 提交于 2020-01-06 07:31:47
问题 "pointer" holds the address of "Int". I want to pass that address to my given classes by reference: class N { public: N(int &pPointer){ std::cout << "Address: " << &(pPointer) <<" \n"; } }; class M { public: M(int &pPointer):n(pPointer) { std::cout << "Address: " << &pPointer <<" \n"; } private: N n; }; int main () { int Int = 5; int *pointer = &Int; std::cout << "Address: " << pointer <<" \n"; M m(*pointer); return 0; } Is this a good practice (since I'm kind of using a reference to a

Passing an address to a class and from there to its “child-class” by reference in C++

纵饮孤独 提交于 2020-01-06 07:31:26
问题 "pointer" holds the address of "Int". I want to pass that address to my given classes by reference: class N { public: N(int &pPointer){ std::cout << "Address: " << &(pPointer) <<" \n"; } }; class M { public: M(int &pPointer):n(pPointer) { std::cout << "Address: " << &pPointer <<" \n"; } private: N n; }; int main () { int Int = 5; int *pointer = &Int; std::cout << "Address: " << pointer <<" \n"; M m(*pointer); return 0; } Is this a good practice (since I'm kind of using a reference to a

C: Why do pointer and &pointer have different values?

走远了吗. 提交于 2020-01-06 01:51:13
问题 If I run the following on OS X: int main (void) { int* n; // initialise(declare) pointer *n = 20; // the value in address pointed to by n is 20 printf("n: %i, n&: %i\n", n, &n); return 0; } I get: n: 1592302 512 , n&: 1592302 480 Why the differing values? 回答1: Why do pointer and &pointer have different values? The expression &n yields the address of n itself, while n evaluates to the value of the pointer, i.e. the address of the thing it points to. But note that you have undefined behaviour

Randomization of environment variable addresses

一世执手 提交于 2020-01-04 10:41:52
问题 I am using bash. I have switched off ASLR in Ubuntu 11.04 using #sysctl -w kernel.randomize_va_space=0 And I have exported a variable from the shell using $ export MYSHELL=/bin/sh I wrote a C program to get the address of the MYSHELL : void main(){ char* shell = getenv("MYSHELL"); if (shell) printf("0x%x\n", (unsigned int)shell); } It spat out 0xbffffe82 . When I used it as a part of my attack for ret-to-libc, the address changes (although by a very small offset). Why does this happen? Also

Is a pointer variable also assigned a memory address?

狂风中的少年 提交于 2020-01-04 03:01:28
问题 In C++, on the stack, a simple variable is assigned a memory address so that we can use a pointer to contain this memory to point to it; then is a pointer also assigned a memory address? If yes, can we have a pointer of pointers? 回答1: Yes, you are right. We can have pointers to pointers: int a; int b; int * pa = &a; int ** ppa = &pa; // set a to 10 **ppa = 10; // set pa so it points to b. and then set b to 11. *ppa = &b; **ppa = 11; Read it from right to left: ppa is a pointer to a pointer to

memory address literal

夙愿已清 提交于 2020-01-03 13:09:35
问题 Given a literal memory address in hexadecimal format, how can I create a pointer in C that addresses this memory location? Memory addresses on my platform (IBM iSeries) are 128bits. C type long long is also 128bits. Imagine I have a memory address to a string (char array) that is: C622D0129B0129F0 I assume the correct C syntax to directly address this memory location: const char* const p = (const char* const)0xC622D0129B0129F0ULL I use ULL suffix indicate unsigned long long literal. Whether

What is the difference between pointer to array and pointer to pointer?

℡╲_俬逩灬. 提交于 2020-01-02 14:31:53
问题 I'm new in programming and learning about pointers in array. I'm a bit confused right now. Have a look at the program below: #include <stdio.h> int fun(); int main() { int num[3][3]={23,32,478,55,0,56,25,13, 80}; printf("%d\n",*(*(num+0)+1)); fun(num); printf("%d\n", *(*(num+0)+1)); *(*(num+0)+0)=23; printf("%d\n",*(*(num+0))); return 0; } int fun(*p) // Compilation error { *(p+0)=0; return 0; } This was the program written in my teacher's notes. Here in the main() function, in the printf()

What is the difference between pointer to array and pointer to pointer?

一个人想着一个人 提交于 2020-01-02 14:30:11
问题 I'm new in programming and learning about pointers in array. I'm a bit confused right now. Have a look at the program below: #include <stdio.h> int fun(); int main() { int num[3][3]={23,32,478,55,0,56,25,13, 80}; printf("%d\n",*(*(num+0)+1)); fun(num); printf("%d\n", *(*(num+0)+1)); *(*(num+0)+0)=23; printf("%d\n",*(*(num+0))); return 0; } int fun(*p) // Compilation error { *(p+0)=0; return 0; } This was the program written in my teacher's notes. Here in the main() function, in the printf()

C: Does the address operator (&) produce a pointer (address + type) or just an address?

喜你入骨 提交于 2020-01-02 10:22:24
问题 Most of what I've read about the address operator, & , says it's used to get just that - an address. I recently heard it described differently, though, as producing a full-fledged pointer. Given the following C code, int i1 = 5; int *p1; p = &i1; my understanding is that p1 references the int 5 by storing the address where i1 's data is stored and remembering that the data in that location is to be interpreted as an int (which dictates how many bytes to read and how to interpret the read data

print the memory location of a variable (or pointer)

☆樱花仙子☆ 提交于 2020-01-02 10:04:57
问题 I want to print where a variable is stored. I Google it and I found this: int *p; printf("memory location of ptr: %p\n", (void *)p); If I write this, is it right? printf("memory location of ptr: %p\n", &p); I compiled it and I didn't get any errors or warnings. However, the above two commands didn’t return the same value! 回答1: Lets say you have these declarations: int i; int *p = &i; It would look something like this in memory: +---+ +---+ | p | --> | i | +---+ +---+ If you then use &p you