What is the difference between char array[100]; and char array[100] = "";? - c

I am trying to write a C program that loads a file, reads it and outputs the longest line from the file together with the number of symbols. The result is written in another file. The code seems to be working like it is, but I would like to understand why I get wrong results once I slightly change the definition of the arrays, to remove the equals to empty quotes (= "") from the maxLine definition. For example, if I write the following:
char currentLine[100];
char maxLine[100];
then I get unwanted results.
Here is the whole function:
#define MAX_FILE_NAME 50
void maxCharRow()
{
FILE *fptr;
errno_t err;
char fileNameRead[MAX_FILE_NAME] = "test.txt";
char fileNameWrite[MAX_FILE_NAME] = "results.txt";
char currentLine[100];
char maxLine[100] = "";
if ((err = fopen_s(&fptr, fileNameRead, "r")) != NULL) {
printf("Could not open the file: %s\n", fileNameRead);
exit(1);
}
while (fgets(currentLine, sizeof(currentLine), fptr) != NULL)
{
if (strlen(maxLine) < strlen(currentLine))
{
strcpy_s(maxLine, currentLine);
}
}
printf("\nLongest line in file has %i symbols\nand its content is:%s", strlen(maxLine), maxLine);
((err = fopen_s(&fptr, fileNameWrite, "w")) != NULL); {
fprintf(fptr, "%s", maxLine);
exit(1);
}
fclose(fptr);
}

In c "" is an empty string terminated by null character 0.
Since char currentLine[100] is a local variable it is by default not initialized to zero and is filled with indeterminate values.
But if you initialize zero-th element to 0 or "" then c will initialize whole array with zeros.
example:
#include <stdio.h>
int main()
{
char arr0[100];
char arr1[100] = "";
char arr2[100] = {0};
for(int i=0; i<100; ++i){
printf("%d ", arr0[i]);
}
printf("\n");
for(int i=0; i<100; ++i){
printf("%d ", arr1[i]);
}
printf("\n");
for(int i=0; i<100; ++i){
printf("%d ", arr2[i]);
}
printf("\n");
}
Output:
0 0 0 0 0 0 0 0 -1 -75 -16 0 0 0 0 0 -62 0 0 0 0 0 0 0 -89 -82 55 -96 -4 127 0 0 -90 -82 55 -96 -4 127 0 0 85 -105 -84 -34 -92 127 0 0 1 0 0 0 0 0 0 0 101 88 74 28 -111 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 88 74 28 -111 85 0 0 -48 85 74 28 -111 85 0 0 -80 -81 55 -96
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Edit:
Also note that even if you set first eg. 3 elements to non-zero values char arr0[100] = {1,2,3} the rest of the elements will still be initialized to zero.

Assigning "" to an array null-terminates the array so it becomes a string.
It's like doing this:
char array[100] = { '\0' };
Note that this isn't needed at global scope because variables at global scope are already zero-initialized.

Related

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 0 0
... reallocating p[] to 26 bytes
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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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 0 0 0 0 0 0 0 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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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
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.

regExp in c: match a digit after <n> repetitions

i have a group of numbers that start with string, inside a string in C:
intr 250727985 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 238463729 0 0 0 0 0 8510 1009565 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 75963 0 0 0 0 0 0 0 0 0 0 0 0 0 6416543 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 29812 197 0 0 0 0 842664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 want to capture (match) only the 26th position of them (desired match: 1009565 on my return.)
i've tried this pattern:
(?:[0-9]+[[:space:]]){26}(?<![0-9])
but this is capturing whole string until the desired position.
How to achieve this with RegExp in C? someone could provide an sample source?
RegExp is the fastest (and lightest on system resources) way to do this? i need to repeat this operation various times in a second, all the uptime.
i'm confused on how to do this.
I believe using RegExp will make the problem more complicated, as suggested by others, using strtok is much easier.
You can parse the string at each space, and match the pattern you are trying to search for using strcmp from <string.h>.
Here is a basic idea of what you might want to use:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *string_search(char *string, unsigned position);
int main(void) {
char string[] = "250727985 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 238463729 0 0 0 0 0 8510 1009565 0 0 0 0 0";
char *check;
unsigned position = 26;
check = string_search(string, position);
if (check != NULL) {
printf("String %s found in position %d.\n", check, position);
} else {
printf("No string found in position %d.\n", position);
}
return 0;
}
char *string_search(char *string, unsigned position) {
char *number;
const char *delim = " ";
unsigned pos_count = 1;
number = strtok(string, delim);
while (number != NULL) {
if (pos_count == position) {
return number;
}
pos_count++;
number = strtok(NULL, delim);
}
return NULL;
}
The Unix strings library has strtok which divides a string into tokens given a delimiter. You should be able to simply iterate through the string until you get to the position you want.
A discussion on Stack Overflow of strtok is at Using strtok in c which has some sample code and gotchas.

