1.
Are memory adresses a certain size?
I've seen some that have 6 characters after the 0x
and some that have 8 after the 0x

2.
A pointer like this: int *pointern
does what exactly?
Selects a location in the memory? Randomly? Or maybe like the first availible memory adress?
If I do cout<<pointern I get:0x77159e34
and a cout<<*pointern gives: -7494423 Wth?


3.
When I do: pointern = new int [5]
the adresses are 0x5f0fb0, 0x5f0fb4 etc.
What happened to the 0x77159e34?

4.
I only have one variable, the int *pointern
If I do pointern = new int[5] and then give them the values
1, 2 , 3 ,4 ,5
And then cout the adress of pointern[0] - pointern[4] and their values i get
1, 2 ,3 ,4 ,5 as expected, but if I take it one step further and cout the next adress and value, i get a very high number.
What the hell is that from?
Is it another program or something?
Or.....?
The code:

int *pointern;
pointern = new int[5];

for(int i=0; i<5; i++)
{
        pointern[i] = i+1;
}

for(int i=0; i<6; i++)
{
        cout <<pointern <<"\t" <<*pointern <<endl;
        pointern++;
}


This will print the following on the screen:
0x3b0fb0 1 //pointern[0]
0x3b0fb4 2 //pointern[1]
0x3b0fb8 3 //pointern[2]
0x3b0fbc 4 //pointern[3]
0x3b0fc0 5 //pointern[4]
0x3b0fb4 1919381362 //the adress after pointern[4]

5.
Is it possible to get a value from a memory adress?
For example, if I have the memory adress 0x3b1cf2, can I find out the value located in that adress?

a. If it's a variable from my own program
b. If it's a adress that my program hasn't stored anything in


I'd really appreciate it if someone could answer these for me
Thanks ;/

I can't answer those questions directly because they sound too much like assignment questions and you shouldn't be given a freebie (it would help you to learn). However, I can correct some of your underlying misconceptions.

>>I've seen some that have 6 characters after the 0x and some that have 8 after the 0x
How the values are printed to the screen (when using cout) does not relate to how much memory is used to store them. For instance, on a 32bit system, normally, an integer (int) would take 4 bytes in memory, but, with 4 bytes, you can store an integer up to about +-2 billion (+-2^31). When printed to the screen, a large value would take much more that 4 characters, but it still requires only 4 bytes to store in memory (in binary, 4 bytes = 32 bits). So, whatever the number of characters that the address stored in a pointer takes to be printed out does really matter that much.

>>Selects a location in the memory? Randomly? Or maybe like the first availible memory adress?
A pointer is a variable like any other variable and it has a value (in case of pointer, that value represents an address in memory). If you create a variable like int i; without giving it an initial value, it will have the value that corresponds to whatever bits were in the memory location where that variable exists at the time it was created (i.e. we usually call that a "garbage value", or uninitialized variable). A pointer is no different than any other type, if created without being initialized with a value, the pointer will have a garbage value as the address that it stores. This address points nowhere and has whatever value, and it is very dangerous to try and set the value at that "random" address.

The other answers are corollaries of the above two remarks. I'll let you figure them out.

I can't answer those questions directly because they sound too much like assignment questions and you shouldn't be given a freebie (it would help you to learn).

Those are not asignment questions.
I have been trying out pointers and while playing around with it I have questions.

We only have excercises in a book, and they are like: Create a program that does this and that.
Not questions like if you can get a value from a memory location

Also, if it's binary code, how can it have negative values?
Would the binary code look like "-01010010 10101011 00101010 11011001" or..?

>>how can it have negative values?

Say you have 8 bits, thus, it can represent 2^8 different numbers (which is 256). Well, you can split the range in two and represent numbers from -128 to 127, inclusively. You could, for example, reserve one bit (at the start or end) that gives the sign of the number (i.e. 0 = plus, 1 = minus). That's one way that is typically used for floating-point numbers (but these numbers have a more complicated representation). Usually, however, negative integers are represented by taking the 2's complement of the bits, see this wiki. The computer architecture (the processor) has particular representations, in binary, of the numbers (integers and floating-point values) and the modules of the processor are made to use those particular representations and perform the required operations on the values.

ok.
But can you please answer the questions in my first post?
I swear it's not an assignment...

And even IF it were... How would I be supposed to learn if no one would help me out and explain?.......

It's just questions I've come up with while playing around with c++

1. Pointers are of a size to match the word size of the processor and operating system. A 32-bit OS will have 32-bit (4 byte) pointers, and a 64-bit OS will have 64-bit (8 byte) pointers. In the "old days" DEC 10 and 20 systems had 36 bit addresses some supercomputers (wavetracer, etc) had 128-bit addresses, and a lot of micro-computers had 8 or 16-bit addresses.. These days, it is either 32-bit or 64-bit for the most part.

