Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102892147
ch-04-ex-01-solution.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Feb 25, 06:34
Size
797 B
Mime Type
text/x-c
Expires
Thu, Feb 27, 06:34 (2 d)
Engine
blob
Format
Raw Data
Handle
24450550
Attached To
R1106 Programming Concept Rouaze
ch-04-ex-01-solution.cpp
View Options
/*
* chapter-04-exercise-01.cpp
*
* Dynamic memory 1
*
* Created on: Sep 24, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <iostream>
int main(int argc, char* argv[])
{
// We declare an integer variable ...
int i = 5;
// And a pointer to an integer which holds the address of the first int
int* p_j;
p_j = &i;
// Now we change the value of the integer through the pointer
std::cout << "Value of i before: " << i << std::endl;
// Next line is equivalent to *p_j = *p_j * 5;
*p_j *= 5;
std::cout << "Value of i after: " << i << std::endl;
// We dynamically allocate an integer now
int* p_k = new int;
*p_k = i;
std::cout << "Value stored at p_k: " << *p_k << std::endl;
// Cleanup is very important with dynamic memory
delete p_k;
return 0;
}
Event Timeline
Log In to Comment