Dynamically allocating 2d array in c error - c

My problem is prettty simple, I wanna allocate memory for a 2d array in c, fill it with -1, then free it and exit the program. My code keeps crashing and I dont know what I am doing wrong...
This is what I got:
int main(){
int i,j;
char str1[]="xxxabxcxxxaabbcc";
char str2[]="abc";
int len1=strlen(str1);
int len2=strlen(str2);
printf("%d %d",len1,len2);
//allocate 2d_array
int **H_table = (int**)malloc((len1+1)*sizeof(int));
for (i=0; i<len1+1; i++){
H_table[i] = (int*)malloc((len2+1)*sizeof(int));
}
//fill and print 2d array
for(i=0;i<len1+1;i++){
for(j=0;j<len2+1;j++){
printf("i:%d j:%d",i,j);
H_table[i][j]=-1;
printf(" value:%d\n",H_table[i][j]);
}
}
// free 2d array
for(i=0;i<len1;i++){
free(H_table[i]);
}
free(H_table);
return 0;
}
So what happens is that I wanna allocate an array that has 1 extra line and 1 extra column than the 2 strings if you put them vertically compared to each other.
And this is what I expected( the things in bracets are obviously not part of the table, I put the there for comparison):
(x x x a b x c x x x a a b b c c)
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
a)1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
b)1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
c)1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The problem is that the code crashes when it fills the table, and it always crashes for i=9 and j=3, for those specific strings. The weird part is that if you swap the 2 strings(put "abc" in str1) then the code passes the filling stage, and crashes when it tries to free the array.
Sorry for any grammar mistakes or stackoverflow mistakes, I am kinda new here :P
Any idea is welcome :) thx in advance

As a number of people have pointed out, you're allocating H_table with room for len1 + 1 integers but it is actually supposed to be an array of len1 + 1 pointers (to integers). Since pointers are bigger than integers (on your system, anyway), you end up with undefined behaviour from a buffer overrun.
Here's a hint. Avoid this problem, and a variety of other similar issues, by always using the following model for malloc:
some_variable = malloc(n * sizeof *some_variable);
For example:
int** H_table = malloc((len1 + 1) * sizeof *H_table);
for (int i = 0; i <= len1; ++i)
H_table[i] = malloc((len2 + 1) * sizeof *H_table[i]);
That is, let the compiler figure out the right type for the variable (or lvalue). The compiler is less prone to typos than you are, and not writing the type explicitly will make it a lot easier for you to later decide that H_table should have been long or short or unsigned.
For the same reason, don't explicitly cast the return value of malloc. C automatically casts void* to the destination type, and does not provide an error if you manually cast to the wrong type. So just let the compiler do it; it's less typing, safer, and more future-proof.
Note that if you use an expression with sizeof, the compiler does not evaluate the expression [Note 1]. It just figures out the type and substitutes that for the expression. So don't worry about extra evaluation: there isn't any. That's also why it's ok to use this model with declarations, even though some_variable doesn't yet have a value when the malloc is executed.
Notes:
There is one circumstance in which the compiler might evaluate ex in sizeof ex: if ex is a variable-length array. However, in this case ex is always a pointer, so that case cannot apply.

As #xing mentioned in his comment, H_table is a pointer to pointer to integer. so you need to change the int to int* in the first malloc.
here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i,j;
char str1[]="xxxabxcxxxaabbcc";
char str2[]="abc";
int len1=strlen(str1);
int len2=strlen(str2);
printf("%d %d",len1,len2);
//allocate 2d_array
int **H_table = (int**)malloc((len1+1)*sizeof(int*));
for (i=0; i<len1+1; i++){
H_table[i] = (int*)malloc((len2+1)*sizeof(int));
}
//fill and print 2d array
for(i=0;i<len1+1;i++){
for(j=0;j<len2+1;j++){
printf("i:%d j:%d",i,j);
H_table[i][j]=-1;
printf(" value:%d\n",H_table[i][j]);
}
}
// free 2d array
for(i=0;i<len1;i++){
free(H_table[i]);
}
free(H_table);
return 0;
}

Related

Integer value changes during insertion to array

