Messing with memory

Started by
20 comments, last by Zahlman 18 years, 4 months ago
Hey everyone. I'm just sitting here writing programs messing around with pointers, trying to get to know them better, and I decided to use the bit shift operator(>> or <<). I want to know why it's doing what it's doing. Here's my program.

#include <iostream>
using namespace std;
int main() {
  int a = 10;
  int* b = &a;
  cout<<a<<endl<<b<<endl;
  a>>=1;
  cout<<a<<endl<<b<<endl;
  return 0;
}

outputs: 10 0x22ff74 5 0x22ff74 and if I bitshift to the left instead of a 5, it gives 20. Shouldn't it change the memory allocation, not the variable?
Advertisement
You are only bitshifting the value variable, so only the value is affected. You aren't touching the pointer. That's why the address outputted is still the same.

I'm not sure if the compiler would let you actually use the bitshift operator on the pointer.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Hmmm... Okay. The only reason I'm doing this is because I don't understand the point of pointers. I have been programming in C++ forever and a half.. on and off, and all this time I haven't been able to find a good use for them.
Like Endurion said you are only moving the bits in variable a.

Here are the bits in variable 'a' before the shift

... 01010

When added together they equal 10. (8+2=10)

Here they are after the shift.

... 00101

When added together they equal 5. (4+1=5)

Hope that explains bit shifting.
Most of our obstacles would melt away if, instead of cowering before them, we should make up our minds to walk boldly through them.- Orison Swett Marden
The only line where you are bitshifting:
  a>>=1;


Is being done directly on an integer variable, so it will shift the value.

I have no clue why anyone would want to shift a pointer, unless it was for some vodoo division or something.

Pointers are used "a lot" with graphics, and when dealing with classes or large amounts of data.

If you are learning c++, learning about pointers are a must.
as i said, I've been learning C++ for 4 years now..(off and on) Back then I got discouraged because I couldn't understand it the slightest. I understand a lot more now, just can't find a use for pointers. I know how they work, I just can't find a use for them.
Programming what? Have you ever not known how big an array needs to be? Guess you'd use a vector. Have you used virtual functions? Because without pointers they are useless.

I guess it's understandable really, everyone does their best to make pointers unnecessary, favouring containers and references etc. And when people do use them, they often use smart pointers.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
programming nothing really, just learning. I have had many failed attempts at making games, and other random things. I tried to make pong, tic tac toe, a console RPG, a length unit conversion calculator, a calculator, tetris, a graphical RPG, a side scroller. Aaah, it's so discouraging talking about my failing attempts at stuff.
Quote:Original post by Niddles
as i said, I've been learning C++ for 4 years now..(off and on) Back then I got discouraged because I couldn't understand it the slightest. I understand a lot more now, just can't find a use for pointers. I know how they work, I just can't find a use for them.


Pointers are very powerful, especially in game developement because you can access a myriad of variables by a single common pointer name. For example...

class OBJECTMANAGER{private:GAMEOBJECT objectlist[50];public:GAMEOBJECT *GetObject(int id){    return &objeclist[id];}};


Then whenever you needed to access a certain game object you would...

//OBJECTMANAGER objectmanagerGAMEOBJECT *anobject = objectmanager.GetObject(10);


It is very useful for looping through a list also as you just call for a new pointer each iteration.

With pointers you can also call new to dynamically create variables at run time and delete them whenever.

I hope it makes sense now.
Most of our obstacles would melt away if, instead of cowering before them, we should make up our minds to walk boldly through them.- Orison Swett Marden
Okay so what this does is it creates an array of 50 objects, and a function pointer to return the address of one of those 50 objects. Ugh, that's confusing.

This topic is closed to new replies.

Advertisement