I have tried to solver linear system Ax = b using eigen solvers (Eigen 3.3.7). However, I found the results (x) solved by SparseQR is different with results from dense solver (bdcsvd) and also other sparse solver, but the relative errors (||Ax - b|| / ||b||) are same.
Could someone can help me to understand why sparseQR gives different results and how to make sure it gives the same results as from SVD?
I also have noticed that for some other datasets, sparseQR and SVD give the same results, but not from CG.
Thank you.
Matrix A and b can be found in below.
Eigen::MatrixXd A; // reading from a file, see below for a simple sample
Eigen::MatrixXd b; // reading from a file
Eigen::VectorXd x_svd = A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
//convert to sparse matrix
Eigen::SparseMatrix<double> As = A.sparseView(0.1, 1e-5);
tau = 1e-3; // check coefficients are close or not
// SparseQR
{
Eigen::SparseQR<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int>> solver;
//solver.setPivotThreshold(1e-10);
solver.compute(As);
if (solver.info() != Eigen::Success)
std::cout << "SparseQR::Decomposition Failed" << std::endl;
Eigen::VectorXd xs = solver.solve(b);
if (solver.info() != Eigen::Success)
std::cout << "SparseQR::Solving Failed" << std::endl;
std::string str_equal = xs.isApprox(x_svd, tau) ? " Equal" : " UnEqual";
std::cout << "Results::" << str_equal << std::endl;
std::cout << "EstimatedError: " << (As * xs - b).norm() / b.norm() << std::endl;
}
// ConjugateGradient
{
Eigen::ConjugateGradient<Eigen::SparseMatrix<double> > solver(As.transpose() * As);
if (solver.info() != Eigen::Success)
std::cout << "CG::Decomposition Failed" << std::endl;
Eigen::VectorXd xcg = solver.solve(As.transpose() * b);
if (solver.info() != Eigen::Success)
std::cout << "CG::Solving Failed" << std::endl;
std::cout << "#Iterations: " << solver.iterations() << std::endl;
std::cout << "EstimatedError: " << solver.error() << std::endl;
std::string str_equal = corr.isApprox(para_coeff, tau) ? " Equal" : " UnEqual";
std::cout << "Results::" << str_equal << std::endl;
}
Matrix A:
0 35516 0 17732 -35516 0 -17732 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 17732 0 35516 -17732 0 -35516 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 -35516 0 -17732 35516 0 17732 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 -17732 0 -35516 17732 0 35516 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 35516 0 17732 -35516 0 -17732 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 17732 0 35516 -17732 0 -35516 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 -35516 0 -17732 35516 0 17732 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 -17732 0 -35516 17732 0 35516 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 35516 0 17732 -35516 0 -17732 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 17732 0 35516 -17732 0 -35516 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 -35516 0 -17732 35516 0 17732 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 -17732 0 -35516 17732 0 35516 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 35516 0 17732 -35516 0 -17732 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17732 0 35516 -17732 0 -35516 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -35516 0 -17732 35516 0 17732 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -17732 0 -35516 17732 0 35516 0
0 0 35516 17732 0 0 0 0 0 0 0 0 -35516 -17732 0 0 0 0 0 0 0 0 0 0
0 0 17732 35516 0 0 0 0 0 0 0 0 -17732 -35516 0 0 0 0 0 0 0 0 0 0
0 0 -35516 -17732 0 0 0 0 0 0 0 0 35516 17732 0 0 0 0 0 0 0 0 0 0
0 0 -17732 -35516 0 0 0 0 0 0 0 0 17732 35516 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 35516 17732 0 0 0 0 0 0 0 0 -35516 -17732 0 0 0 0 0 0
0 0 0 0 0 0 17732 35516 0 0 0 0 0 0 0 0 -17732 -35516 0 0 0 0 0 0
0 0 0 0 0 0 -35516 -17732 0 0 0 0 0 0 0 0 35516 17732 0 0 0 0 0 0
0 0 0 0 0 0 -17732 -35516 0 0 0 0 0 0 0 0 17732 35516 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 35516 17732 0 0 0 0 0 0 0 0 -35516 -17732 0 0
0 0 0 0 0 0 0 0 0 0 17732 35516 0 0 0 0 0 0 0 0 -17732 -35516 0 0
0 0 0 0 0 0 0 0 0 0 -35516 -17732 0 0 0 0 0 0 0 0 35516 17732 0 0
0 0 0 0 0 0 0 0 0 0 -17732 -35516 0 0 0 0 0 0 0 0 17732 35516 0 0
b
225915
-380755
-225915
380755
1.68807e+07
9.48266e+06
-1.68807e+07
-9.48266e+06
-32610.3
-1.5961e+06
32610.3
1.5961e+06
-1.08558e+07
1.41591e+06
1.08558e+07
-1.41591e+06
-2.79489e+06
-4.57751e+06
2.79489e+06
4.57751e+06
1.22018e+06
3.70838e+06
-1.22018e+06
-3.70838e+06
-4.80155e+06
-2.53063e+06
4.80155e+06
2.53063e+06
Related
int main(int argc, char *argv[]){
int grid_u[51][51];
int grid_v[51][51];
int u[] = {0,1};
int v[] = {0,1};
int i,j;
int n = 0;
for (i = 0; i < 10; i++){
for (j = 0; j < 10; j++){
grid_u[i][j] = u[1];
grid_v[i][j] = v[0];
}
}
for (i = 10;i <= 20; i++){
for (j = 10;j <= 20; j++){
grid_u[i][j] = u[0];
grid_v[i][j] = v[1];
}
}
for (i = 21; i <= 50; i++){
for (j = 21; j <= 50; j++){
grid_u[i][j] = u[1];
grid_v[i][j] = v[0];
}
}
for (i = 0; i <= 50; i++){
for (j = 0; j <= 50; j++, n++){
if (n % 51 == 0){
printf("\n");
}
printf("%d ", grid_u[i][j]);
}
printf("\n");
}
for (i = 0; i <= 50; i++){
for (j = 0; j <= 50; j++, n++){
if (n % 51 == 0){
printf("\n");
}
printf("%d ", grid_v[i][j]);
}
printf("\n");
}
}
Like the code above, I want to create two 51*51 matrices with certain areas a box indexed from 10~20 appended certain numbers, which is an 11*11 squared box. But the results are pretty weird, can somebody help me figure it out? Very appreciated.
The expected matrix is like this:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
But my output is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -389530064 32766 201198390 1 0 0 539592928 32767 0 -507641278 539592928 32767 539592928 32767 539592928 32767 0 0 0 0 -389529936 32766 201195565 1 0 0 0 0 0
0 0 0 0 0 0 0 -389527408 32766 539592928 32767 446038026 -2119743701 539592928 32767 539592928 32767 539592928 32767 0 0 -389527408 32766 -389528784 32766 201194920 1 0 0 0 0 0 0 0 0 539592928 32767 0 0 -389527408 32766 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -389529648 32766 201198390 1 -389529632 32766 538241768 32767 0 -11761900 538241768 32767 538241768 32767 538241768 32767 -389529584 32766 201198390 1 -389529520 32766 710482432 32767 0 -1690551536 710482432
32767 710482432 32767 710482432 32767 0 0 0 0 -389529456 32766 201195565 1 446038026 -2119743701 538241768 32767 538241768 32767 538241768 32767 0 0 0 0 -389526928 32766 710482432 32767 446038026 -2119743701 710482432 32767 710482432 32767 710482432 32767 0 0 -389526928 32766 -389528304 32766 201194920 1 -389526976 32766 0 0 0 0
0 0 710482432 32767 0 0 -389526928 32766 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -389529184 32766 201198390 1 0 0 538145032 32767 0 -886581335 538145032 32767 538145032
32767 538145032 32767 0 0 0 0 -389529056 32766 201195565 1 -389529088 32766 201198390 1 0 0 541141784 32767 0 -1288945918 541141784 32767 541141784 32767 541141784 32767 0 0 0 0 -389528960 32766 201195565 1 0 0 -389526528 32766 -389527904 32766 201194920 1 0 0 0 0 -389526432 32766 541141784 32767
446038026 -2119743701 541141784 32767 541141784 32767 541141784 32767 0 0 -389526432 32766 -389527808 32766 201194920 1 0 0 0 0 0 0 0 0 541141784 32767 0 0 -389526432 32766 0 0 0 0 0 0 0 0 0 0 32 0 135296 0 446038026 -2119743701 539592928 32767 539592928 32767 -389527408
32766 0 0 539592928 32767 -389528704 32766 201192449 1 9 0 0 0 446038026 -2119743701 539592928 32767 0 0 9 0 65536 0 539592928 32767 -389528576 32766 201157890 1 0 5 132858 133013 133035 135150 135254 135274 -389528576 32766 0 0 -389527408 32766 539592928 32767 446038026 -2119743701 539592928 32767 -389527408 32766
9 0 65536 0 539592928 32767 -389527424 32766 201156681 1 0 0 0 0 0 0 0 0 0 0 0 0 -389525920 32766 616766208 26 201842144 1 616766208 32767 -389527104 32766 201814680 1 0 0 -389525920 32766 -389527296 32766 201194920 1 -389528352 32766 201198390 1 446038026 -2119743701 538241768 32767 538241768
32767 -389526992 32766 0 0 538241768 32767 -389528288 32766 201192449 1 446038026 -2119743701 710482432 32767 710482432 32767 -389526928 32766 0 0 710482432 32767 -389528224 32766 201192449 1 -389528160 32766 201157890 1 446038026 -2119743701 710482432 32767 0 0 12 0 65536 0 710482432 32767 -389528096 32766 201157890 1 446038026 -2119743701 538241768 32767
-389526992 32766 11 0 65536 0 0 0 -389526928 32766 710482432 32767 446038026 -2119743701 710482432 32767 -389526928 32766 12 0 65536 0 710482432 32767 -389526944 32766 201156681 1 201837632 1 537923568 27 -389526640 32766 201814680 1 -389526640 32766 201814680 1 -389527904 32766 201195565 27 201841856 1 540166192 32767 -389526544 32766 201814680
1 201846864 1 0 0 -389525376 32766 537923568 32767 446038026 -2119743701 538145032 32767 538145032 32767 -389526528 32766 0 0 538145032 32767 -389527824 32766 201192449 1 -389527808 32766 201198390 1 446038026 -2119743701 538145032 32767 446038026 -2119743701 541141784 32767 541141784 32767 -389526432 32766 0 0 541141784 32767 -389527728 32766 201192449 1 -389527664 32766
201195565 1 446038026 -2119743701 541141784 32767 0 0 13 0 65536 0 541141784 32767 -389527600 32766 201157890 1 65536 0 538145032 32767 -389526544 32766 201156681 1 -389526528 32766 0 0 -389526432 32766 541141784 32767 446038026 -2119743701 541141784 32767 -389526432 32766 13 0 65536 0 541141784 32767 -389526448 32766 201156681 1 -389527456
32766 201195565 1 -389527488 32766 201198390 1 -389527408 32766 578869728 32767 0 -1288945918 578869728 30 201842416 1 578869728 32767 -389526032 32766 201814680 1 -389527360 32766 201195565 1 446038026 -2119743701 201842928 1 201842144 0 9 0 65536 0 -389527144 32766 446038026 -2119743701 9 0 201842144 1 -389527120 32766 0 0 201814680 1
-389526960 32766 201237120 1 539592360 32767 2 65536 14942208 -2130640895 539592416 32767 2 65536 55836928 1 539592472 32767 2 65536 471567399 1 539592536 32767 2 65536 16666626 1 539592608 32767 2 65536 -389527152 32766 538248373 32767 -2029124312 32767 -2140435368 32767 -2140435352 32767 -2140435320 32767 0 0 -2140435664 32767 0 0 -2140434248
32767 -389527072 32766 538248373 32767 -2029124312 32767 -2140435368 32767 -2140435352 32767 -2140435320 32767 0 0 -2140435664 32767 0 0 -2140434248 32767 -389526992 32766 538248373 32767 -2029124312 32767 -389526976 32766 -389526960 32766 -389526928 32766 0 0 -2140434248 32767 -389526992 32766 540045747 32767 0 0 -389526512 32766 -389526400 32766 538250950 32767 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 538902282 32767 0 0 5120 0 1 0 2 0 116988416 1 116993536 1 -389526464 32766 538901088 32767 -1073737726 -157745152 -2140405761 32767 1530938464 32721 116961280 1 -389526544 32766 538904623 32767
0 0 538902282 32767 0 0 5120 0 1 0 2 0 116988416 1 116993536 1 -389526336 32766 538901088 32767 -1073737726 -157745152 539492351 32767 1530938464 32721 116961280 1 -389526416 32766 538904623 32767 0 0 538902282 32767 0 0 5120 0 1 0 2 0 116988416 1 116993536 1 -389526208 32766 538901088
32767 -1073737726 -157745152 539492351 32767 1530938464 32721 116961280 1 -389526288 32766 538904623 32767 0 0 1530938464 32721 -389526224 32766 538919169 32767 -2146959359 0 1530938464 32721 1530938464 32721 -2140581792 32767 1 0 0 0 -389526096 32766 539453250 32767 -389526160 32766 538895975 32767 -2140323840 32767 32 0 0 0 2 0 -389526096 32766
539457653 32767 -2140581712 32767 1535149624 32721 0 0 -2029115376 32767 2 0 0 0 -389525968 32766 -2140581672 32767 1530938432 32721 116961280 1 -389526032 32766 538904623 32767 0 0 1530938432 32721 -389525968 32766 538919169 32767 -2146959360 0 1530938432 32721 1530938432 32721 -2140581752 32767 -389525696 32766 0 0 -389525840 32766 539453250 32767 0
It's pretty messy and I cannot find why this came out like this. It seems like the appending process in for loop does not act in expected order so that it returns a lot of large memory addresses...
You are not explicitly setting all the entries in the arrays. Local variables are not automatically initialised so they contain garbage until they are set. Change the array declarations to be the following to initialise them to all zeroes:
int grid_u[51][51] = { 0 };
int grid_v[51][51] = { 0 };
I need to write an assignement regarding how memory managment is implemented in order to understand what do the few non-zero numbers in the output of this code represent.
I do know that the malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of type void which can be casted into pointer of any form. I also know that if the dynamically allocated memory is insufficient or more than required, you can change the size of previously allocated memory using realloc() function.
Here is the code I have to analyze:
#include <stdlib.h>
#include <stdio.h>
/*** Just playing with the malloc(), realloc(), free()
*** in order to guess how memory management
*** is implemented on this machine. If you get SEGMENTATION
*** FAULT while addressing unallocated memory, just run
*** the program with different "min" and/or "max" values,
*** explicitly given on the command line through argv[]
*** NOTICE: the default values are appropriate for the 32bit systems
*** available in the labs ***/
void showmem (unsigned char *ptr, int min, int max, char name) {
int i;
for (i = min; i < 0; i++)
printf ("%hhu ",ptr[i]);
printf ("*%c=%hhu ",name,*ptr);
for (i = 1; i <= max; i++)
printf ("%hhu ",ptr[i]);
printf ("\n\n");
}
int main(int argc, char**argv) {
unsigned char *p, *q, *o;
int sz=1, min=-8, max=60;
if ( argc > 1 )
sscanf(argv[1],"%d",&sz);
if ( sz <= 0 )
sz = 1;
else if ( sz > 300 )
sz = 300;
if ( argc > 2 )
sscanf(argv[2],"%d",&min);
if ( min > -1 )
min = -1;
else if ( min < -50 )
min = -50;
if ( argc > 3 )
sscanf(argv[3],"%d",&max);
if ( max < sz )
max = sz;
else if ( max > (sz+100) )
max = sz+100;
printf("... allocating %d bytes to p[] (show memory from p[%d] to p[%d])\n\n",sz,min,max);
p = (unsigned char*)malloc(sz);
if ( p == NULL ) {
perror ("Error allocating p\n");
return -1;
}
showmem (p,min,max,'p');
printf("... allocating %d bytes to q[]\n\n",sz);
q = (unsigned char*)malloc(sz);
if ( q == NULL ) {
perror ("Error allocating q\n");
return -1;
}
showmem (p,min,max,'p');
showmem (q,min,max,'q');
sz += 10;
printf("... reallocating p[] to %d bytes (show old p[], new p[], and q[])\n\n",sz);
o = p;
p = (unsigned char*)realloc((void*)p,sz);
showmem (o,min,max,'o');
showmem (p,min,max,'p');
showmem (q,min,max,'q');
sz += 15;
printf("... reallocating p[] to %d bytes\n\n",sz);
p = (unsigned char*)realloc((void*)p,sz); //void e' l'indirizzo di memoria. sz e' la nuova dimensione
showmem (o,min,max,'o');
showmem (q,min,max,'q');
showmem (p,min,max,'p');
sz -= 25;
printf("... reallocating p[] to %d bytes\n\n",sz);
p = (unsigned char*)realloc((void*)p,sz);
showmem (o,min,max,'o');
showmem (q,min,max,'q');
showmem (p,min,max,'p');
printf("... freeing p\n\n");
free((void*)p); `
showmem (o,min,max,'o');
showmem (q,min,max,'q');
showmem (p,min,max,'p');
printf("... freeing q\n\n");
free((void*)q);
showmem (o,min,max,'o');
showmem (q,min,max,'q');
showmem (p,min,max,'p');
printf("... freeing old p\n\n");
free((void*)o);
showmem (o,min,max,'o');
showmem (q,min,max,'q');
showmem (p,min,max,'p');
return 0;
}
And here is the ouput compiling the file without any other inputs:
... allocating 1 bytes to p[] (show memory from p[-8] to p[60])
33 0 0 0 0 0 0 0 *p=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 129 253 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
... allocating 1 bytes to q[]
33 0 0 0 0 0 0 0 *p=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97 253 1 0 0
33 0 0 0 0 0 0 0 *q=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97 253 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
... reallocating p[] to 11 bytes (show old p[], new p[], and q[])
33 0 0 0 0 0 0 0 *o=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97 253 1 0 0
33 0 0 0 0 0 0 0 *p=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97 253 1 0 0
33 0 0 0 0 0 0 0 *q=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97 253 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
... reallocating p[] to 26 bytes
33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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49 253 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Here is the output while compiling with the input '64':
... allocating 64 bytes to p[] (show memory from p[-8] to p[64])
81 0 0 0 0 0 0 0 *p=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
... allocating 64 bytes to q[]
81 0 0 0 0 0 0 0 *p=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
81 0 0 0 0 0 0 0 *q=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
... reallocating p[] to 74 bytes (show old p[], new p[], and q[])
81 0 0 0 0 0 0 0 *o=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
97 0 0 0 0 0 0 0 *p=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
81 0 0 0 0 0 0 0 *q=0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0
81 0 0 0 0 0 0 0 *q=16 211 66 144 174 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
81 0 0 0 0 0 0 0 *p=112 210 66 144 174 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I did notice how in the memory pointed by q there is a piece of what was pointed by the p one.
What should I focus on while analyzing the output, and most importantly, what do those numbers represent in relation to the definition of malloc, realloc and free?
Thank you in advance for you help.
malloc might allocate more than the n bytes required. It has to do some book-keeping so that e.g. free knows how big the block was in order to fully deallocate it.
It's implementation-specific how malloc does that, either it can prepend each block by some known structure that free,realloc can read like here or it can keep some kind of searchable structure with block information. This is subject to heavy optimizations so it might get quite complicated, try to search for "memory allocation algorithm" or similar.
First of all: I know that this is super basic question and you'd expect to find enough material on the internet and there probably is. I feel pretty stupid right now for not understanding it, so no need to point that out to me - I know^^
From the google Directory API, the response you get when reading a custom Schema is JSON-enocded:
https://developers.google.com/admin-sdk/directory/v1/reference/schemas
I copy/pasted that response and wanted to read it.
func main() {
jsonExample := `
{
"kind": "admin#directory#schema",
"schemaId": "string",
"etag": "etag",
"schemaName": "string",
"displayName": "string",
"fields": [
{
"kind": "admin#directory#schema#fieldspec",
"fieldId": "string",
"etag": "etag",
"fieldType": "string",
"fieldName": "string",
"displayName": "string",
"multiValued": true,
"readAccessType": "string",
"indexed": true,
"numericIndexingSpec": {
"minValue": 2.0,
"maxValue": 3.0
}
}
]
}
`
var jsonDec schemaExample
jsonExampleBytes := []byte(jsonExample)
m := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonExample), &m)
byteStorage := make([]byte,600)
byteReader := bytes.NewReader(byteStorage)
res, err := byteReader.ReadAt(jsonExampleBytes,50)
fmt.Printf("############Hier : %v Err: \n%v",res,err)
fmt.Printf("Storage: %v\n",byteStorage)
byteStorage := make([]byte,600)
byteReader := bytes.NewReader(byteStorage)
res, err := byteReader.ReadAt(jsonExampleBytes,50)
fmt.Printf("Result : %v Err: %v\n",res,err)
fmt.Printf("Storage: %v\n",byteStorage)
This returns
res : 526 Err: <nil>
Storage: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
.My question is how to implement a ReadFromTo method, which allows me to read a specific range of bytes from a byte array? And since the storage is empty, I also lack to understand how read that array back at all with the reader functions, only way I know how to pull it off is this:
fmt.Printf("Und die bytes to String: %v",string([]byte(jsonExample)))
From the docs (emphasis mine):
ReaderAt is the interface that wraps the basic ReadAt method.
ReadAt reads len(p) bytes into p starting at offset off in the underlying input source.
type ReaderAt interface {
ReadAt(p []byte, off int64) (n int, err error)
}
The argument to ReadAt (and Read in general) is the destination. You've got jsonExampleBytes and byteStorage the wrong way around.
package main
import (
"bytes"
"fmt"
)
func main() {
jsonExampleBytes := []byte(`{...}`)
byteReader := bytes.NewReader(jsonExampleBytes)
byteStorage := make([]byte, 600)
n, err := byteReader.ReadAt(byteStorage, 3)
fmt.Println("Storage:", string(byteStorage[:n]), err) // Storage: .} EOF
}
To access a sub-slice of bytes, you can in the most basic case just use the index operator:
array := make([]byte, 100)
bytes5to9 = array[5:10]
note here that the second index is exclusive.
If you need an io.Reader from these bytes, you can use
r := bytes.NewReader(array[5:10])
You can do this again, creating a second read for the same or a different range of the array.
The utility functions in io and ioutil might be of interest to you as well. See for example ioutil.ReadAll, io.Copy, io.CopyBuffer, io.CopyN and io.ReadFull.
This question already has answers here:
Is the memory chunk returned by malloc (and its cousins) initialized to Zero?
(6 answers)
What are the contents of the memory just allocated by `malloc()`?
(3 answers)
Closed 5 years ago.
i need to malloc an array with 1000 zeros but when I print it something weird happen
int MAX = 1000;
int *count_array
count_array = (int *) malloc(MAX * sizeof(int));
printf("success?\n");
print_array(count_array,MAX);
but the RETURN looks like:
0 0 0 0 -1973472672 32766 134609 0 857747513 842276920 540291872 540024888 824194871 540293152 892477493 540161824 874525748 809050162 540227104 668466 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 134081 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 133633 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I really need to get rid of the other numbers, why they are in this array? did I used Malloc wrong?
best regards, Tim4497
malloc() only allocates the memory, it does not zero it. You get whatever was in the memory before (e.g. random noise, or whatever the process that held the memory previously left in there when free()ing the memory -- which could be a safety issue if handling encryption keys and data).
If you need zeroed memory, use calloc().
I am trying to create a 2D matrix in C (basically a dynamically allocatable 2d array of any given size) in both the most efficient and clean way possible. I had implemented such a thing in a larger project I am working on, but was having issues, and was able to narrow it down to the following.
I decided to malloc a giant array (I called it data), and then make an array of pointers (i called it cell) to be able to address the data in the big array in such a way that would make sense in a two-dimensional context (as in matrix[x][y] instead of data[ugly pointer arithmetic each time].) I thought this would be a good idea because it only calls malloc once, and so it would be faster, also, the allocated memory is in one consecutive block, which I believe (not too knowledgeable here) is a really good thing on some systems because of overhead in keeping track of allocated memory blocks.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct {
unsigned int sizeX;
unsigned int sizeY;
int **cell;
int *data; /* FOR INTERNAL USE ONLY */
} matrix;
matrix * matrix_malloc(unsigned int, unsigned int);
void matrix_free(matrix *);
void matrix_zero(matrix *);
void matrix_print(matrix *);
int
main(int argc, char *argv[])
{
int y, x;
matrix *theMatrix = NULL;
if (argc != 3) {
fprintf(stderr, "usage: %s sizeX sizeY\n", argv[0]);
return 1;
}
x = atoi(argv[1]);
y = atoi(argv[2]);
if (x < 10 || y < 10) {
fprintf(stderr, "usage: sizeX and sizeY must be >= 10\n");
return 1;
}
if ((theMatrix = matrix_malloc(x, y)) == NULL)
return 1;
matrix_zero(theMatrix);
/* lots of modification of the contents of the matrix would happen here */
matrix_print(theMatrix);
matrix_free(theMatrix);
return 0;
}
matrix *
matrix_malloc(unsigned int sizeX, unsigned int sizeY)
{
int i;
matrix *mat;
if ((mat = malloc(sizeof(matrix))) == NULL) {
return NULL;
}
if ((mat->data = malloc(sizeX * sizeY * sizeof(int))) == NULL) {
free(mat);
mat = NULL;
return NULL;
}
if ((mat->cell = malloc(sizeX * sizeof(int *))) == NULL) {
free(mat->data);
free(mat);
mat = NULL;
return NULL;
}
mat->sizeX = sizeX;
mat->sizeY = sizeY;
for (i = 0; i < sizeX; i++) {
mat->cell[i] = mat->data + mat->sizeX * i;
}
return mat;
}
void
matrix_free(matrix *mat) {
free(mat->cell);
free(mat->data);
free(mat);
mat = NULL;
}
void
matrix_zero(matrix *mat)
{
memset(mat->data, 0, mat->sizeX * mat->sizeY * sizeof(int));
}
void
matrix_print(matrix *mat)
{
unsigned int x, y;
for (x = 0; x < mat->sizeX; x++) {
for (y = 0; y < mat->sizeY; y++)
printf("%d ", mat->cell[x][y]);
printf("\n");
}
}
When I run the above program as ./a.out 10 10 there is no problem, but when I specify 30 20 instead of 10 10, I run into some issues.
On MacOSX (10.6.7) I get:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 540024880 540024880 540024880 540024880 540024880 808465461 943207474 875896880 875704368 540031032
842216505 926168880 926425140 909719605 540031032 926234424 909325360 875896888 825438256 540160816 10 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
and then it exits properly.
On OpenBSD (4.7) I get this far:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
and then it just segfaults
My initial thought was that it was just some issue when allocating big enough blocks of memory that they cross page boundaries, but when I use 50 50 as the size, it runs fine.
I've narrowed it down this far, and tried googleing (not quite sure what it is I should be searching for though :| ) and asked a few of my friends, but this has them all stumped.
I found C. Segmentation Fault when function modifies dynamically allocated 2d array int matrix with pointers in C - memory allocation confusion but they were not relevant (as far as I can tell).
If somebody could please point me in the right direction, perhaps point out the problem or point me to some relevant documentation, I would be very grateful.
for (i = 0; i < sizeX; i++) {
mat->cell[i] = mat->data + mat->sizeX * i;
}
One of these SizeX'es needs to be a sizeY.
for (i = 0; i < sizeX; i++) {
mat->cell[i] = mat->data + mat->sizeX * i;
}
Imagine if sizeX is 100 and sizeY is 2. Here, you're laying out sizeX rows, 100 of them, each sizeX integers, 100 of them. Ooops.
That mat->sizeX should be mat->sizeY. You have sizeX rows, each with sizeY elements in them. So you need to skip forward sizeY integers to get to the next row.