Memory and Pointers

Memory and Pointers - 2 Get memory address from another...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Memory and Pointers A pointer is a variable that contains an address in memory. In a 32 bit architectures, the size of a pointer is 4 bytes independent on the type of the pointer. 0 (4GB-1) 232-1 Address space p:20 : 12 Char c = ‘A’; //ascii 65 char * p = &c; c:12 : 65
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Ways to get a pointer value 1. Assign a numerical value into a pointer Char * p = (char *) 0x1800; *p = 5; // Store a 5 in location 0x1800; Note: Assigning a numerical value to a pointer isn't recommended and only left to programmers of OS, kernels, or device drivers
Image of page 2
Ways to get a pointer value
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2. Get memory address from another variable: int *p; int buff[ 30]; p = &buff[1]; *p =78; buff[0]:100 : buff[1]:104 : buff[29]:216 : 220 : P: 96: 104 78 Ways to get a pointer value 3. Allocate memory from the heap int *p p = new int; int *q; q = (int*)malloc(sizeof(int)) Ways to get a pointer value • You can pass a pointer as a parameter to a function if the function will modify the content of the parameters void swap (int *a, int *b){ int temp; temp=*a; *a=*b; *b=temp; } In main: swap(&x, &y)...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern