Heap block error while freeing a pointer array [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 19 days ago.
Improve this question
I wrote this program that basically allocate a matrix of chars using pointer.
Here's the code:
char **bitmap;
void create_bitmap() {
int i;
int columns = (int) ceil(m / 8) + 1;
bitmap = (char **) malloc(sizeof(char) * n);
for (i = 0; i < n; i++)
bitmap[i] = (char *) calloc(columns, sizeof(char));
}
void free_bitmap() {
int i;
for (i = 0; i < n; i++) {
free(bitmap[i]);
}
free(bitmap);
}
The heap block error comes out when at the last instruction of the free_bitmap() procedure.
I just can't figure out what triggers the error.
Thanks in advance.

The error was simple and I guess this is the final proof that I am not focused this morning.
In the procedure create_bitmap(), the malloc istruction would have been written:
bitmap = (char **) malloc(sizeof(char *) * n);
Now, this istruction will allocate n char* pointers, instead of only a char.
Pardon.

Related

Define and initialize an array of strings in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im trying to define and initialize an array of strings using a function, but, the function is causing segmentation fault while allocating memory for each of the pointers.
Please find below the minimal reproducible version of the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COL 100
#define ROW 10
int init_arr(char ***arr_ptr) {
*arr_ptr = malloc(sizeof(char *) * ROW);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
*arr_ptr[temp_iter] = malloc(COL + 1);
strncpy(*arr_ptr[temp_iter], "MY_STRING_IS_THIS", COL);
}
return 0;
}
int main() {
char **arr_of_str = NULL;
init_arr(&arr_of_str);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
printf("\nData: %s", arr_of_str[temp_iter]);
}
}
This code works fine when the function is split into 2 where the first function defines it and 2nd one initializes it.
The problem lies here *arr_ptr[temp_iter], first dereference(in his case a subscription) that was applied was from box brackets, and then from asterix. What you want to do is separate these dereferences, so that asterix is applied first and the from box brackets, like this:
int init_arr(char*** arr_ptr) {
*arr_ptr = (char**) malloc(sizeof(char *) * ROW);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
(*arr_ptr)[temp_iter] = (char*) malloc(COL + 1);
strncpy((*arr_ptr)[temp_iter], "MY_STRING_IS_THIS", COL);
}
return 0;
}
Also, you need to cast the pointer from malloc into appropriate one, since the malloc always returns void*, and you are dereferencing void pointer later on.

Not getting the correct output elements from an array in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to write a code that inputs a number each time into an array and than print out the result from the arrays elements but for some reason i either get an infintie loop or it prints out the same number.
void main() {
char arr[SIZE];
int k = 1;
int i = 0;
while (k != 0) {
scanf("%d", &k);
arr[i] = k;
i++;
}
arr[i] = '\0';
int b = 0;
while (b < i) {
printf("elements are %d\n", arr[i]);
b++;
}
You want to print arr[b] and not arr[i].
Thus, you want: printf("elements are %d\n", arr[b]);
As you [currently] have it, printing arr[i] will always print the same element and it's UB because at that point i is one beyond the end of the arr array, so the value will be unknown/undefined.

rand() doesn't give me random numbers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to print 1000 random numbers saved in a array. Numbers have to be between 1 and 10000.
I put srand(time(NULL)) in my main function and the array have to be filled with random numbers in my init function. The ausgabe function is for formatted output.
But rand fills my array with numbers all in row.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 1000
void init(int * ptr, int size){
for (int i = 0; i < size; i++){
*(ptr + i) = (rand() %10000+1);
}
}
void ausgabe(int * ptr, int size){
for (int i = 0; i < size; i++){
printf("%5i\t", * ptr + i);
if ((i + 1) %10 == 0){
printf("\n");
}
}
printf("\n\n");
}
int main(){
int zufall[ARR_SIZE];
srand(time(NULL));
init(zufall, ARR_SIZE);
printf("\n\t\t\t---unsortierte Ausgabe---\n\n");
ausgabe(zufall, ARR_SIZE);
return 0;
}
* ptr + i is (*ptr)+i, not *(ptr+i). You need to be more careful with operator precedence. (And to learn to use your debugger: 30 seconds in your debugger would have clearly revealed that the problem was the printing, not the initialization.)

Copying data to an integer pointer array through loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am facing issue copying data to void pointer array through for loop. I am not allowed to use double pointer.
I am trying someting like this:
for(i=0;i<9;i++){
array + i = i;
}
but I cant just do this, and I know that,
I am trying to do it like this
memcpy(
array+i,
i,
s
);
I have already allocated memory:
My actual code is some thing like this:
if(v->e_type==V_INT){
// printf("%p ",v->e_array+v->no_e);
memcpy(
v->e_array+v->no_e,
new_val,
v->e_sz
);
}
If you have something like this:
int *x = malloc(sizeof(int)*4);
You can put values in it with pointer arithmetic:
for(int i = 0; i < 4; i++) {
*(x+i) = i; // or *(x+1) = 0; for example
}
Or simply:
for(int i = 0; i < 4; i++) {
x[i] = i;
}

OpenMp only one thread executes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have set 4 threads.Screen1 But only one threads executes in parallel region (look at "id"). Screen2.
Available threads (I used omp_get_max_threads()).
What is wrong?
int main(int argc, char** argv)
{
srand(time(NULL));
int P = atoi(argv[1]);
omp_set_num_threads(4);
#pragma parallel for
for( int i = 0; i < 1000000; i++ ) {
printf("%d\n",omp_get_max_threads());
printf("Num Threads:%d ",omp_get_num_threads());
printf("id:%d\n",omp_get_thread_num());
}
while(1);
const uint num_elements = 79;
int* data = (int*) malloc(sizeof(int) * num_elements);
for( uint i = 0; i < num_elements; i++ ) {
data[i] = rand() % 100 - 50;
}
int* buffer = (int*) malloc(sizeof(int) * num_elements);
PrintArray(data,0,num_elements);
int* res = MergeSort(data,buffer,0,num_elements - 1,8);
PrintArray(res,0,num_elements);
return 0;
}
The error was: #pragma parallel for.
Correct instruction is "#pragma omp parallel for"
printf("%d\n",omp_get_max_threads());
printf("Num Threads:%d ",omp_get_num_threads());
printf("id:%d\n",omp_get_thread_num());
Please place these three printf into one printf. Easy. Then remove {} the brackets from the for loop,
and it works, I tested. It's just that I am writing here from my smartphone. I am going to add to github the basic for sample later.

Resources