card game in c, shuffle linked list - c

I trying to shuffle a linked list in c.
My idea was to move the list into an array of card then to shuffle the array and then to put it all back in the linked list.
when I do build everything is ok but when id use the debugger the program stops and says "triggered a breakpoint" and i didn't to anything different .
Thanks for your help!
here is some of my code
typedef struct card
{
int rank;
int suit;
}card;
void shuffleDeckPlay(cardList *deck1, int size)
{
card *array = (card*)malloc(sizeof(card));
for (int i = 0; i < size ; i++)
{
array[i] = deck1->front->data;
array = realloc(array, sizeof(card));
Dequeue(deck1);
}
int i, j;
card temp;
for (i = 0; i < size; i++)
{
j = rand() % size;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
for (int i = 0; i < size; i++)
{
Enqueue(array[i], deck1);
}
}

array is only large enough to store one card. Even after you realloc, you are only realloc-ing it to a size of one. Syntactically there is nothing wrong, however during runtime array[i] will go out of bounds and the debugger is picking it up.
The best solution would be to allocate the correct number immediately:
card * array = (card*)malloc(numCards*sizeof(card));
However if you want to re-allocate in the for loop then your for loop should look like this:
for (int i = 0; i < size; i++) {
array = (card*)realloc(array, (i+1)*sizeof(card));
array[i] = deck1->front->data;
Dequeue(deck1);
}

Related

Why do I get a segmentation fault by declaring a 2d array in c?

I am new to threads and I have a program that uses threads to find the minimum number out of a 2d array and later on, it finds the distance that the other elements of the array have from the minimum number and stores them in another array.
The user should enter the size of the array and the number of threads he wants to use.
I tried the program below for 1d array and it worked just fine. When I converted it to work for a 2d array it started crashing and throwing a segmentation fault. I, however, cannot find which part of the 2d declaration is wrong.
Any help is really appreciated.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <pthread.h>
struct Parameters
{
// input
int s,p; //n is size of array, p is number of threads
int** array; //array with elements
int start;
int end;
// output
int smallest;
int pos; //position if minimum
int** B; //array that holds the distances
};
void* min(void* args)
{
struct Parameters* p = (struct Parameters*)args;
int **array = p->array;
int **B1 = p->B;
int start = p->start;
int end = p->end;
int smallest = array[start][start];
int pos = p->pos;
int distance;
//find the smallest
for (int i = start; i < end; i++)
{
for(int j = start; j < end; j++)
{
if (array[i][j] < smallest)
{
smallest = array[i][j];
pos = i;
}
}
}
//find the distances
for(int i = 0; i < ((struct Parameters*)args) -> s; i++)
{
for(int j = 0; j < ((struct Parameters*)args) -> s; j++)
{
distance = abs(pos - i);
B1[i][j] = distance;
}
}
params->smallest = smallest;
params->B = B1;
return NULL;
}
int main()
{
int smallest,pos;
int s,p;
struct Parameters *ptr = (struct Parameters *)malloc(sizeof(struct Parameters));
if(ptr == NULL)
{
printf("Not enough. Try again \n");
exit(0);
}
printf("Type s\n");
scanf("%d",&(ptr->s));
printf("Type p\n");
scanf("%d", &(ptr->p));
// declare an array of threads and associated parameter instances
pthread_t threads[(ptr->p)];
struct Parameters thread_parameters[(ptr->p)] ;
int arr[ptr->s][ptr->s];
int B2[ptr->s][ptr->s];
// intialize the array
for(int i=0; i< ptr->s; i++)
{
for(int j=0; j< ptr->s; j++)
{
printf("Type a \n");
scanf("%d",&arr[i][j]);
}
}
// smallest needs to be set to something
smallest = arr[0][0];
// start all the threads
for (int i = 0; i < ptr->p; i++)
{
memcpy(arr, thread_parameters[i].array, sizeof(arr));
thread_parameters[i].s = ptr->s;
memcpy(Bb, thread_parameters[i].B, sizeof(B2));
thread_parameters[i].start = i * (ptr->s / ptr->p);
thread_parameters[i].end = (i+1) * (ptr->s / ptr->p);
pthread_create(&threads[i], NULL, min, &thread_parameters[i]);
}
// wait for all the threads to complete
for (int i = 0; i < ptr->p; i++)
{
pthread_join(threads[i], NULL);
}
// Now aggregate the "smallest" and "largest" results from all thread runs
for (int i = 0; i < ptr->p; i++)
{
if (thread_parameters[i].smallest < smallest)
{
smallest = thread_parameters[i].smallest;
}
}
printf("Smallest is %d\n", smallest);
thread_parameters[ptr->p].B[ptr->s][ptr->s];
for (int i = 0; i < 1; i++)
{
for(int j = 0; j < ptr->s;j++)
{
for(int k = 0; k < ptr->s; k++)
{
printf("Element %d is %d away from min\n",j,thread_parameters[i].B[j][k]);
}
}
}
return 0;
}
Thank you!!
The issue with your code might also come from :
memcpy(arr, thread_parameters[i].array, sizeof(arr));
...
memcpy(Bb, thread_parameters[i].B, sizeof(B2));
as thread_parameters[i].array and thread_parameters[i].B are not allocated, if you are only reading the array it might b fine to only pass them by address
thread_parameters[i].array = arr
but for thread_parameters[i].B you would need to allocate the arrays and perform a deep copy (memcpy would not work)
The below text does not answer the question but does provide some insight on VLA usage
One reason for causing the segmentation with a declaration of a Variable Length Array is that the value is to large to allocate the array on the stack (some compiler choose this option, this choice might have performance reason).
The is not much option to recover cleanly from failure to allocate memory on the stack as there is little way to clean up stack memory during runtime within the same stack context.
You can mitigate the issue by allocating your 2D arrays on the heap instead, some of the strategies are available here(thanks #Lundin) and here.
int** alloc_2d_int_array(size_t rows, size_t cols) {
int **result = malloc(rows * sizeof(int *));
if(result == NULL) {
// could not allocate more memory
return NULL;
}
size_t row_size = cols * sizeof(int);
for(int i=0; i < rows; ++i) {
result[i] = malloc(row_size);
if(result[i] == NULL) {
// could not allocate more memory
// cleanup
return NULL;
}
}
return result;
}
the above implementation have not been tested, but does compile, there are still risk of integer overflow.
Then use the above define function as following:
int **arr = alloc_2d_int_array(ptr->s, ptr->s);
int **B2 = alloc_2d_int_array(ptr->s, ptr->s);
easier implementation (see here(thanks #Lundin))
int **arr = malloc(sizeof(int[ptr->s][ptr->s]);
int **B2 = malloc(sizeof(int[ptr->s][ptr->s]);

How to increment value in a struct matrix in c?

I've calloc'd a correlation matrix in a struct but am unable to increment/set values in that matrix. The struct is
typedef struct matrixStruct{
char** word;
int numberOfWords;
int** matrix;
} matrix;
This is how i've allocated the structure using calloc. I've used calloc because i'd like all the values in the matrix to be 0.
//Allocate rows of matrix
wordStore->matrix = calloc(2000,sizeof(int*));
//Allocate columns of matrix
for(int j = 0; j< 2000; j++)
{
wordStore->matrix[j] = calloc(2000,sizeof(int*));
}
Assume i have an array which has values of
int reference [] = {20,400,5,1899};
And this is how i'm trying to assign/increment values within the matrix, but it doesn't seem to work.
for(int k = 0; k<lenReference; k++)
{
for(int l = 0; l<lenReference;l++)
{
wordStore->matrix[k][l] += 1;
if(k == l){
wordStore->matrix[k][l] = 0;
}
}
}
This is the print loop that i'm using, and when i run the file it has an error that says "subscripted value is not an array, pointer, or vector", but i thought that this was the correct way to print a 2d array. What is the issue with this print?
for(int i = 0; i<2000; i++)
{
for (int j = 0; j<2000; j++)
{
printf("%08d ", words.matrix[i][j]);
}
printf("\n");
}
Any help would be much appreciated!

Undefined behavior with 2d array of struct C

I have a 2d array of structs that I am assigning strings to, here is my struct.
struct node {
char* value;
};
Here is my allocation (I am new to C so I am not sure if it is right) but there will always be 35 columns but there could be millions of rows.( I just had it at 3 for now for testing)
const int rows=3;
struct node ** arrayofnodes[rows][35];
for(int i=0; i<rows; i++) {
array[i] = malloc(test * sizeof array[0]);
for(int j=0; j<35; j++) array[i][j] = malloc(sizeof array[0][0]);
}
I then read in character by character from a csv file and have a temp string, and assign the value of the temp to the position I want by using this below.
//int row and count are defined in my while loop I have for counting commas(or what col I am on) then new lines for the rows
arrayofnodes[row][count]->value=strdup(temp);
printf("%s \n", arrayofnodes[row][count]->value);
printf("%d %d \n",row, count );
When I assign like the way above it seems to work. I added these print statements in to make sure it was assigning the right values.
For example above would print out something like
Red
0 0
And this is correct for that position.
But then after I do all of my assigning. I placed a print statement printf("%s \n", arrayofnodes[0][0]->value); to test if I can retrieve the 1st value as shown above which should be "Red".
In my terminal it outputs "#`??" or "#Pz?" or just any random output. I have tried this for a bunch of different positions besides 0,0, but they all get the same outcome. I guess I am just confused why the print statements work right after I assign them, but not at the end of my code when I call them later.
This is what it looks like you're trying to do. You will need to scan your csv file and compute the number of rows required, then populate the values however you want.
#include <stdio.h>
#include <stdlib.h>
struct node {
char* value;
};
int main() {
const int rows = 3; // you will need to compute this beforehand
const int columns = 35;
struct node** arrayofnodes = malloc(rows * sizeof(struct node*));
for (int i = 0; i < rows; ++i) {
arrayofnodes[i] = malloc(columns * sizeof(struct node));
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
arrayofnodes[i][j].value = malloc(...);
strcpy(arrayofnodes[i][j].value, ...); // etc..
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
free(arrayofnodes[i][j].value);
}
}
for (int i = 0; i < rows; ++i) {
free(arrayofnodes[i]);
}
free(arrayofnodes);
}
You can avoid overcomplicated allocation by using constant size inside the structure:
struct OneRow
{
char Value[35];
}
const int Rows=3;
OneRow *MyArray=NULL;
MyArray = (OneRow*) malloc (Rows*sizeof(OneRow));
You can now access each element (character) or a whole string as
MyArray[rownumber].Value[colnumber] = …
strcpy (MyArray[rownumber].Value, "I'm_shorter_than_35"); //34 chars max + null-term

c Program crashing using 2d array comparing values

Hi im having problems with my c code it keeps crashing with no error and im not sure why. i am trying to find the value at a point inside a 2d array for example [1][1] and see what the value is there (only 1 or a 0) and then process the value depending on if its 1 or a 0 but the program keeps crashing with no error and im not sure why.please help
typedef struct gol{ // structure containing a square board
int **board; // pointer to a pointer
size_t size; //size_t = unnasigned value
}gol;
the struct is created in main using
struct gol *GAME;
GAME = create_gol(30);
using an if menu options if option is selected it will just call
next pattern function but it crashes
gol* create_gol(size_t size){
struct gol *Game_Of_Life;
Game_Of_Life = (struct gol*)malloc(sizeof(struct gol*)); //dynamically create the struct the ptr is pointing to **IMPORTANT
Gameboard = new int*[size];
for (int i = 0; i < size; ++i) {
Gameboard[i] = new int[size];
// each i-the pointer is now pointing to dynamic array (size 20) of actual int values
}
for (int i = 0; i < size; ++i) { // for each row
for (int j = 0; j < size; ++j) { // for each column
Gameboard[i][j] = 0;
}
}
Game_Of_Life->board=Gameboard;
Game_Of_Life->size=size;
return Game_Of_Life;
}
void next_pattern(gol* g)
{
for (int i = 0; i < 20; ++i) { // for each row
for (int j = 0; j < 20; ++j) { // for each column
int sum = neighbour_sum(g,i,j);
if (g->board[i][j]==1){
if (sum<2){
g->board[i][j]=0;
}
if (sum==3 || sum==2 ){
g->board[i][j]=1;
}
if (sum>3){
g->board[i][j]=0;
}
}
if (g->board[i][j]==0 && sum==3){
g->board[i][j]=1;
}
}
}
}
updates neighbour sum so it cant go out of bounds program still crashing
int neighbour_sum(gol* g, int i, int j)
{ int sum;
if ((g->board[(i-1+g->size)%g->size][j])==1){ // left
sum++;
}
if ((g->board[(i-1+g->size)%g->size][(j-1+g->size)%g->size])==1){//left up
sum++;
}
if ((g->board[i][(j-1+g->size)%g->size])==1){ //up
sum++;
}
if ((g->board[(i+1)%g->size][(j+1)%g->size])==1){ //right up
sum++;
}
if ((g->board[i][(j+1)%g->size])==1){ //right
sum++;
}
if ((g->board[(i+1)%g->size][(j+1)]%g->size)==1){//right bottom
sum++;
}
if ((g->board[i][(j+1)%g->size])==1){//bottom
sum++;
}
if ((g->board[(i-1+g->size)%g->size][(j+1)%g->size])==1){// bottom left
sum++;
}
return sum;
}
These lines
for (int i = 0; i < 20; ++i) { // for each row
for (int j = 0; j < 20; ++j) { // for each column
int sum = neighbour_sum(g,i,j);
means that you first call neighbour_sum with both i and j being zero.
Inside neighbour_sum you do:
if ((g->board[(i-1)][j])==1){ // left
^^^^
So since both i and j are zero, it is actually:
if ((g->board[-1][0])==1){ // left
^^^^
ups
So you access the array out of bounds. That may cause a crash. In any case it is illegal.
The general problem seems to be that you don't handle when the point is at the edge of the board.
edit after OP posted more code
This is wrong
Game_Of_Life = (struct gol*)malloc(sizeof(struct gol*));
^^^
do
Game_Of_Life = malloc(sizeof(struct gol));
I found the solution in the end there where a number of issues including i was looking for value outside of the array that was solved using
if ((g->board[(i-1+g->size)%g->size][j])==1){ // left
sum++;
}
i also was using c++ syntax instead of c syntax witch was resolved using the malloc function
Game_Of_Life = (struct gol*)malloc(sizeof(struct gol));
and the last issue causing the crashing still was the function neighbour sum was returning -2 because it wasn't initialized at 0 properly
int sum = 0;

Quadruple pointer and memcpy() in C

First of all, I know triple and quadruple pointers are bad practice and are ugly, that's not the point of this question, I'm trying to understand how they work. I'm aware using a struct would be much better.
I am trying to write a function that does some memory operations using memmove() and memcpy() on triple and double pointers that are passed-by-reference (or the C version of that). My memmove() works fine, but the memcpy() yields a SIGSEGV. Here's a minimal example
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define UNDO_DEPTH 25
void boardSave(int ***board, int game_sz, int ****history) {
// Shift history to the right
memmove(*history + 1, *history, (UNDO_DEPTH - 1) * sizeof(**history));
// Copy board into history
for (int row = 0; row < game_sz; ++row) {
memcpy((*history)[0][row], (*board)[row], game_sz * sizeof((**board)[row]));
}
}
int main(){
// Game
int game_sz = 5;
// Allocate array for the board
int **board = calloc(game_sz, sizeof(int *));
for (int i = 0; i < game_sz; ++i) board[i] = calloc(game_sz, sizeof(int));
// Allocate array for the history
int ***history = calloc(UNDO_DEPTH, sizeof(int **));
for (int i = 0; i < UNDO_DEPTH; ++i) {
history[i] = calloc(game_sz, sizeof(int *));
for (int j = 0; j < game_sz; ++j) {
history[i][j] = calloc(game_sz, sizeof(int));
}
}
board[0][0] = 1;
boardSave(&board, game_sz, &history);
}
The objective of boardSave() here is to copy board onto history[0]. What am I doing wrong? Why is this causing a segmentation fault?
In the main function you make history point to an array of UNDO_DEPTH pointers, each of which points to a board that has its own allocation. Since memmove moves a contiguous memory blocks, you cannot move the content of all those boards with memmove.
However, you could move down the pointers in that history array, leaving the board allocations untouched.
Just doing a single memmove would require you to free memory of the last board shuffled off, and allocate memory for the new board. But you could recycle that memory by moving the last pointer to the start instead.
Now, there is no need to pass the addresses of board and history to the boardSave function. It just makes your code more complicated for no reason. The simpler version would be:
void boardSave(int **board, int game_sz, int ***history)
{
// Save the last board
int ** last_board = history[UNDO_DEPTH - 1];
// Shuffle down all the boards
memmove( &history[1], &history[0], (UNDO_DEPTH - 1) * sizeof history[0] );
// Put the old last board on the front
history[0] = last_board;
// Copy board into front of history
copy_board( game_sz, history[0], board );
}
// Put a prototype for this earlier in the code. I think it makes
// the boardSave function clearer to use a separate function for this
// operation, which you might end up using on its own anyway.
//
void copy_board( int game_sz, int **dest, int **src )
{
for(int row = 0; row < game_sz; ++row)
memcpy(dest[row], src[row], game_sz * sizeof dest[0][0]);
}
Personally I'd prefer to avoid memcpy in the last function and just write a simple loop that is obviously correct. The compiler will optimize it to use memcpy anyway, but without the possibility of making an error in the memcpy parameters:
for(int row = 0; row < game_sz; ++row)
for (int col = 0; col < game_sz; ++col)
dest[row][col] = src[row][col];
Similar comments would apply to the use of memmove actually.
I would also make some use of const in the function signatures, so that a compiler error is generated if I accidentally switched the "dest" and "src" arguments. But I left that out at this stage for simplicitly.
In main the call would now be:
boardSave(board, game_sz, history);
If you reeeeealy want to pass pointers for practice then I would "de-point" them at the start of the function:
void complicated_boardSave(int ***p_board, int game_sz, int ****p_history)
{
int *** history = *p_history;
int ** board = *p_board;
// rest of code the same
I understand you want to challenge pointers.
I wanted provide a solution that utilizes single pointer.
As a matter of fact, you don't need to use a pointer at all.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int game_sz = 5;
#define UNDO_DEPTH 25
void boardSave(int *board[game_sz], int game_sz, int *history[UNDO_DEPTH]
[game_sz])
{
int i,j,k;
for( i = 0; i < UNDO_DEPTH - 1; i++)
for( j = 0; j < game_sz; j ++ )
for( k = 0; j < game_sz; j ++ )
history[i+1][j][k] = history[i][j][k];
for( i = 0; i < game_sz - 1; i++)
for( j = 0; j < game_sz; j++ )
history[0][i][j] = board[i][j];
}
int
main(void)
{
int *board[game_sz];
int *history[UNDO_DEPTH][game_sz];
int i, j;
for (i = 0; i < game_sz; ++i)
board[i] = calloc(game_sz, sizeof(int));
board[0][0] = 1;
// Allocate array for the history
for ( i = 0; i < UNDO_DEPTH; ++i)
for ( j = 0; j < game_sz; ++j)
history[i][j] = calloc(game_sz, sizeof(int));
boardSave( board, game_sz, history);
return 0;
}

Resources