I am having some weird problems with my code and I have no idea what I'm doing wrong. I'm relatively new to C-language, so I'd really appreciate if someone could point out if I'm doing something stupid.
I'm trying to read data from a file into two dimensional array of type state. Type state is defined as follows:
typedef uint8_t state [4][4];
I have defined method read_block that gets the file pointer as parameter and returns pointer two dimensional type state array which contains first 16 bytes from that file.
state* read_block(FILE *fp) {
state *arr = (state*) malloc(sizeof (state));
uint8_t *temp = (uint8_t*) malloc(sizeof (uint8_t)*16);
fread(temp, 16, 1, fp);
/*
for (int y = 0; y < 16; y++) {
printf("%u ", temp[y]);
}
printf("\n");
*/
int n = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
/*
printf("%u ", temp[n]);
*/
*arr[i][j] = temp[n];
n++;
}
}
return arr;
}
Code compiles and runs ok, but the type state array that method returns always contains one wrong value. Specifically, the value in position [3][0] is always wrong.
The test file I'm trying to read contains numbers:
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
I decided to check if the values are read right and if I remove comments around printf lines I get as output:
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
0 1 2 3 0 1 2 3 0 1 2 3 3 1 2 3
In which the first line is exactly correct (that is printed right after fread()), but for some reason the second line is incorrect (that is printed when inserting values into arr).
I can't figure out what is causing this. I've also tried reading values one byte at time with fread, but the same value keeps being wrong. I'm using NetBeans, so I'm not sure if this is somehow being caused by it.
You're running into operator precedence.
*arr[i][j]
is the same as
*(arr[i][j]) // or arr[i][j][0]
This is the same only if i == 0, i.e. in the first line.
What you want is
(*arr)[i][j]
It is not really a good idea to hide an array behind a typedef. This is the root of the problem, since arr is actually an array pointer of type uint8_t (*)[4][4]. And when de-referencing an array pointer you have to first take the contents of the pointer to get the array, then after access a specific index. That is, you will have to do (*arr)[i][j] rather than *arr[i][j].
To avoid that fishy syntax in turn, you could use a trick and declare an array pointer to the outermost dimension: uint8_t(*)[4]. Then you can suddenly type arr[i][j] instead. arr[i] then means do pointer arithmetic on a uint8_t(*)[4] type, and then in the obtained result you can access a specific array index, [j].
In addition to the operator precedence issue, you probably want to store data directly into the array. There's no reason why you can't do fread(arr, 16, 1, fp);, since you are using that array in the same order as it is allocated.
I haven't tested it, but you should be able to replace the whole function with this:
void read_block(FILE* fp, uint8_t (*arr)[4][4])
{
const size_t size = sizeof(uint8_t[4][4]);
*arr = malloc(size);
fread(*arr, size, 1, fp);
}

Why is the seventh variable 6 in my program?

#include <stdio.h>
int main(void) {
int n,i;
int str[n];
scanf("%d\n",&n);
for(i=0;i<=n;i++)
{
scanf("%d",&str[i]);
printf("%d th %d\n",i,n);
}
return 0;
}
Input:
10
8 9 2 1 4 10 7 6 8 7
Output:
0 th 10
1 th 10
2 th 10
3 th 10
4 th 10
5 th 10
6 th 10
7 th 6
Why is 6 in the output?
This is really strange code with undefined behavior. What do you expect this:
int n; // No value!
int str[n];
To do? You get an array whose length is unknown since n has no value at the point of the str declaration.
If you expected the compiler to "time-travel" back to the str[n] line magically when n is given a value by scanf(), then ... that's not how Co works, and you should really read up on the language a bit more. And compile with all warnings you can get from your environment.
As an extra detail, even if it were fixed so that n had a value, the for loop overruns the array and gives you undefined behavior again.
For an array of size m, the loop header should read
for (size_t i = 0; i < m; ++i)
Since indexing is 0-based, you cannot index at m, that's outside the array.
In your code
int n,i;
int str[n];
you're using n while it has indeterminate value, unitialized. It invokes undefined behavior.
To elaborate, n being an automatic local variable, unless initialized explicitly, contains indeterminate value.
Solution: You need to define int str[n]; after you has taken the value from user (and sanitized).
After that, there's once more issue, your loop runs off-by-one for the array. C uses 0-based array indexing, so, for an array of size n, the valid indexes will be 0 to n-1. You loop construct should be
for(i=0; i<n; i++)
The value for the size of the string str[] is not yet defined. You need to have initialized this value before declaring a string/array.
If you're using gcc to compile, try including the -Wall flag in your compile line to catch errors like this.

iterating multi-dimensional arrays in C without knowing index