What is wrong with my sudoku generate code in c?

That's my homework, making a sudoku game. I have done my algorithm but it's entering infinite loop. I didn't understand why.
I am trying create a random number and control it for find true number. Checking all columns and rows for find same number as like as our random number if it is, it's changing test number and if test has changed trying find another number for true number. Simple sudoku logic.
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int num, col, row, row2, col2, test = 0;
int sudo[9][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
do {
test = 0;
num = rand() % 9 + 1;
//control
for (col2 = 0; col2 <= 8; col2++) {
if (num == sudo[col2][row]) {
test++;
}
}
for (row2 = 0; row2 <= 8; row2++) {
if (num == sudo[col][row2]) {
test++;
}
}
} while (test > 0);
sudo[col][row] = num;
}
}
//print
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
printf(" %d ", sudo[col][row]);
if (col == 2 || col == 5) {
printf(" | ");
}
}
if (row == 2 || row == 5) {
printf("\n---------------------------------");
}
printf("\n");
}
}
Your algorithm is broken, and I can demonstrate why. If it were possible to fill in a sudoku puzzle this way, it would also be trivial to solve a sudoku puzzle this way, which it is not.
Essentially your code boils down to the following. I've added early exits on the inner for-loops to stop searching once we find the number already in the current row or column (and actually made sense of what 99.9% of the world thinks of concerning "rows" and "columns" in a NxN matrix):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NSIZE 9
void print_matrix(int const ar[][NSIZE])
{
for (size_t i=0; i<NSIZE; ++i)
{
for (size_t j=0; j<NSIZE; ++j)
{
fputc('0' + ar[i][j], stdout);
fputc(' ', stdout);
}
fputc('\n', stdout);
}
}
int main()
{
srand((unsigned)time(NULL));
int sudo[NSIZE][NSIZE] = {{0}};
int row, col;
for(row=0;row<NSIZE;++row)
{
for(col=0;col<NSIZE;++col)
{
int row2 = 0, col2 = 0, num;
printf("Trying ");
do
{
num = rand()%9+1;
printf("%d ", num);
for(row2=0; row2<NSIZE && num!=sudo[row2][col]; ++row2);
for(col2=0; col2<NSIZE && num!=sudo[row][col2]; ++col2);
}
while (row2 < NSIZE || col2 < NSIZE);
fputc('\n', stdout);
sudo[row][col] = num;
printf("sudo[%d][%d] = %d\n", row, col, num);
print_matrix(sudo);
}
}
}
As the loops progress, we report what number we're trying, and what the matrix looks like upon placement of a keeper. For example, a test run of the above initially can look like this:
Trying 8
sudo[0][0] = 8
8 0 0 0 0 0 0 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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 1 9 6 3 4 7 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 1 3 1 3 4 1 3 8 4 9 3 8 1 4 7 9 3 8 8 8 4 9 6 5
sudo[0][7] = 5
8 1 9 6 3 4 7 5 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 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 this continues for perhaps a while. But eventually, unless you get extraordinarily lucky, the following is bound to happen (and this one went pretty deep before the wheels fell off):
Trying 1 6 3 4
sudo[6][6] = 4
8 1 9 6 3 4 7 5 2
1 3 5 4 8 6 2 7 9
3 6 4 8 7 9 5 2 1
7 9 1 2 4 5 3 8 6
4 7 3 9 2 8 6 1 5
5 4 2 3 6 1 8 9 7
6 8 7 1 9 3 4 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Note we're about to try and populate sudo[6][7]. To do that we must find a number that is not in the sudo[r][7] column already, nor the sudo[6][c] row. But looking at the numbers already in those positions.
sudo[r][7] : {5,7,2,8,1,9}
sudo[6][c] : {6,8,7,1,9,3,4}
Therefore we're looking for a number from 1..9 that is NOT in: {1,2,3,4,5,6,7,8,9}, which we're NEVER going to find.
The algorithm is broken. There is a reason backtracking is used for tasks like this.

