Joey Sides Joey Sides - 1 year ago 170
C++ Question

Visual Studio Variable Type Asterisk

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 *

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download