I'm having a problem processing a multidimensional array. I'm trying to get "123" "456" "x123" and "x456" to appear on screen inside a function using pointers:
void f(char ***array){
while (**array != '\0'){
while (*array != '\0'){
printf("%s\n",*array);array++;
}
}
}
int main(){
char* arr[50][50]={{"123","456"},{"X123","X456"}};
f(arr);
return 0;
}
When compiling, I receive the warning passing argument 1 of 'f' from incompatible pointer type at the line f(arr);. and when running the code, I see:
123
456
Segmentation fault
and the program exits.
When I change my code to this:
void f(char **array){
while (*array != '\0'){
printf("%s\n",*array);array++;
}
}
int main(){
char* arr[50]={"123","456"};
f(arr);
return 0;
}
The numbers iterate fine, but I'd rather group my data into sets at some point for better organization. Why does the first set of code with multidimensional array not execute properly?
First, why three stars? What is it that you are trying to accomplish?
The obvious solution is to create a two-dimensional array of characters, then store the string in the array, one per row. Consider the following example:
char arr[][ 6 ] = { "123", "456", "X123", "X456" };
Note that we are allowed to omit the number of rows in the arr array, but C requires that we specify the number of columns. Unfortunately, not all the strings are long enough to fill an entire row of the array, so C padded them with null characters. ( Note that there is a bit of wasted space in the array )
0 1 2 3 4 5
+-----+-----+-----+-----+-----+-----+
0 | 1 | 2 | 3 | '\0'| '\0'| '\0'|
+-----+-----+-----+-----+-----+-----+
1 | 4 | 5 | 6 | '\0'| '\0'| '\0'|
+-----+-----+-----+-----+-----+-----+
2 | X | 1 | 2 | 3 | '\0'| '\0'|
+-----+-----+-----+-----+-----+-----+
3 | X | 4 | 5 | 6 | '\0'| '\0'|
+-----+-----+-----+-----+-----+-----+
If having three stars in your code is what you want, then you will have to add one additional element which is always NULL. ( By the way, why don't you want to use the length of the array? )
Another way is to use a ragged array. C doesn't provide a "ragged array type", but it does give us the tools to simulate one. ( just create an array whose elements are pointers to strings ) Consider the following example:
#define N 4
int main( int argc, const char * argv[] ) {
char *str[] = { "123", "456", "X123", "X456" };
for ( char **p = &str[ 0 ]; p < str + N; ++p ) {
puts( *p );
}
return EXIT_SUCCESS;
}
To access one of the strings, all we need is subscript the arr array.
Hope this helps..
It is hard to tell what you are attempting to do, but if I understand correctly, you appear to have an additional array dimension that isn't required. You are testing an uninitialized value as soon as you reach the 5th string (leading to your segfault). To correct it, you could do something like:
#include <stdio.h>
void f(char **array){
while (*array != '\0'){
// while (*array != '\0'){
printf("%s\n",*array);array++;
//}
}
}
int main(){
char *arr[50]={"123","456","X123","X456",NULL};
f(arr);
return 0;
}
Output
$ ./bin/func_f
123
456
X123
X456
Note: an explicit NULL is used as a sentinel to stop iterating when the data is exhausted. There are a number of ways to handle this, this being only one.
You generally shouldn't use pointers and arrays interchangeably. The common knowledge is that they're the same, but they aren't. This is particularly true for multi-dimensional arrays.
Multidimensional arrays have to be laid out linearly in memory, since addresses are linear. There are a couple ways to do this, but C uses row-major ordering. What this means is that the last index increases fastest as addresses increase.
For example, a two dimensional array
int x[rows][cols];
the two statements would be equivalent
x[row][col] = y;
*(x + (row * cols) + col) = y;
What this means is that in order to access the elements of your multi-dimensional array in a function, the function needs to know at least the sizes of the higher dimensions to validly access.
void f(int rows, int cols, char* array[rows][cols]){
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%s\n", array[i][j]);
}
}
}
int main(){
char* arr[50][50]={{"123","456"},{"X123","X456"}};
f(50, 50, arr);
return 0;
}
However, if it is necessary to iterate without knowing the dimensions, you can take advantage of the memory layout to effectively iterate over rows and columns (and higher dimensions). Although, this adds the necessity for an entry in the array that signals an end (e.g. NULL). Aside from making the code more complex, it adds memory overhead since the last row containing the NULL generally must be allocated.
#include <stdio.h>
void f(char** array){
while (*array)
printf("%s\n", *(array++));
}
int main(){
char* arr[][2] = { {"123","456"}, {"X123","X456"}, {NULL, NULL} };
f((char**) arr);
return 0;
}

