How to generate a matrix with certain areas appended certain numbers in C programming? - c

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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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 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 };

Related

find all 256 cases of 2 numbers in array of 8 length [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I find all combination of 2 numbers {0,1} in array of 8 length in c,
example
arr[]={0,0,0,0,0,0,0,0}
arr[]={0,0,0,0,0,0,0,1}
arr[]={0,0,0,1,1,0,0,1}
an so on
You can generate all combinations fairly easily using a recursive procedure:
arr = [0,0,0,0,0,0,0,0]
Generate(position)
if position > 8 then
print arr
else
arr[position] = 0
Generate(position+1)
arr[position] = 1
Generate(position+1)
Generate(1)
This will go down 8 levels in the call stack and then print the array [0, 0, 0, 0, 0, 0, 0, 0]. Then it will return to the 7th level, and go down again, printing [0, 0, 0, 0, 0, 0, 0, 1]. It will repeat this process, toggling each of the higher-order bits in turn until all 256 possibilities are generated. Instead of printing the arrays, you could save the arrays as you go.
Another possibility is to just create the 256 8-bit arrays and use an iterative procedure to toggle the elements in such a way as to guarantee you cover all your bases. An example with 4-bit strings:
0 0 0 0 0 0 0 0
0 0 0 0 => toggle bits in 4th position => 0 0 0 1
0 0 0 0 in blocks of size 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 1 => toggle bits in 3rd position => 0 0 0 1
0 0 0 0 in blocks of size 2 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 => toggle bits in 2nd position => 0 0 0 1
0 0 1 0 in blocks of size 4 0 0 1 0
0 0 1 1 0 0 1 1
0 0 0 0 0 1 0 0
0 0 0 1 0 1 0 1
0 0 1 0 0 1 1 0
0 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 0
0 0 1 1 0 0 1 1
0 0 0 0 0 1 0 0
0 0 0 1 0 1 0 1
0 0 1 0 0 1 1 0
0 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
0 0 0 1 => toggle bits in 1st position => 0 0 0 1
0 0 1 0 in blocks of size 8 0 0 1 0
0 0 1 1 0 0 1 1
0 1 0 0 0 1 0 0
0 1 0 1 0 1 0 1
0 1 1 0 0 1 1 0
0 1 1 1 0 1 1 1
0 0 0 0 1 0 0 0
0 0 0 1 1 0 0 1
0 0 1 0 1 0 1 0
0 0 1 1 1 0 1 1
0 1 0 0 1 1 0 0
0 1 0 1 1 1 0 1
0 1 1 0 1 1 1 0
0 1 1 1 1 1 1 1

Understanding malloc(), realloc() and free() in C from reading an output

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 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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 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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 0 0 0 0 0 0 0 0 0 0 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.

Golang read byte array from .. to and display result

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.

c malloc, why has my array random numbers in it? [duplicate]

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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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
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().

How to diminish CPU usage in linux c application (detached multithread)?

I eager to know which situation makes increased cpu-usage in checking top information to process written by me.
below is my environment.
# cat /proc/cpuinfo
system type : CN3010_EVB_HS5 (CN5010p1.1-500-SCP)
processor : 0
cpu model : Cavium Octeon+ V0.1
BogoMIPS : 1000.00
wait instruction : yes
microsecond timers : yes
tlb_entries : 64
extra interrupt vector : yes
hardware watchpoint : yes, count: 2, address/irw mask: [0x0ffc, 0x0ffb]
ASEs implemented :
shadow register sets : 1
kscratch registers : 0
core : 0
VCED exceptions : not available
VCEI exceptions : not available
# cat /proc/softirqs
CPU0
HI: 0
TIMER: 37673
NET_TX: 1
NET_RX: 63481
BLOCK: 0
BLOCK_IOPOLL: 0
TASKLET: 241456
SCHED: 0
HRTIMER: 0
RCU: 45060
#
# cat /proc/stat
cpu 6890 0 7591 11217 324 691 17637 0 0
cpu0 6890 0 7591 11217 324 691 17637 0 0
intr 3872174 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3557213 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30852 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2530 0 6328 275165 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 7152106
btime 1387517330
processes 1956
procs_running 2
procs_blocked 0
softirq 454085 0 44350 1 74147 0 0 282595 0 0 52992
**Cpu(s): 20.6%us, 23.0%sy, 0.0%ni, 15.0%id, 0.0%wa, 2.2%hi, 39.3%si, 0.0%st**
Mem: 45220K used, 50560K free, 0K shrd, 0K buff, 17568K cached
Load average: 0.52 0.72 0.52
PID USER STATUS RSS PPID %CPU %MEM COMMAND
800 root S 5076 1 75.3 5.2 core
2104 root S 2448 848 0.0 2.5 sshd
....
--> core is my process(with multithread) that has about 22 threads for doing jobs.
shortly, one thread to collect wireless packet, one thread changes wifi-frequency using netlink library. I'm not sure but I think that makes increased cpu-usage.
I don't know to control this situations, how to approach,
Which part do I check?
below is my thread style.
while(1) {
do jobs;
sleep(x);
}
--> switched to
while (1) {
sleep(x);
do jobs;
}
Can't resolve it. How to handle this issue? which part do I check ?
Please help me. I don't want to upgrade CPU.
You would generally use a profiler to determine where your application is spending its time.
Newer Linux kernels have a very low overhead profiler built in, which can trace in and out of kernel space as well, named perf. You can perf record your application and then run perf report to see what it did.
It seems you are using OCTEON+ CN5010 cpu. As an advanced perf improvement task, you could utilize OCTEON simple-exec programs (on Linux, or on separate cores natively), to get a further performance boost.
Paxym

Resources