Jump to content
  • Advertisement
Sign in to follow this  
Niddles

Messing with memory

This topic is 4570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 objectmanager
GAMEOBJECT *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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!