Matrix with variables in C

I'm going to write in pseudo code to make my question more clear. Please keep in mind this code will be done in C.
Imagine I have an array of any amount of numbers. The first number tells me how big of an array we're dealing with. For example, if my first number is 3, it means I have two 3x3 matrices. So I create two multidimensional arrays with:
matrix1[3][3]
matrix2[3][3]
What I'm having a hard time with is the arithmetic/coding to assign all the numbers to the matrices, I'm having a very hard time visualizing how it would be done.
Imagine a test array contains [2,1,2,3,4,5,6,7,8]
My program should now have two matrixes with:
1 2 5 6
3 4 7 8
Do I need several nested loops? Any help would be appreciated.
At the moment the only idea i get is using two for loops. Or you can make a function and call it every time you need (but don't forget to use k as second argument).
int i, j, k;
/* We start in the 2nd element of the array that's why k = 1. */
k = 1;
/* Now we fill the array1 copying 1 by 1 the elements of the "test array" until
we fill it. Then we do the same with the array2. */
for( i = 0; i < test[ 0 ]; i++ ){
for( j = 0; j < test[ 0 ]; j++ ){
array1[ i ][ j ] = test[ k ]
k++;
}
}
for( i = 0; i < test[ 0 ]; i++ ){
for( j = 0; j < test[ 0 ]; j++ ){
array2[ i ][ j ] = test[ k ]
k++;
}
}
Your data is presented in row-major order. After reading your integer array and validating the content (i.e. the dim=4 means 32 values follow, dim=2 means 8 values follow, etc.) I'm not sure why you want to allocate or loop anything.
I.e. You can use your physical test[] data as the matrices:
int dim = test[0];
int (*mat1)[dim] = (int (*)[dim])(test+1);
int (*mat2)[dim] = (int (*)[dim])(test+1 + dim*dim);
C99 supports variable array declarations at the implementation level (i.e. the compiler can support the feature as it is defined by the standard, but does not have to; see 6.7.6.2 of the C99 standard for more info). If your toolchain does NOT support it, then a predefined macro, __STDC_NO_VLA__ must be defined and can be tested at compile time (see section 6.10.8.3-1 of the C99 standard). That being said, every C99-compliant compiler I've ever used in the last decade-plus does support it, so if your's does not, tell us below in a comment.
If it does, then pay note to the use of 'dim' in the declarations of mat1 and mat2 above). It is one of the few features of C I like that C++ does not have. So dance with the one you brought.
Finally, assuming your compiler is C99 compliant and supports VLAs (__STDC_NO_VLA__ is NOT defined), as an extra super-special bonus it is all-but-guaranteed to be the fastest algorithm to get your two matrices, because there is no algorithm. You read one array element, then assign two pointers. O(3) is hard to beat.
Example
#include <stdlib.h>
#include <stdio.h>
// main loader.
int main(int argc, char *argv[])
{
int test[] = {2,1,2,3,4,5,6,7,8};
int dim = test[0];
int (*mat1)[dim] = (int (*)[dim])(test+1);
int (*mat2)[dim] = (int (*)[dim])(test+1 + dim*dim);
// proof stuff is where it should be.
int i=0,j=0;
for (i=0;i<dim;i++)
{
for (j=0;j<dim;printf("%d ", mat1[i][j++]));
printf (" ");
for (j=0;j<dim;printf("%d ", mat2[i][j++]));
printf("\n");
}
return EXIT_SUCCESS;
}
Output
1 2 5 6
3 4 7 8
A similar test with a 3x3 data set:
int test[] = {3,1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1};
Output
1 2 3 9 8 7
4 5 6 6 5 4
7 8 9 3 2 1
And finally, a 4x4 data set:
int test[] = {4,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1,8,7,6,5,4,3,2,1};
Output
1 2 3 4 8 7 6 5
5 6 7 8 4 3 2 1
1 2 3 4 8 7 6 5
5 6 7 8 4 3 2 1
The problem with multidimensional arrays in C is that you need to know in advance (at compile time) n-1 of the dimension sizes, they are also a drag when used as function parameters.
There are a couple of alternate approaches:
Creating an array of arrays. i.e. allocating an array of array pointers and then allocating arrays to those pointers.
type **array = malloc(sizeof(type * ) * < firstnumread > );
array[0] = malloc(sizeof(type) * < firstnumread > );
...
Allocating a single dimension array with the size of all the multiplied dimensions. i.e.
type *array = malloc(sizeof(type) * < firstnumread > * < firstnumread >);
In your case, the second is probably more appropiate. Something like:
matrix1 = malloc(sizeof(type)*<firstnumread>*<firstnumread>);
matrix2 = malloc(sizeof(type)*<firstnumread>*<firstnumread>);
Then you can assign values like this:
matrix1[column*<firstnumread> + row] = <value>;
Yes, with 2 for loops.
2D arrays are stored in continuous series of lines from the matrix. So you doesn't even need to allocate new memory you can use your original array. Anyway you can create 2 new standalone array too.
You can crate a function like this, to get the correct number of the matrix.
int getNumber(int array[], int arraynumber, int index_x, int index_y)
{
return array[(((array[0]*index_x)+index_y)+1)+((array[0]*array[0])*arraynumber)];
}
The arraynumber variable is 0 for the first and 1 for the second matrix. This funciton works only if all parameters are correct, so ther is no error detection.
With this function you can easily loop through and create 2 new arrays:
int i,k;
for (i=0; i<array[0]; i++)
{
for (k=0; k<array[0]; k++)
{
newarray1[i][k] = getNumber(array, 0, i,k);
newarray2[i][k] = getNumber(array, 1, i,k);
}
}
Here is something that works in a single loop; no nests and no repeats. I don't know if it'll outperform other answers, but I just felt like giving you a different answer ^_^
I have not tested this code, but it looks like the logic of the algorithm works - that's the point, right? Let me know if it has any errors....
int c=0, x=0, y=0, size=test[0], length=sizeof(test);
for(i=1; i<length; i++) {
if((c-size)<0) {
matrix1[x][y] = test[i];
} else {
matrix2[x][y] = test[i];
}
++y;
if(y%size == 0) {
++c;
y = 0;
x = (c-size)<0 ? ++x : 0;
}
}