2. Think of a pointer as in "points to content". So, an int* pointer will point to the location in memory of an integer.

3. In the first instance, since the pointer was not initialized with a valid value, it was pointing to random data. Meaningless, unless you try to access it, in which case your program will crash. In the second case, where it is initialized to an array, a pointer may point to a single value, or an array of values. In this case, the addresses are sane/valid and "point to" the actual data. This is a Caveat Programmer issues, since a pointer to data may be a single value, or an array of values. Make sure you know which!

4. When you assign an array to a pointer, it only is valid for members of the array. Anything beyond the end of the array is either pointing to something else, or is random data. Caveat Programmer! is still in force here.

Alright:

1.Are memory adresses a certain size?

This is implementation dependent, but normally a 32bit system would take 4 bytes (32 bits) to store a pointer (on a 64bit it is usually 8 bytes).

2. A pointer like this: int *pointern; does what exactly?

It declares a pointer named "pointern" which can store the address to an integer value, but since it is not initialized, it is not pointing to any valid or usable memory (the address is a garbage value). Reading or writing to the address stored in pointern (i.e. a garbage address) is undefined behaviour (meaning that in the best case it will just corrupt your memory, in a worse case it will crash your application, and in the worst case it will crash your operating system (very unlikely, but possible, I guess)).

3. When I do: pointern = new int[5]; . The adresses are 0x5f0fb0, 0x5f0fb4 etc. What happened to the 0x77159e34?

The 0x77159e34 was the garbage value previously stored in pointern, it could be any address and probably would be different (almost) every time you run the program. This previous address is meaningless, it does not point to valid memory, nothing happens to it after the above statement, and who cares?

However, if pointern did point to valid memory before, memory that was dynamically allocated (with new), then simply resetting the pointer to point somewhere else would mean that the memory at the previous address becomes unreachable and, more importantly, undeletable (you cannot call delete on it because you no longer have a pointer to that memory). This is called a memory leak.

4. .... I get a very high number. What the hell is that from? Is it another program or something?

It is just another piece of uninitialized memory, i.e. that big integer value is a garbage value. Do not ever read or write memory that is beyond the memory that you allocated, you never can tell what is there, it could be garbage or it could be memory from another variable of your program. If you do modify memory outside the range allowed by the amount of memory you allocated, you will have a memory corruption problem.

5. Is it possible to get a value from a memory adress? For example, if I have the memory adress 0x3b1cf2, can I find out the value located in that adress?

If you have an address, you can access the value stored at that address given that you know the type of the value (int, double, FooClass, whatever). You can do it via the dereferencing operator (either the simple dereferencing *pointern or the indexing pointern[0] ).

a. If it's a variable from my own program

That's really the only case in which you can do it. You can't just pick an address at random and read/write to that address, you will get a segmentation fault or access violation, or a crash, or even a system crash. The address you have must be the address of a variable of your program in order to be able to access the value at that address, anything else is undefined behaviour.

b. If it's a adress that my program hasn't stored anything in

Ditto. Additionally, I will remark that you have to separate in your mind the concept of "storing stuff in memory" with the concept of "allocating memory". In terms of accessing values from an address, it really doesn't matter whether you stored something or not there. What matter immensely is if you have allocated that memory. You must see allocation as a sort of reservation system. By allocating memory (with new ), you tell the program: "look, I need X amount of memory, please reserve that space for me and give me an address to it". Once you have that address, you are allowed to do whatever you like with that chunk of memory, but you are not allowed to access any memory that you didn't reserve because you don't know what other part of the program/system might be using that memory. Similarly, when you are finished with using that chunk of memory, you need to free it (which is done via delete ).

1.
You said:
That's really the only case in which you can do it. You can't just pick an address at random and read/write to that address, you will get a segmentation fault or access violation, or a crash, or even a system crash. The address you have must be the address of a variable of your program in order to be able to access the value at that address, anything else is undefined behaviour.

But I kind of did?
I allocated 0x3b0fb0 - 0x3b0fc0 and then did a cout on 0x3b0fc4 and got, what I assume, was one of the 'garbage value's.

2.
You said:
The 0x77159e34 was the garbage value previously stored in pointern, it could be any address and probably would be different (almost) every time you run the program.

What do you mean? The address was a garbage value? Or the high int stored in that address was a garbage value?

1. Until you write a sane value to an address, it is uninitialized, and contains random data.
2. Ditto.

You can allocate memory, it has an address, but until you store something there, it contains garbage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.