|
That code will not work.
You are correct to question it.
If the code actually runs it is only because of the way that the compiler lays down the stack and/or what the exact code did just before the first method was called.
In the method the pointer is considered to be non-initialized but because it is a pointer that was created on the stack it will have a value, whatever was in the memory that the stack is using at that point. Thus correctly stated 'garbage'.
|
|
|
|
|
It works.
Yes, I think this is typical wild pointer.
|
|
|
|
|
samzcs wrote: It works.
It runs, which is a bit different.
If the compiler changes in the future or the flow does then it might stop. Or it might cause problems in other places.
|
|
|
|
|
That is wrong. The usual idiom is instead
void funcA( unsigned int * dtc, unsigned int* status)
{
int rval;
unsigned int lstatus;
rval = funcB(dtc, &lstatus);
.....
}
int funcB(unsigned int *idtc, unsinged int* istatus ) {
*istatus = XXX;
}
|
|
|
|
|
I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments.
"ESSolver.h"
#include <vector>
using namespace std;
extern int row;
extern int col;
extern vector<vector<double>> _muPop(row, vector<double>(col));
class ESSolver
{
public:
ESSolver();
void Init();
void Disp();
private:
vector< vector<double> > *muPop;
};
"ESSolver.cpp"
#include <iostream>
#include <vector>
#include "../include/ESSolver.h"
using namespace std;
ESSolver::ESSolver()
{
muPop = &_muPop;
return;
}
void ESSolver::Init()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
(*muPop)[i][j] = i+1;
}
}
}
void ESSolver::Disp()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << (*muPop)[i][j] << " ";
}
cout << endl;
}
}
"main.cpp"
#include "../include/ESSolver.h"
#include <vector>
#include <iostream>
using namespace std;
int row;
int col;
vector<vector<double>> _muPop(row, vector<double>(col));
int main(int argc, char *argv[]){
row = atoi(argv[1]);
col = atoi(argv[2]);
ESSolver es;
es.Init();
}
modified 29-Jan-21 21:01pm.
|
|
|
|
|
You declare your vector as a global, but the values of row and col will be zero when it is created. Make it a local variable and let your ESSolver object manage it. Or better still make it a member of the ESSolver class.
|
|
|
|
|
Your concept is totally wrong and won't work at least here:
vector<vector<double>> _muPop(row, vector<double>(col)); because row and col are not defined.
Write a class that holds the 2D array. That class should have an initialisation function with the number of rows and columns as parameters that allocates the array and initialises the array items.
Then there is even no need to have a global instance of that class. It can be a local member of your main function.
You might also check if the usage of vector is really necessary. If you don't need vector specific operations using classic C style arrays or C++ array templates might be a better option.
|
|
|
|
|
As suggested, don't use globals. Try, for instance
#include <vector>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Matrix
{
int R, C;
vector <vector <int> > m;
public:
Matrix(int r, int c):R(r),C(c)
{
for (int r = 0; r<R; ++r)
{
m.push_back(vector <int >());
for (int c = 0; c<C; ++c)
m[r].push_back(c+1);
}
}
friend ostream & operator << (ostream & os, const Matrix & mat);
};
ostream & operator << (ostream & os, const Matrix & mat)
{
for (int r = 0; r<mat.R; ++r)
{
for (int c = 0; c<mat.C; ++c)
os << setw(4) << mat.m[r][c];
os << endl;
}
return os;
}
int main(int argc, char * argv[])
{
int rows=1, cols=1;
if ( argc >= 3)
{
rows = stoi(argv[1]);
cols = stoi(argv[2]);
}
Matrix m(rows,cols);
cout << m << endl;
}
|
|
|
|
|
Thanks
modified 29-Jan-21 21:01pm.
|
|
|
|
|
|
I have a application, where on clicking on minimize button it should minimise to system tray and on clicking right button it should pop up a menu. Get i have that piece of code.
void CXTransferDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
DWORD dwMsg = NIM_ADD;
if (nID == SC_MINIMIZE)
{}
where i need to add the piece of code in the minimize loop
|
|
|
|
|
|
Is there any way to pass the size of an array using a command prompt.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int arr[ argv[1] ];
return 0;
}
modified 29-Jan-21 21:01pm.
|
|
|
|
|
Arguments come as an array of char * - that's the argv variable. One way to convert them is :
int size = atoi( argv[1] );
|
|
|
|
|
Now consider this code
#include <iostream>
using namespace std;
class cls
{
private:
int arr[len];
public:
int len;
void print_arr();
};
int main(int argc, char *argv[])
{
cls obj;
obj.len = atoi( argv[1] );
obj.print_arr();
return 0;
}
void cls::print_arr()
{
for (int i = 0; i < len; i++)
{
arr[i] = i;
cout << arr[i] << " ";
}
}
Getting errors
error: ‘len’ was not declared in this scope
error: ‘arr’ was not declared in this scope
modified 29-Jan-21 21:01pm.
|
|
|
|
|
The array needs to be dynamically allocated. This is done using the new operator. I usually prepend an "m_" to member variable names so I know that is what they are. The array will be allocated in the class constructor and released in the destructor.
class cls
{
private:
int * m_array;
public:
int m_length;
cls( int length );
~cls();
void print_array();
};
cls::cls( int length )
{
m_length = length;
m_array = new int[length];
}
cls::~cls()
{
delete [] m_array;
m_array = nullptr;
}
int main(int argc, char *argv[])
{
int length = atoi( argv[1] );
cls obj( length );
obj.print_array();
return 0;
}
|
|
|
|
|
Thanks
modified 29-Jan-21 21:01pm.
|
|
|
|
|
Consider also using a vector<int>[^], instead of a C-like array.
|
|
|
|
|
Hi
I want to limit the number of characters on a line in a multiline edit control
I figured I would do CEdit::linelength and divide that by rect.right
From getclientrect that would get me the number of pixels per character
I would then multiply it by the number of characters I want on a line however
Cedit:Linelength return zero
my edit control is declared CEdit mycontrol
I know under the covers it does a SendMessage EM_LINELENGTH
Is there a issue that I dont have a message map
The character index is 0
|
|
|
|
|
Did you pass a valid line number to it like 0? If you pass it -1 "the return value is the number of unselected characters in the lines that contain selected characters" according to the documentation.
You will have to do the length limiting of each line yourself because LimitText and SetLimitText define how many total characters the control can accept, not just on one line.
Also, to get the number of pixels per character you should call GetTextExtent which is a member of the CDC class. The typical way is to generate a string with all upper and lower case letters, call GetTextExtent with that string, and then divide the resulting width by the string length to get an average for each character. There are lots of examples that show how to do this.
|
|
|
|
|
I did pass 0 is the index and still it returned 0
I do use The GetTextExtent but thought that LineLength would be easier
This is the code I used to figured out how much space I needed per line I was looking to allow the user up to 8 hex characters (by that I mean 0 -F )
It just didn't come up exactly the way I figured
CSize mycsize = gprdc->GetTextExtent(_T("0123456789ABCDEF"), 16);
mycsize.cx = mycsize.cx / 16;
RECT gprrect, gprrect1, accessrect;
WINDOWINFO dlgwin;
GetWindowInfo(&dlgwin);
asidgprs.GetWindowRect(&gprrect);
asidaccess.GetWindowRect(&accessrect);
int height = gprrect.bottom - gprrect.top;
gprrect.left = (gprrect.left - dlgwin.rcWindow.left) + dlgwin.cxWindowBorders;
accessrect.left = (accessrect.left - dlgwin.rcWindow.left) + dlgwin.cxWindowBorders;
gprrect.top = gprrect.top - ( dlgwin.rcWindow.top + dlgwin.cyWindowBorders + 20) ;
accessrect.top = accessrect.top - ( dlgwin.rcWindow.top + dlgwin.cyWindowBorders + 20);
gprrect.bottom = ( gprrect.top + height) -20 ;
accessrect.bottom = (accessrect.top + height) - 20;
gprrect.right = gprrect.left + (mycsize.cx * 10);
accessrect.right = accessrect.left + (mycsize.cx * 10);
mycsize.cx represents the number of pixels per character originally I multiplied by 8 figuring 8 characters per line but I ended up multiplying by 10 to get 8 characters to fit
asidgprs and asidacess are 2 multiline edit controls in the Dialog
I ended doing a lot tweeking wit this to get it to the right size
|
|
|
|
|
Is there a reason you need to use multi-line edit controls of a fixed width? It seems like an array of single line edit controls might do what you need and it is easier to limit the length of data in them.
|
|
|
|
|
If they are all adjacent to each other they look like multi line ?
|
|
|
|
|
This message returns the length of the existing text at that line, not the amount of space available. How many characters are in the control when you call this?
|
|
|
|
|
Thanks that’s it I wanted to calculate the number of characters the control can hold on a line
|
|
|
|