-2

Using Visual Studio, I am running into some trouble with an int type variable and a float type variable. They are both stored in their own arrays. When I go to print them out they come out as memory location gibberish. When I debug I noticed that the correct value is displayed next to the memory gibberish in the watch area. I also noticed that under type, the variable types have an * (asterisk) next to them. Could anybody offer information as to why this would happen? Thanks in advance.

Watch area looks like this...

Name Value Type

score 0x002ff5c8 {96.0000000} float *

studentID 0x002ff698 {9317} int *

Joey Sides
  • 67
  • 2
  • 8

2 Answers2

1

I recommend reading into the tutorial above and perhaps an introductory book depending on how interested you are in pursuing learning c++. The type is a pointer type to int and float data. Here is a small example that answers the question (how to print out these values):

float* a =  new float(5.8);

the pointer is established, this points to a memory location where a float with the value 5.8 is stored.

std::cout << *a;

The asterisk before a is called dereferencing a pointer, this is how the data is accessed, you may want to check to make sure you have a valid pointer or your program can crash.

delete a;

delete memory allocated when it will no longer be used(EDIT 2), this will free the space given to store a (failing to do so causes a memory leak)

EDIT 1: Consider that the pointer may point to a contiguous array of floats or int (which is much more likely than just one), then you will have to know the size of the array you are reading to access the elements. In this case, you will use the operator [] to access the members, let's say we have an array b,

float* b = new float[2] {0.0,1.0};

to print it's members you would have to access each element

std::cout << b[0] << ' ' << b[1];

the delete operator looks like this for arrays

delete[] b;

EDIT 2: Whenever you use new to dynamically allocate memory, think about the scope of the variable, when the scope is over delete the pointer. User is correct, you do not want to delete a pointer which may be used later, nor is it necessary to call delete to pointers obtained from references.

0

First off you are using the debugger. This is awesome. The sheer number of SO questions that could be solved in five minutes with a debugger is staggering. You are already far, far ahead of the game than a lot of the time-wasting sad sacks who can't be bothered to use the expletive deleted tools that came with the compiler.

Second, some important reading because it explains part of what is going on: What is array decaying?

Now to break down what the debugger is showing you

score 0x002ff5c8 {96.0000000} float *

score: Obviously the variable's name

float *: score is a variable of type float *, a pointer to a float.

0x002ff5c8: This is the data value of score. Pointers are a reference to a location in memory. Rather than being data, they point to data. So a pointer is a variable that contains where to find, the address of, another variable. 002ff5c8 is the hexadecimal location in memory where you will find what score points to.

{96.0000000}: score points to a floating point value that has been set to 96 (possibly plus or minus some fuzziness because not all numbers can be exactly represented with floating point)

So the crazy number 0x002ff5c8 tells the program where to find score's data, and this data happens to be 96.

Note the debugger only shows you the first value in the array of data that is at score, which brings us back to array decaying. Odds are good that the program has knowledge of how much data is pointed at by score. Could be one float. Could be a million. You have to carry the length of a block of an array around with it once the array has decayed.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54