Does making the iterator a pointer speed up a C loop?

I ran the following:
#include <stdio.h>
typedef unsigned short boolean;
#define false 0
#define true (!false)
int main()
{
int STATUS = 0;
int i = 0;
boolean ret = true;
for(i = 0; i < 99999; i++)
{
ret = ret && printf("Hello, World.");
}
if(!ret)
{
STATUS = -1;
}
return STATUS;
}
It completes in just under a second. Typically 0.9 - 0.92.
Then I changed int i = 0; to int *i = 0; and now I am getting execution times under 0.2 seconds. Why the speed change?
Your runtime is dominated by the time required to print to the console. i++ on an int* will increment the pointer by the size of a pointer. That will be either 4 or 8 depending on your computer and compiler settings. Based on the numbers you report, presumably it would be 4. So printf is executed only a quarter as many times.
Typically, printing to a console will be several orders of magnitude larger than any gain with micro optimization you could do to such a loop.
Are you really sure your second version prints hello world 99999 times as well ?
When you're doing for(int *i = 0; i++ ; i < 99999 ) , you're cheking if the pointer value(an address) is less than 99999, which doesn't normally make a lot of sense. Incrementing a pointer means you step it up to point at the next element, and since you have an int*, you'll increment the pointer by sizeof(int) bytes.
You're just iterating 99999/sizeof(int) times.
Your comment on nos's answer confirmed my suspicion: it's pointer arithmetic. When you increment an int pointer using ++, it doesn't just add one to the number, but it actually jumps up by the size of an integer, which is usually 4 (bytes). So i++ is actually adding 4 to the numeric value of i.
Similarly, if you use += on a pointer, like i += 5, it won't just add 5 (or whatever) to the numeric value of i, it'll advance i by the size of that many integers, so 5*4 = 20 bytes in that case.
The reasoning behind this is that if you have a chunk of memory that you're treating as an array,
int array[100]; // for example
you can iterate over the elements in the array by incrementing a pointer.
int* i = array;
int* end = array + 100;
for (i = array; i < end; i++) { /* do whatever */ }
and you won't have to rewrite the loop if you use a data type of a different size.
The reason is because the increment operates differently on pointers.
On ints, i++ increments i by 1.
For pointers, i++ increments by the size of the pointed-to object, which will be 4 or 8 depending on your architecture.
So your loop runs for only 1/4 or 1/8 of the iteration count when i is a pointer vs when i is an int.
The correct way to do this test with a pointer would be something like:
int i;
int *i_ptr = &i;
for (*i_ptr = 0; *i_ptr < 99999; *i_ptr++) {
...

Resources