Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F122233404
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
Wed, Jul 16, 20:08
Size
810 B
Mime Type
text/x-c
Expires
Fri, Jul 18, 20:08 (2 d)
Engine
blob
Format
Raw Data
Handle
27454311
Attached To
R9360 PCSC_git
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