I would like to reserve memory for 3 int arrays in C. All are int types.
Array a is size n, array b is size m and array c is size m.
I have following idea:
void *c;
int *a;
int *b;
int *m;
m = malloc((n + m + m +1) * sizeof(int));
a = n;
b = a + m;
c = b + m;
free(m);
When I try to acces to some of them using syntax for example
a[i] =
I got segmentation fault error.
Here is complete code:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void *c;
int *dretve;
int *stol;
int *rez;
int n;
int m;
void *Rezerviraj(void *x){
int c = *((int*)x);
printf("Ušo u funkciju rezerviraj\n");
// sleep(10);
printf("Gotov sam!");
}
int Provjeri(){
int i;
// for(i = n; i < m+n; i++)
// if(stol[i] == 1)
return 0;
return 1;
}
int main(int argc, char *argv[]){
n = atoi(argv[1]);
m = atoi(argv[2]);
int f = 4;
int i = 0;
pthread_t thr_id[2];
c = malloc((n + m + m + 1) * sizeof(int) + n * sizeof(pthread_t));
dretve = n;
stol = dretve + m;
rez = stol + m;
for(i = 0; i < n; i++)
printf("%d ", dretve[i]);
pthread_create(&thr_id[1], NULL, Rezerviraj, &f);
pthread_join(thr_id[1],NULL);
// pthread_create(&thr_id[1], NULL, Rezerviraj,&f);
// pthread_join(thr_id[1],NULL);
// free(c);
return 0;
}
Can someone explain me what is mistake and how can I fix it?
Many thanks!
To allocate memory for 3 arrays of the same type,
int *a_array;
size_t a_count = foo();
int *ba_array;
size_t b_count = foo();
int *c_array;
size_t c_count = foo();
a_array = malloc(sizeof *a_array * (a_count + b_count + c_count));
b_array = a_array + a_count;
c_array = b_array + b_count;
// code uses a_array, b_array, c_array
...
// When done with all 3, only 1 free() call
free(a_array);
Related
This question already has answers here:
Changing address contained by pointer using function
(5 answers)
Closed 2 years ago.
A C function can modify more than one variable by an illusion of pass-by-reference (a pass-by-value of address as explained by Ely), e.g.:
#include <stdio.h>
void function(int *pa, int *pb) {
*pa *= *pa;
*pb *= *pb;
}
int main(void) {
int a = 1, b = 2;
function(&a, &b);
printf("a = %d\nb = %d\n", a, b);
return 0;
}
which outputs
a = 1
b = 4
It is also possible to modify a whole range of variables by returning a pointer to an array, e.g.:
#include <stdlib.h>
#include <stdio.h>
int *function(int *ptr_size) {
int n = 6; // arbitrary ptr_size
int *array = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; ++i)
array[i] = i * i;
//
*ptr_size = n;
return array;
}
int main(void) {
int size = 0;
int *array = function(&size);
printf("size = %d\n", size);
for (int i = 0; i < size; ++i)
printf("array[%d] = %d\n", i, array[i]);
free(array);
array = NULL;
return 0;
}
which outputs :
size = 6
array[0] = 0
array[1] = 1
array[2] = 4
array[3] = 9
array[4] = 16
array[5] = 25
But what if I want a function that modify more than one (dynamic allocated) array ?
I tried this
#include <stdlib.h>
#include <stdio.h>
void function(int *array, int *ptr_asize, int *brray, int *ptr_bsize) {
int size = 6;
array = (int *)malloc(size * sizeof(int));
brray = (int *)malloc(size * sizeof(int));
for (int i = 0; i < size; ++i) {
array[i] = i * i;
brray[i] = i * i * i;
}
*ptr_asize = size;
*ptr_bsize = size;
}
int main(void) {
int asize, bsize;
int *array, *brray;
function(array, &asize, brray, &bsize);
// array
printf("asize = %d\n", asize);
for (int i = 0; i < asize; ++i)
printf("array[%d] = %d\n", i, array[i]);
free(array);
array = NULL;
// brray
printf("bsize = %d\n", bsize);
for (int i = 0; i < bsize; ++i)
printf("brray[%d] = %d\n", i, brray[i]);
free(brray);
brray = NULL;
//
return 0;
}
but it makes a segmentation fault.
That is not very surprising, how main would know about how much memory has been allocated to array and brray?
So my question is: is it possible in C that a function allocate and modify more than one array, and those changes remain in main?
PS: A solution would be to allocate a new abrray that contains both array and brray (int **function(...) { ... return abrray; }), but I would like to know if it is possible to a function to modify two (or more) arrays, and that changes remain in main.
Pass a pointer to pointer, like this:
void func(int** array, int** brray, size_t size_a, size_t size_b) {
*array = malloc(size_a * sizeof(int));
*brray = malloc(size_b * sizeof(int));
}
Call it like:
int *arr, *brr;
func(&arr, &brr, 2, 5);
As explained by iTs.SL4y3r, it is possible by passing pointer to pointer.
Here is a minimal working example :
#include <stdlib.h>
#include <stdio.h>
void function(int **array, int *ptr_asize, int **brray, int *ptr_bsize) {
int size = 6;
*array = malloc(size * sizeof(int));
*brray = malloc(size * sizeof(int));
for (int i = 0; i < size; ++i) {
(*array)[i] = i * i;
(*brray)[i] = i * i * i;
}
*ptr_asize = size;
*ptr_bsize = size;
}
int main() {
int asize, bsize;
int *array, *brray;
function(&array, &asize, &brray, &bsize);
// array
printf("asize = %d\n", asize);
for (int i = 0; i < asize; ++i)
printf("array[%d] = %d\n", i, array[i]);
free(array);
array = NULL;
// brray
printf("bsize = %d\n", bsize);
for (int i = 0; i < bsize; ++i)
printf("brray[%d] = %d\n", i, brray[i]);
free(brray);
brray = NULL;
//
return 0;
}
which outputs :
asize = 6
array[0] = 0
array[1] = 1
array[2] = 4
array[3] = 9
array[4] = 16
array[5] = 25
bsize = 6
brray[0] = 0
brray[1] = 1
brray[2] = 8
brray[3] = 27
brray[4] = 64
brray[5] = 125
Can someone tell me why do I get this segmentation fault?
Is it because I have set a pointer to NULL and passed it to a function?
Should I use arrays or pointers when I want to send an array to function?
#include <stdlib.h>
#include <stdio.h>
int* ComputeFibo(int _size);
void PrintFibo(int* _arr, int _size);
int main (int argc, char* argv[])
{
int* arr = NULL;
int n = 0;
printf("Please enter Fibonacci size:");
scanf("%d", &n);
arr = ComputeFibo(n);
PrintFibo(arr, n);
return 0;
}
int* ComputeFibo(int _size)
{
int sum = 0;
int indx = 0;
int* arr = NULL;
arr[indx] = 1;
for (indx = 1; indx < _size; ++indx)
{
arr[indx] = arr[indx - 1] + sum;
sum = arr[indx - 1];
}
return arr;
}
void PrintFibo(int* _arr, int _size)
{
int indx = 0;
for (; indx < _size; ++indx)
{
printf("%d\t", _arr[indx]);
}
}
I would love to get an answer with a good explanation for that.
The problematic statement is:
int* arr = NULL;
where arr is pointing at NULL and then you assign values to arr as if it's pointing to valid memory location.
You could fix it with:
int* arr = malloc(_size * sizeof *arr);
if (arr == NULL) { /* error *}
and then call free() on the returned pointer later.
arr = ComputeFibo(n);
PrintFibo(arr, n);
free(arr);
Also be aware that int can only represent limited values in C. So, for example, if you try to calculate upto 100 Fibonacci numbers, you are likely to run into integer overflow.
You tried to access index of a null pointer.
int* ComputeFibo(int _size)
{
int* arr = NULL;
arr[indx] = 1; //SIGF
...
}
I fixed your code if you want a reference.
#include <stdlib.h>
#include <stdio.h>
void compute_fibo(int *_arr, int _size);
void print_fibo(int *_arr, int _size);
int main(int argc, char *argv[]) {
int n = 0;
printf("Please enter Fibonacci size:");
scanf("%d", &n);
int *arr = (int *) malloc(sizeof(int) * n);
if (arr == NULL) return 1;
compute_fibo(arr, n);
print_fibo(arr, n);
free(arr);
return 0;
}
void compute_fibo(int *_arr, int _size) {
_arr[0] = 1;
_arr[1] = 1;
for (int index = 2; index < _size; index++) {
_arr[index] = _arr[index - 1] + _arr[index - 2];
}
}
void print_fibo(int *_arr, int _size) {
for (int index = 0; index < _size; ++index) {
printf("%d\t", _arr[index]);
}
}
output:
Please enter Fibonacci size:10
1 1 2 3 5 8 13 21 34 55
I have code example which sorts 3d string array.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
static char * rand_string(size_t ssize)
{
char * str = malloc(sizeof(char) * ssize);
const char charset[] = "abcdefghijklmnopqrstuvwxyz";
if (ssize) {
--ssize;
for (size_t n = 0; n < ssize; n++) {
int key = rand() % (int) (sizeof charset - 1);
str[n] = charset[key];
}
str[ssize] = '\0';
}
return str;
}
char *** init_array(char *** array, int n_rows, int n_cols, int ssize)
{
int i, j;
array = malloc(n_rows * sizeof(char **));
for (i = 0; i < n_rows; i++)
{
array[i] = malloc(n_cols * sizeof(char *));
for (j = 0; j < n_cols; j++)
array[i][j] = malloc(ssize * sizeof(char));
}
return array;
}
void gen_array(char *** array, int n_rows, int n_cols, int ssize)
{
int i, j, k;
for (i = 0; i < n_rows; i++)
for (j = 0; j < n_cols; j++)
snprintf(array[i][j], ssize, "%s", rand_string(ssize));
}
int str_compare(const void * a, const void * b)
{
const char *pa = **(const char ***) a;
const char *pb = **(const char ***) b;
// printf("debug: a = %s\n", **(const char ***) a);
return strcmp(pa, pb);
}
void print_array(char *** array, int n_rows, int n_cols)
{
int i, j;
for (i = 0; i < n_rows; i++) {
for (j = 0; j < n_cols; j++)
printf("%s ", array[i][j]);
printf("\n");
}
}
int main(void)
{
int n_rows = 3, n_cols = 5, ssize = 10;
char *** z;
z = init_array(z, n_rows, n_cols, ssize);
gen_array(z, n_rows, n_cols, ssize);
print_array(z, n_rows, n_cols);
printf("\n");
qsort(z, 3, sizeof(char *), str_compare);
print_array(z, n_rows, n_cols);
return 0;
}
with the following output, where are origin array and sorted array (these are example values and randomness doesn't matter):
nwlrbbmqb hcdarzowk kyhiddqsc dxrjmowfr xsjybldbe
fsarcbyne cdyggxxpk lorellnmp apqfwkhop kmcoqhnwn
kuewhsqmg bbuqcljji vswmdkqtb xixmvtrrb ljptnsnfw
fsarcbyne cdyggxxpk lorellnmp apqfwkhop kmcoqhnwn
kuewhsqmg bbuqcljji vswmdkqtb xixmvtrrb ljptnsnfw
nwlrbbmqb hcdarzowk kyhiddqsc dxrjmowfr xsjybldbe
I am looking for a way to sort by another column (second or third). Is it possible?
Thanks.
You can sort the matrix along its second column with this sorting function:
int str_compare2(const void * a, const void * b) {
const char *pa = ((const char ***)a)[0][1];
const char *pb = ((const char ***)b)[0][1];
return strcmp(pa, pb);
}
Changing the 1 into 2 will sort along the third column, etc.
Note that you should invoke qsort this way:
qsort(z, 3, sizeof(char **), str_compare);
z is an array of char **, not char *. On most architectures, sizeof(char*) == sizeof(char **), so there is no difference, but for consistency and readability, using the correct type is advisable.
I've been trying to make a function that appends matrix B onto Matrix A by making a new combined matrix. The first function I created passes a pointer (that was declared in main()) to the function which then works up the pointer to add values. This worked. However, I am also trying a different method by using malloc() within the function to define a pointer so that the function is more portable and dyanmic. However, when I try to print the final values in the final matrix I am getting undefined behavior.
Here is the included function from the created header file.
#include <stdio.h>
#include <stdlib.h>
int *fAddArrays(int *A, int *B, int a, int b)
{
int *O;
O = (int *) malloc((a+b) * sizeof(int));
int c;
int d;
for (c = 0; c < a; c++)
{
*O = *A;
A++;
O++;
}
for (d = 0; d < b; d++)
{
*O = *B;
B++;
O++;
}
return O;
}
Here is the use of the function in main()
#include <stdio.h>
#include <unistd.h>
#include "CustomArray.h"
#include <stdlib.h>
int main(void)
{
int A[5] = {1,2,3,4,5};
int B[7] = {6,7,8,9,10,11,12};
int a = 5;
int b = 7;
int c = a + b;
int x = 0;
int NewArray[c], *ArrayPtr;
ArrayPtr = fAddArrays(A,B,a,b);
for( x = 0; x < c; x++)
{
*(NewArray + x) = *ArrayPtr;
printf("Value of NewArray[%d] = %d\n", x, *ArrayPtr);
sleep(1);
ArrayPtr++;
}
return 0;
}
Your problem is that you increment O and then return it.
You need to save away the original value and increment a copy.
int *fAddArrays(int *A, int *B, int a, int b) {
int * original = (int *) malloc((a+b) * sizeof(int));
int * p = original;
for (int c = 0; c < a; c++) {
*p = *A;
A++;
p++;
}
for (int d = 0; d < b; d++) {
*p = *B;
B++;
p++;
}
return original;
}
I'm having problems understanding how to write code that solves the following problem: I have a structure containing a 2D-array. Then I have a recursive function that take a pointer to the structure as an argument and I want the recursive function to be able to manipulate the structure sent, not a local copy.
The struct is initialized in the function initStruct, where memory for the 2D-array is allocated. The recursive function builds up an array and at a specific point calls a function to insert it into the structure's array.
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int** spBasis(int);
void mpBasis(int**, int, int, int, int, int, int, int*, struct mpBasis *, int, int);
void initMpBasis(struct mpBasis *, int, int);
void insertMpState(struct mpBasis *, int *);
struct mpBasis {
int** basis;
int size;
int capacity;
};
int main() {
int a, b, c, d;
char maxE[256];
char noParticles[256];
char P[256];
char M[256];
FILE *fp;
int **spStates;
struct mpBasis *mp;
int mpState[6] = {0, 0, 0, 0, 0, 0};
printf("Input max e for sp states, no of particles, parity (1 for odd and 0 for even) and magnetic projection: ");
gets(maxE);
gets(noParticles);
gets(P);
gets(M);
spStates = spBasis(atoi(maxE));
fp = fopen("spStates.txt", "a+");
fprintf(fp, "E\tj\tl\tm\n");
for (a = 0; a < 330; a++) {
fprintf(fp, "State %d: ", a+1);
for (b = 0; b < 4; b++) {
fprintf(fp, "%d\t", spStates[a][b]);
}
fprintf(fp, "\n");
}
mp = malloc(sizeof(struct mpBasis));
initMpBasis(mp, 5449, 6);
for (c = 0; c < 5449; c++) {
for (d = 0; d < 6; d++) {
fprintf(fp, "%d: %d\t", c, mp->basis[c][d]);
}
fprintf(fp, "\n");
}
printf("%p\n", (void*) mp);
printf("hello 3");
mpBasis(spStates, 0, atoi(maxE), 0, atoi(M), 0, atoi(P), mpState, mp, 0, 0);
fclose(fp);
return 0;
}
int** spBasis(int maxE) {
int c;
int i, j, k, l;
int q = 0;
int** spStates;
spStates = (int**)malloc(330 * sizeof(int *));
for (c = 0; c < 330; c++) {
spStates[c] = malloc(4 * sizeof(int));
}
for (i = 0; i <= maxE; i++) {
for (j = i % 2; j <= i; j += 2) {
for (k = -(2 * j + 1); k <= (2 * j + 1); k += 2) {
spStates[q][0] = i;
spStates[q][1] = j;
spStates[q][2] = 2 * j + 1;
spStates[q][3] = k;
q += 1;
}
for (l = -(2 * j - 1); l <= (2 * j - 1); l += 2) {
spStates[q][0] = i;
spStates[q][1] = j;
spStates[q][2] = 2 * j - 1;
spStates[q][3] = l;
q += 1;
}
}
}
return spStates;
}
void mpBasis(int** spStates, int e, int maxE, int m, int M, int l,
int P, int * mpState, struct mpBasis *mpB, int position, int lastSpState) {
int i;
for (i = lastSpState; i < 330; i++) {
if (e > maxE) {
break;
} else if (position == 5) {
if (m == M && l % 2 == P) {
insertMpState(mpB, mpState);
break;
}
} else {
// add spState to mpState and make the recursive call for the next position
mpState[position] = i;
mpBasis(spStates, e + spStates[i][0], maxE, m + spStates[i][3], M,
l + spStates[i][1], P, mpState, mpB, position+1, i);
}
}
}
void initMpBasis(struct mpBasis *a, int initialSize, int sizeY) {
int c;
a->basis = (int **)malloc(initialSize * sizeof(int*));
for (c = 0; c < initialSize; c++) {
a->basis[c] = (int *) malloc(sizeY * sizeof(int));
}
a->size = 0;
a->capacity = initialSize;
}
void insertMpState(struct mpBasis *a, int* mpState) {
/*if (a->size == a->capacity) {
a->size *= 2;
a->basis = (int **)realloc(a->basis, a->size * sizeof(int));
}*/
a->basis[a->size++] = mpState;
}
Added all the code.
The problem is that after the recursive function has been called, the "basis" array in structure mpBasis still only contains random values, i.e. the mpBasis function hasn't done anything with it. Am I passing the mp argument by value here?
Thanks for your help!
The first step is to compile with warnings enabled. Eg if you are using GCC you can use option -Wall -Wextra.
EDIT:
(previous listing of >20 errors removed)
Ok, since you are using Visual Studio, enable warnings like this:
Open the project's Property Pages dialog box.
Select C/C++.
On the General property page, modify the Warning Level to /W4