How to read in a specific number with sscanf from lines which are similar?

I'm trying to read in a specific number from a file with the sscanf()function in the C standard library. My example data comes from /proc/stat on a system running a Linux Kernel. Here is how it looks:
cpu 90158 11772 50095 6885572 36975 0 207 0 0 0
cpu0 22942 2975 12847 1720241 9655 0 58 0 0 0
cpu1 23879 2979 12080 1717405 12483 0 45 0 0 0
cpu2 21510 3105 12864 1722238 7790 0 57 0 0 0
cpu3 21824 2712 12301 1725687 7044 0 45 0 0 0
.
.
.
intr 2108705 19 28724 0 0 0 0 0 0 1 90871 0 0 204911 0 0 0 143 0 0 0 0 35 0 0 0 0 2362 0 101810 25 388 0 404786 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 11136028
btime 1423918994
processes 155184
procs_running 2
procs_blocked 1
softirq 2109698 8 644880 168 19330 95660 0 24557 551780 3897 769418
I try to print all lines after the if-statement except those lines containing cpu directly followed (without a whitespace) by a number. That is the first line cpu should be printed with all the others but e.g. not the second line cpu0. Furthermore, the number directly following cpu in these lines should be stored as an int into int cpu.
I really tried to get my head around this and I can at least get the correct lines to print by using character classes. My if-statement contains:
sscanf(line, "cpu%*1[^ ]%d", &cpu) != 1)
where %1[^ ] means read in the line until you encounter a single whitespace (the 1 is probably not needed) and then store the following number %d in int cpu. But the wrong values are stored for the cpuN-lines. Instead of storing 0, 1, 2, 3 the values 22942, 23879, 21510, 21824 are stored. Now this can be traced back to my usage of %1[^ ]. But I tried so many different things that I might be missing the obvious. How can I print all lines except the ones where cpu is followed (without a whitespace) by a number N and storing N in int cpu? (I'd like to avoid using regex.h if possible.) Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/proc/stat", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
int cpu;
if (sscanf(line, "cpu%*1[^ ]%d", &cpu) != 1) {
printf("%s", line);
}
}
fclose(fp);
if (line)
free(line);
exit(EXIT_SUCCESS);
}
You do not want to suppress the assignment of the digit after cpu. However, you also don't want to skip blanks, so you need to use either %c or %[] since all other formats (other than %n, which definitely doesn't count in this contex) skip leading blanks. That, in turn, means you need to read a character string, not an integer. So, the code should be:
char cpu_str[8]; // Allow for big machines!
if (sscanf(line, "cpu%7[^ ]", cpu_str) != 1)
printf("%s", line);
else if (sscanf(cpu_str, "%d", &cpu) != 1)
…oops: may the scanset should be %7[0-9]…
else
…cpu contains the cpu number…
suggest:
1) read line into buffer
2) strncmp (buffer, "cpu ") if 0, ignore line // notice trailing space
3) strncmp (buffer, "cpu" ) if 0, saved = atoi( buffer[3] ) // no trailing space
4) ignore all other lines

Unexpected data/unexpected segfault in a dynamically allocated array (for two dimensional use)

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.

Resources