Reputation: 5056
So, I never learnt C++ pointers at University so now im doing it on my own as a hobby. My idea as a learning exercise was to start writing a random number ( 666 ) to a series of memory locations. The thing is some stuff is confusing me ( I started two days ago ). For instance :
I know these are all probably dumb newbie question but please bear with me on this one. I'm doing my best effort to learn this on my own.
#include <iostream>
using namespace std;
int main() {
int limit =0;
int* x = (int*) malloc(sizeof(int) * 1);
*x = 10;
cout << "----------------------------------------------------" << endl;
cout << "Incrementing memory address : " << endl;
for(int j=0 ; j < 999 ; j++) {
limit++;
*x=666;
x++;
}
x-= limit;
for(int j=0 ; j < ( limit ) ; j++) {
cout << x << " " << *x << endl;
x++;
}
x-= limit;
free(x);
}
----------------------------------------------------
Incrementing memory address :
0x55e11d8cae70 666
0x55e11d8cae74 666
0x55e11d8cae78 666
0x55e11d8cae7c 666
0x55e11d8cae80 666
0x55e11d8cae84 666
0x55e11d8cae88 666
0x55e11d8cae8c 666
0x55e11d8cae90 892696624
0x55e11d8cae94 1680945509
0x55e11d8cae98 1700881208
0x55e11d8cae9c 824206137
0x55e11d8caea0 808596530
0x55e11d8caea4 808662326
0x55e11d8caea8 2570
0x55e11d8caeac 666
0x55e11d8caeb0 666
0x55e11d8caeb4 666
0x55e11d8caeb8 666
0x55e11d8caebc 666
0x55e11d8caec0 666
0x55e11d8caec4 666
Thanks in advance!
Note - I know the code is not doing anything useful, I'm just playing with C++.
Upvotes: 2
Views: 126
Reputation: 30926
for(int j=0 ; j < 999 ; j++) {
limit++;
*x=666;
x++; <---- yikes
}
Here you are incrementing it to point to some memory that you didn't allocate. Invoking undefined behavior when you in next itertion dereference it and write something to it.
Anything after *x=666
is in the realm of undefined behavior.
To answer your questions, I would say malloc
does what it is supposed to do. It allocates memory - the amount you request. C
doesn't provide any warning or exception on accessing out of bound memory.
You asked why do use malloc
? You don't have to. You can simply do this using c++
std::vector<int> v;
for(int j=0 ; j < 999 ; j++) {
limit++;
v.push_back(666);
/* other work */
}
Third questions answer you already know - you have incremented the pointer value and that is being increased and printed. When you tried to access it - well that's where thing went wrong for this code.
Back to C
:
Yes you can do that - you can allocate as much as you need. And as you allocated now it's your turn to access it carefully. Your use of malloc
for a C
compiler is a bit off. If I have to write a C code out of it - I wil write like this
int* x = malloc(sizeof(int) * 1000);
if( x == NULL ){
perror("Malloc failed");
exit(1);
}
for(size i=0; i<1000; i++){
x[i] = 666;
}
When you allocated for 1000
ints - yes it will work fine. And instead of incrementing x
you can simply do it like this. No harm following the simple approach.
If you wanted to initialized the 1000
elements with 666
, you can do this in C++
std::vector<int> v(1000,666);
Upvotes: 4