I am using Qt on Ubuntu. When I debug I only see the very first value of the array in Locals and Watchers. How can I view all the array contents?
struct node
{
int *keys;
void **pointers;
int num_keys;
struct node *parent;
int is_leaf;
struct node *nextLevelNode;
};
It shows only the first key value in the debugging window.
In Expression evaluator,
Try (int[10])(*myArray) instead of (int[10])myArray
Or, *myArray#10 instead of myArray#10
It shows only the first key value,in the debugging window
I presume you're referring to the pointer keys, declared with int *keys;
The debugger doesn't know that this is an array: all it knows is that this is a pointer to an int. So it can't know how many values you want it to display.
What I've found, using the Qt Creator 2.1.0 debugger on Ubuntu, is that the following code allows me to see all 5 values:
int array1[5];
array1[0] = 2;
array1[1] = 4;
array1[2] = 6;
array1[3] = 8;
array1[4] = 10;
Whereas with this code, the debugger only shows the first value, exactly as you describe.
int* array2 = new int[5];
array2[0] = 20;
array2[1] = 21;
array2[2] = 22;
array2[3] = 23;
array2[4] = 24;
Aside: of course, the above code would be followed by this, to avoid leaking memory:
delete[] array2;
Later: This Qt Developer Network Forum Post says that you can tell the debugger to display a pointer as an array:
In Locals and Watchers, context menu of your pointer’s entry, select “Watch Expression”. This creates a new watched expression below.
There, double click on the entry in the “Names” column, and add “#10” to display 10 entries.
This sounds like it should get you going.
Just right-click on your variable, and choose Change Value Display Format and check Array of 100 items.
In Qt for mac what worked for me was:
Add an expression evaluator for the desired variable (right click the variable on the debugger window then "Add expression evaluator for "var name here""
The array variable appears initially as a single value. Just change "var" to "var[start...end] and the array values appear.
Two dimensional arrays sometimes cannot be displayed that way. There is a work-around. First, declare a two-dimensional array as a one-dimensional array like this:
int width = 3;
int height = 4;
int* array2D = new int [width*height];
int x,y;
for(x=width-1;x>-1;x--)
for(y=height-1;y>-1;y--)
array2D[x*height + y] = -1; // mark a breakpoint here!
// add to expression evaluator: (int[3][4]) *array2D
delete [] array2D;
Then add (int[3][4]) *array2D to the expression evaluator. Unfortunately you have to index the array your self, but you can write a special-purpose inline function or use another encapsulation method to make it slightly cleaner.
Related
So first all I'll preface this with: I just started using c++.
I have a structure that I store the pointer to in an unordered_map, setting members' values in the struct pointer as I get them through my process. Then I no longer need them in a map so I transfer then to a vector and loop through them.
Though on the second loop, it outputs my index (1) but the next statement of making a local pointer var for the struct at that index breaks it and the code terminates without any errors. since there are no errors then a try/catch doesn't give me anything either.
// Wanted to create a structure to handle the objects easier instead
// of multiple vectors for each property
struct appData {
std::string id = "";
std::string name = "";
std::string vdf_file = "";
std::string vdf_path = "";
};
// Relevant parts of my main()
int main() {
// Map that stores all the struct pointers
std::unordered_map<std::string, appData*> appDatas;
char memory[sizeof(appData)];
void* p = memory;
// New instance of appData
appData *tempAppData = new(p) appData();
tempAppData->appid = "86901";
// Add tempAppData to map with string key
appDatas["86901"] = tempAppData;
...
std::vector<appData*> unhashed_appDatas;
for (auto const& pair: appDatas) {
unhashed_appDatas.push_back(pair.second);
}
...
for (unsigned int x = 0; x < unhashed_appDatas.size(); x++) {
// Output index to see where it was messing up
std::cout << x << std::endl;
!! // This is where the issue happens on the second loop (see output)
appData *thisAppData = unhashed_appDatas[x];
std::string id = thisAppData->appid;
std::cout << id << std::endl;
/* ...
Do more stuff below
*/
}
...
return 0;
}
Terminal Output:
0 // Initial index of x
86901 // Id of first item
1 // New index of x on second loop before pointer var is created
// Nothing more is printed and execution terminates with no errors
My knowledge of c++ is pretty lacking, started it couple days ago, so the few things within my knowledge I've tried: moving the *thisAppData variable outside of the loop, using a for(var: vector) { ... }, and a while loop. I can assume that the issue lies with the pointer and the local variable when inside the loop.
Any help/input about how I could better approach this or if there's an issue with my code would be appreciated :)
Edit: Changed code to use .size() instead of sizeof() per #Jarod42 answer, though main issue persists
Edit2: Turns out it was my own mess-up, imagine that. 4Am brain wasn't working too well- posted answer regarding what I did incorrectly. Thanks to everyone who helped me
sizeof is the wrong tool here:
for (unsigned int x = 0; x < sizeof(unhashed_appDatas); x++) {
// ^^ wrong: give **static** size of the structure
// mainly 3 members (data, capacity, size), so something like `3*sizeof(void*)`
it should be
for (unsigned int x = 0; x < unhashed_appDatas.size(); x++) {
After many hours of trial and error I have determined the issue (aside from doing things in a way I should, which I've since corrected) it was something I messed up on that caused this issue.
TLDR:
Items wouldn't exist that I assumed did and tried to read files with a blank path and parse the contents that didn't exist.
Explaination:
In the first loop, the data I was getting was a list of files from a directory then parsing a json-like file that contained these file names and properties associated with them. Though, the file list contained entries that weren't in this other data file (since I had no check if they existed) so it would break there.
Additionally in the last loop I would get a member from a struct that would be the path of a file to read, but it would be blank (unset) because it didn't exist in data file so std::ifstream file(path); would break it.
I've since implemented checks for each key and value to ensure it will no longer break because of that.
Fixes:
Here are some fixes that were mentioned that I added to the code, which did help it work correctly in the end even if they weren't the main issue that I myself caused:
// Thanks to #EOF:
// No longer "using placement new on a buffer with automatic storage duration"
// (whatever that means haha) and was changed from:
char memory[sizeof(appData)];
void* p = memory;
appData *tempAppData = new(p) appData();
// To:
appData *tempAppData = new appData();
// Thanks to #Jarod42:
// Last for loop limit expression was corrected from:
for (unsigned int x = 0; x < sizeof(unhashed_appDatas); x++) {
}
// To:
for (unsigned int x = 0; x < unhashed_appDatas.size(); x++) {
}
// I am still using a map, despite comment noting to just use vectors
// (which I could have, but just would prefer using maps):
std::unordered_map<std::string, appData*> appDatas;
// Instead of doing something like this instead (would have arguably have been easier):
std::vector<std::string> dataKeys = { "1234" };
std::vector<appData*> appDatas = { ... };
auto indx = find(dataKeys.begin(), dataKeys.end(), "1234");
indx = (indx != dataKeys.end() ? indx : -1);
if (indx == -1) continue;
auto dataItem = appDatas[indx];
//
I appreciate everyone's assistance with my code
I'm trying to use a linear search function on a dynamic array I created but then the teaching assistant for my computer science class told us that most search functions use const arrays.
Is there any way I can edit my dynamic array to become contant? Or is it also possible to make a search function that uses a dynamic array (and would not give errors?).
I'll give a brief insight into my code:
I dynamically create an array using the rows I read in from a file and then dynamically allocate each row to an array of columns.
char ** domain[] = new char * [rows];
for(int i = 0; i < rows; i++)
{
*domain = new char[columns];
domain++;
}
The type of function that we were taught for searching is:``
char searchArray( const char list[], char letter, int maxSize)
{
>code goes here
}
Is there any other method of using a search function that takes in dynamic multidimensional arrays?
In response to the comments, I can't use vectors. This is an assignment for us to use normal arrays. I haven't been taught how to to use vectors yet.
In the line
char ** domain[] = new char * [rows];
char ** domain[] tries to make an array of char **. If the compiler didn't complain about not having a valid array size in the [] and you would have a 3D structure. You want just plain old char ** for a 2D structure, so
char ** domain = new char * [rows];
The loop filling out the inner dimension is correct except it loses track of the starting point of domain
for(int i = 0; i < rows; i++)
{
*domain = new char[columns];
domain++;
}
Should be something like
char ** temp = domain;
for(int i = 0; i < rows; i++)
{
*temp = new char[columns];
temp++;
}
To preserve the starting point, but for this case array notation is probably the smarter and easier-to-read option.
for(int i = 0; i < rows; i++)
{
domain[i] = new char[columns];
}
On to searchArray. It needs to know it's getting two dimensions, (const char **) and that there are two max sizes (maxRow and maxColumn). It will look something like
char searchArray(const char ** list,
char letter,
int maxRow,
int maxColumn)
{
>code goes here
}
Code goes here is your problem, but will probably be two nested for loops iterating to maxRow and maxColumn and returning when letter is found.
But... Why return a char? Returning the location in the array is much more useful. We could use std::pair, but if std::vector is off limits, pair probably is as well. Consider something like the following instead:
struct coord
{
int row;
int column;
};
coord searchArray(const char ** list,
char letter,
int maxRow,
int maxColumn)
{
coord location;
>code goes here
return location;
}
If the item is not found, set row and column to something impossible to get like -1 so you can easily test for the not found case.
Stop here unless you want to <expletive deleted> with your teacher's brain.
The above doesn't build a 2D array. You can't get a dynamically allocated 2D array in C++. What you have is an array of arrays. There are a couple downsides to this, look at all the work that goes into stitching one together and computers love it when things go in straight lines. Array of arrays doesn't. Every different allocation can be somewhere completely different in memory forcing the program to hop around, waiting on and loading different chunks of memory. Sometimes The program will spend more time sitting around waiting for stuff to be found and loaded than it'll spend doing the actual work. This sucks.
The solution is to make a 1D array and make it look like a 2D array. Here's an example of that from the C++ FAQ
You'll learn a lot of neat stuff from following this example, not the least of which being RAII and the Rule of Three, two concepts without which you cannot write non-trivial high quality C++ code.
This code every time display Cat in debugger. First time and second time. But I want to display Cat at 1st time in debugger and then Dog in 2nd time debugger.
int main(){
CStringArray arr;
arr.Add("Cat");
arr.Add("Dog");
for (int i = 0; i < arr.GetSize(); i++)
{
cout<<"arr[i]"<<endl;
}
return 0;*
}
Forget about cout I just used debugger.
Use
for (int i = 0; i < arr.Count(); i++)
{
cout<<arr.GatAt(i)<<endl;
}
cout<<"arr[i]"<<endl; //this will print normal string i.e arr[i]
This is a simple code, it prints Cat and Dog. It won't print Cat, Cat, unless you have an error elsewhere. Are you looking for TRACE or OutputDebugString?
CStringArray arr;
arr.Add("Cat");
arr.Add("Dog");
for (int i = 0; i < arr.GetSize(); i++)
{
const char *temp = arr[i];
TRACE("%s\n", arr[i]);//add break point here to look at temp
//or
//OutputDebugString(arr[i]);
//OutputDebugString("\n");
}
ps, I think you want to use breakpoints and look at arr[i] on the fly. You can use const char *temp = arr[i] it will make the nth element visible. Otherwise I don't know.
pss, debug feature Autos may not show the value at temp depending on where you put breakpoint, because it makes decisions automatically. But you should be able to look at it by just moving the mouse over to temp.
You can't do it in Autos or Locals views, but you can do it in Watch view. Add to Watch view arr.m_pData,3. Value 2 would be enough here, but let's see what happens if you put a larger number. When you expand the name in the Watch view you will be able to see three items. First two will be Cat and Dog and the third will most likely be '<Error reading characters of string.>'.
There is also a way to tell Visual Studio how to display CStringArray variables properly, but you'd have to make some changes, as described in another answer.
I am writing a program to simulate a cache in c++ and am trying to copy addresses that are given in a file into an array. I am struggling to figure out how to copy an array into another array so that I can have an array of memory address arrays. I have read in the addresses into an array called "address" and I want my simulated cache to be an array called "L1_Cache". h is a counter that I am incrementing after I put an address into the L1_Cache. Also, cache size is going to be how many lines of addresses are available in my L1_Cache array, which will be decided by the user of the program. Below is the snippet where I am trying to put the array into the other array.
if(sizeof(L1_Cache) < cachesize)
strcpy(L1_Cache[][h], address);
they are defined as:
const char* address[10];
char* L1_Cache;
If anyone has any suggestions on how to copy one array into another array to make an array of arrays, let me know. I am not sure if anything I am doing is correct, but I am struggling to figure this out.
I want to compare new addresses that I am given to old addresses that are already in the L1_Cache array.
Yes, it is possible to make an array of arrays.
int a[3][3]; // a is an array of integer arrays
You have
a[0]; // this refers to the first integer array
a[1]; // this refers to the second array
Is the following what you are looking for?
#include <iostream>
#include <cstring>
int main()
{
char p[2][256];
strncpy(p[0], "This is my first address", 256);
strncpy(p[1], "This is my second address", 256);
std::cout << p[0] << std::endl << p[1];
return 0;
}
Yes. They are called multidimensional arrays.
They can have any number of dimensions.
For example:
int foo[3][3]; // initialize the 2 dimensional array of integers
foo[0][0] = 1; // change a value
foo[0][1] = 2; // change a value
foo[0][2] = 3; // change a value
foo[1][0] = 4; // change a value
foo[1][1] = 5; // change a value
foo[1][2] = 6; // change a value
foo[2][0] = 7; // change a value
foo[2][1] = 8; // change a value
foo[2][2] = 9; // change a value
for(int i=0;i<3;++i){ // display the 2d array
for(int j=0;j<3;++j){
cout<<foo[i][j];
}
cout<<endl;
}
What's happening:
Values are being assigned in a chart.
Think of it like writing a value on each point of a piece of paper.
I'm creating a new object of my class 'Dynamic' (not shown), which inheritates from 'Organic', which inheritates from 'Being' certain parameters such as id, biomeRow, etc.
Organic has: features_ (a struct), max_spawn_, total_spawn_, age_dur_ (an array) and current_age_.
The problem: Upon creating a Dynamic object, all values are set just right except max_spawn_. I've done my printfs both before creating Dynamic, in the creation of Dynamic and in the creation of Organic for the input value, and all of them are correct.
Features struct is right, total_spawn_ is right, age_dur_ array and current_age_ are both also right.
All of them are what I asked except for max_spawn_. maxSpawn is the value I'm passing (20), max_spawn_ should then be 20, but it isn't. All my printfs and debugging console show it is something around -858993460. I'm guessing that's just garbage, but I don't know how is it possible when all I'm doing is:
max_spawn_ = maxSpawn;
So, this is my function:
Organic::Organic(int id, int biomeRow, int biomeColumn, int biomeType, int beingType,
int object, Features features, int maxSpawn, int totalSpawn,
int age_dur[5], int current_age)
: Being(id, biomeRow, biomeColumn, biomeType, beingType, object)
{
features_ = features;
max_spawn_ = maxSpawn;
total_spawn_ = totalSpawn;
age_ = current_age;
for (int i = 0; i <= 5; i++)
age_dur_[i] = age_dur[i];
printf("\n%d\n", max_spawn_);
}
age_dur (and presumably age_dur_) are int [5] arrays. Copying like this:
for (int i = 0; i <= 5; i++)
age_dur_[i] = age_dur[i];
will overwrite something near age_dur_ with something. If max_spawn_ is adjacent to age_dur_, it's probably being overwritten with garbage.
Change the loop to:
for (int i = 0; i < 5; i++)
age_dur_[i] = age_dur[i];