For example,
I wanna watch pA
int *pA = new int[LEN];
Then in visual studio, I can see only pA[0] in watch-window
I try to view '(int (*)[LEN])pA', but it does not work
I don't want to make
int (*test)[LEN] = (int (*)[LEN])pA
to debug
How can I see dynamic allocated pointer as a static array in debugger of VS2010?
You can write pA,10 (or how many elements you want to see) in the watch window.
In the Quick Watch:
First know the value of LEN. Say, it is 100.
Then, type pA,100 to see all the 100 elements.
Related
I have searched all over and have not found anything on this.
I have the following simple code:
#include<iostream>
using namespace std;
int main() {
int *newArray;
int size;
cout << "enter size: ";
cin >> size;
newArray = new int[size];
cin.get();
return 0;
For some reason, regardless of the number entered, the dynamic array always has only one element allocated. I am not getting any compiler errors or run time errors (other than the array issue). I have also tried
int *newArray;
newArray = new int[100];
and
const int SIZE = 100;
int *newArray;
newArray = new int[SIZE];
I get the same result.
I am using Visual Studio Community 2015 for my IDE if that makes a difference.
If anyone has any pointers (no pun intended) please let me know. Thank you.
newArray is not an array, it's just a pointer to some place in memory. It doesn't contain a number of elements, it's just an address in memory. When you do newArray = new int[n], sizeof(int)*n bytes of memory are allocated and the address of this allocated space is saved in newArray.
Visual Studio only knows that newArray is a pointer to an int. So it just shows the one number at that address. It cannot know how much memory has been allocated there.
If you want to see more than the first int, open the memory view, type newArray as the address, set up it to show ints. You will see plain memory interpreted as integers, so you will see numbers from your array. But there will be no end of these ints. After your numbers, there will be garbage.
Another option to try (I'm not sure if it works though) is to add (int[100])newArray to the Watch window.
So memory is allocated, feel free to write and read it and don't forget to delete[] it.
I'm trying to allocate a new array if integers (See HwGrades allocation below)
When I put the HwNum=2, the new function creates an array of size 1 only!
and when the for loop iterates 2 times it doesnt give me access violation
Help would be appreciated..
Here's the constructor
EE_Course::EE_Course(int Course_ID, char * Course_Name, int Hw_Num, double Hw_Weigh,int Factor_)
{
CourseID = Course_ID;
CourseName = new char[strlen(Course_Name) + 1];
strcpy(CourseName, Course_Name);
HwNum = Hw_Num;
HwWeigh = Hw_Weigh;
HwGrades = new int [HwNum]; // STARTING FROM HERE
for (int i = 0; i < Hw_Num; i++) { //UNTIL HERE
HwGrades[i] = 0;
}
Factor_ = 0;
ExamGrade = 0;
}
And those are the Course class private variables :
protected:
int CourseID;
int HwNum;
char* CourseName;
double HwWeigh;
int ExamGrade;
int* HwGrades;
};
The debugger does not show the whole array if it is a pointer. It shows the address of the array and the first element the array is pointing. So there is nothing wrong with your code.
You could see it if it was defined as an array:
int HwGrades[100];
If you really want to use a pointer and see it's content, you have two choices:
Define it as an array, debug it, fix/verify your code and turn back to pointer.
I don't know what is you environment, but usually there is a memory view option. You can check what's in the array any time you want. Just open the memory view of your IDE and watch the address of your pointer.
EDIT:
Apparently there is a third(and the best) option. See Rabbi Shuki's answer.
The debugger just shows one element. Here's why:
The type of HwGrades is int*. So when showing the contents of HwGrades what should the debugger do? The debugger does not know, that the pointer is actually pointing to the first element of an array. It assumes it just points to an int. Therefore, the debugger shows just the first element of the array that is actually of size 2.
If you're using the Visual Studio debugger, you can write HwGrades,2 in the watch window to see the first two elements of the array. Replace 2 by whatever your tickles your fancy. ;)
However, generally I would strongly advice to use the STL container std::vector for dynamic arrays. It will be easier to program and the debugger will be your friend without the hassle.
If you want to see the next cells of the array in the watch screen you can put the name and add a comma and the number of cells you want to see.
I.E.
HwGrades, 2
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing two-dimensional array via pointer
int table[20][20];
int** table1 = table;
int a;
table[0][0] = 4;
a = table1[0][0];
the last line gives me Access violation, and i dont get it ..
thanks in advance!
The short answer is that a pointer to a pointer is not the same as an array of arrays.
You can't make a int ** from a two dimensional int array in one assignment. First, you need to allocate the correct number of pointers:
table1 = malloc(sizeof(int*) * 20);
Then you can use a loop to fill in your next level pointers:
for(int i = 0; i < 20; i++)
table1[i] = table[i];
The reason is that pointer to pointer is ONE memory location that holds an address. When you make the first allocation, that makes that pointer point to 20 int pointers. We then assign each of those pointers the first address of each row in the original table. Now, when we use a = table1[0][0], the compiler will go fetch table1[0] - so the first pointer in the array we set up in the loop. This points to table[0] row, so we fetch the [0] element from that, and get the 4 that was stuffed in there [of course, any other number index would also get four, so it's hard to show that it works right in this case).
In the table[y][x] case, the compiler will take the address of table, add y * 20 * sizeof(int) to it, and then add x * sizeof(int). Which gives us a nice place in the "square" lump of memory that "table" is.
(Again, typing too much, two more answers since I started writing this)
C compilers are just too forgiving:
int** table1 = table;
c.c: In function ‘main’:
c.c:3:17: warning: initialization from incompatible pointer type [enabled by default]
If we feed it incorrect code, the compiler complains, and we ignore the complaint and then get some strange outcome, should we wonder?
Anyway, table1 doesn't point to a pointer to int, and the way it's going to crash is a matter of implementation (alignment, segmentation etc.)
Although table1[1] is of type pointer, there are no pointers anywhere in table.
Ask yourself: Given a pointer, and being told to access item 3, 4 - how should the compiler know how large the first dimension of your two-dimensional array is, in order to skip the right amount of items?
Correct: Not at all.
Given
int table[20][20];
table is compatible with int*, not int**.
This should compile and be able to access table1 from table1[0] to table1[399]:
int* table1 = table;
Declare:
LPWSTR** lines= new LPWSTR*[totalLines];
then i set using:
lines[totalLines]=&totalText;
SetWindowText(totalChat,(LPWSTR)lines[totalLines]);
totalLines++;
Now I know totalText is right, cause if i SetWindowText using totalText it works fine. I need the text in totalLines too.
I'm also doing:
//accolating more memory.
int orgSize=size;
LPWSTR** tempArray;
if (totalLines == size) {
size *= 2;
tempArray = new LPWSTR*[size];
memcpy(tempArray, lines,sizeof(LPWSTR)*orgSize);
delete [] lines;
lines = tempArray;
}
to allocate more memory when needed.
My problem is that the lines is not getting the right data. It works for the first time around then it get corrupted. I thought at first i was overwriting but totalLines is increase. Hopefully this is enough information.
LPWSTR is already a pointer, so you're creating a 2D array of pointers - is that what you wanted? I think not, because this:
SetWindowText(totalChat,(LPWSTR)lines[totalLines]);
Casts LPWSTR* to LPWSTR. Isn't your compiler complaining?
These two statements:
LPWSTR** lines= new LPWSTR*[totalLines];
lines[totalLines]=&totalText;
invoke undefined behavior. The problem is that the maximum index of an array totalLines long is totalLines-1.
If you'd post what exactly you were trying to accomplish we might be able to help better. For example, it seems this problem could be much better solved with a std::vector<std::vector<wchar_t> > or std::vector<std::basic_string<wchar_t> > rather than an explicitly allocated array of LPWSTRs.
Thanks to Ben and Eli I have my answer. It should be LPWSTR* lines= new LPWSTR[size]; since LPWSTR is already a pointer. Thanks guys.
How do I 'dive' an int * which points to a dynamically allocated array of integers and represent it as a fixed int[] array? Put otherwise, if I dive an int * it shows the address and the int pointed to, but instead I would like to see the array of all of the integers.
I noticed the TotalView tag on this question. Are you asking how to see the values in your array in totalview? If so then the answer is pretty easy.
Lets say you have a pointer p which is of type int * and you have it currently pointing towards an array with 10 integers.
Step 1. Dive on the pointer. That's accomplished by double clicking, clicking the middle mouse button, or using the dive option on the context menu -- all after having placed the mouse cursor on the variable int he source code pane or the stack frame pane.
This will bring up a new window that will say
Expression: p
Address: 0xbfaa1234
Type: int *
and down in the data area will say something like
0x08059199 -> 0x000001a5 (412)
This window is showing you the pointer itself, the address listed is the address of the pointer. The value (0x08059199 in the example above) is the actual value that the pointer has. Everything to the right of the arrow is just a "hint" telling you want it points to.
Step 2. Dive on the pointer again. Repeat the double click or middle mouse button, this time on the data value in the variable window. (So you are double clicking where it says 0x08059199).
This will effectively "dereference" the pointer. Now the window is focused not on pointer itself but the thing that the pointer pointed to. Notice that the address box now contains 0x08059199 which was the value before.
expression: *(((int *) p))
Address: 0x08059199
Type: int
and down in the data area it will say something like
0x000001a5 (412)
Step 3. Cast the data window to the type you want. Just click in the type field and change it to say int[10]. Then hit return.
This tells the debugger that 0x08059199 is the beginning of an array of 10 integers.
The window will grow two new fields: Slice and Filter. You can leave those alone for now, but they can be useful later.
The data area will now show two columns "field" and "value" and 10 rows.
The field column will be the index in the array [0] - [9] and the value column will tell you what data you have in each array location.
Other tips:
In more complicated data structures you can may want to dive on individual elements (which might also be pointers, diving will dereference them as well)
You can always cast to different types or lengths to look at data "as if it was" whatever
You can edit the actual data values by clicking on the value column and editing what you find there. This is useful when you want to provoke specific mis-behavior from your application
You can always undo diving operations with the "<" icon in the upper right hand corner of the variable window.
There are some online videos that you might find helpful at
http://www.roguewave.com/products/totalview/resources/videos.aspx
in particular there is one labeled "getting started with TotalView".
Don't hesitate to contact us at Rogue Wave Software for TotalView usage tips!
support at roguewave dot com is a good address for that.
Chris Gottbrath
(Chris dot Gottbrath at roguewave dot com)
TotalView Product Manager at Rogue Wave Software
It's not very hard, but i forgot how it exactly works. I found you a page which explains it tho ;). I think to point and array with ints called for example test you should get it using &test.
Just check this page out:
http://www.cplusplus.com/doc/tutorial/pointers/
You can't meaningfully do this without knowing exactly how many ints are in the array.
If you have an int *p that points to the first element in a contiguous int data, either dynamically allocated, or a static array, you can index it as if it were an array:
int *data = malloc(3 * sizeof *data);
int *p;
/* malloc error detection omitted for brevity */
data[0] = 1;
data[1] = 2;
data[3] = 42;
p = data;
assert(p[0] == 1);
assert(p[1] == 2);
assert(p[2] == 42);
You have to know the size of the valid data you are accessing this way.
So, let's say I have data as above, and want to write a function to print it. It won't do to declare the function like this:
void print_array(int *data);
because when you call print_array(data);, the function doesn't know the number of elements to print.
You could define your print_array() like:
void print_array(int *data, size_t n);
where n denotes the number of valid elements pointed to by data, that the caller has to supply. Or you could decide that every "array" will end with a sentinel value, a value that is used only at the end of the data, and is not useful value otherwise:
data[2] = 0; /* data[0] and data[1] are useful, valid values
and data[2] is 0 to signify the end of the data */
Then you can declare your print_array() as:
void print_array(int *data);
and keep indexing into data in the function definition till you hit a sentinel:
void print_array(int *data)
{
size_t i;
for (i=0; data[i] != 0; ++i)
printf("%d\n", data[i]);
}
So, to answer your question, you can already treat a pointer to a valid dynamically allocated data as an array. You have to remember the size of the allocated data of course, which you would need to do in a regular array too.