queue using struct that point to array - c

In C I try to learn queue data structure and make pointer to struct but inside the struct there is pointer to array. Here queue is struct, q is pointer to the struct, and inside struct there are rear, front, num and int *que(pointer to array to store data)
typedef struct {
int max;
int rear;
int front;
int num;
int *que;
} queue;
And malloc() using
queue->que=malloc(sizeof(int)12) ; to make arrray And to access it,
q->que[q->rear++] //not to familiar,
First I'm not declaring array but can I access data pointed by que using []?
Is this mean access que[q->rear++] inside q pointer?
Is this the same as (q).que[q->rear++]? I got segmentation fault .
Part of the code; but there are some errors
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int max;
int num;
int front;
int rear;
int *que;
} queue;
int initialization(queue*q, int max) {
q->num = 0;
q->front = 0;
q->rear = 0;
if (q->que =(int*)malloc(max * sizeof(int)) == NULL) { // got pointer NULL i dont know why
q->max = 0;
return-1;
}
q->max=max;
return 0;
}
int main() {
queue que;
if (initialization(&que, 12) == -1)
printf("fail");
else {
int m,x;
while (m != 0) {
printf("1.enque 2.deque. 3.peek 4.display 0. slese");
scanf("%d", &m);
switch (m) {
case 0: break;
case 1: printf("data");
scanf("%d", &x);
enqueue(&que, x);
break;
case 2: dequeue(&que, &x);
printf("%d is dequeue", x);
break;
case 3:x=peek(&que,&x);
printf("max now is %d", x);
break;
case 4:display(&que);
}
}
}
int enqueue(queue*q, int x) {
if (q->num >= q->max)
return -1;
else{
q->num++;
q->que[q->rear++]= x; //got segmentation fault
if (q->rear == q->max)
q->rear = 0;
}
}

In your initialization() function, while allocation memory with malloc() like
if (q->que =(int*)malloc(max * sizeof(int)) == NULL) {
the (int*)malloc(max * sizeof(int)) part is evaluated first and then this value is compared against NULL via the == operator. This will result in 0 if the condition is false and 1 otherwise.
Now this (0 or 1) value is being assigned to q->que instead of the return value of malloc(). So bottom-line is that q->que points to the memory location 0 (or 1 as the case maybe) which is most probably not a part of memory that a normal program is allowed to mess with and hence you get error.
You can solve this operator precedence problem by using parenthesis like
if ((q->que = malloc(max * sizeof(int))) == NULL) {
And in your while loop inside main(), the control expression is m!=0 but m is not even initialised during the first iteration. At that point, its value is indeterminate (garbage value).
You could initialise m to something other than 0 first like
int m=1,x;
while (m != 0) {
And in C, you needn't cast the value returned by malloc(). See here.

Related

RPN calculator using linked list

I'm having trouble with my code. it seems to work only on single digit int. I don't know how to create a function that would work for int greater than 9. Also I don't know how to end the program if string is empty.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//stack type
struct node
{
int head;
int *array;
unsigned capacity;
};
struct node* createNode();
int isEmpty();
char pop();
void push(struct node* stack, char op);
int evaluatePostfix();
int main() {
char exp[1000]; // = "5 1 2 + 4 * + 3 -";
printf("Input string:\t");
fgets(exp, 1000, stdin);
for(int i = 1 ; i <= strlen(exp); i++) {
if(exp[i] == '\n') {
exp[i] = '\0';
}
else if (exp[0] == '\n') {
printf("stack is empty\n");
exit(0);
}
}
printf ("%s = %d\n", exp, evaluatePostfix(exp));
return 0;
}
struct node* createNode(unsigned capacity) {
struct node* stack = (struct node*) malloc(sizeof(struct node));
if (!stack) return NULL;
(*stack).head = -1;
(*stack).capacity = capacity;
(*stack).array = (int*) malloc((*stack).capacity *sizeof(int));
if (!(*stack).array) return NULL;
return stack;
}
int isEmpty(struct node *stack) {
return (*stack).head == -1 ;
}
char pop(struct node* stack) {
if (!isEmpty(stack))
return (*stack).array[(*stack).head--] ;
return '$';
}
void push(struct node* stack, char op) {
(*stack).array[++(*stack).head] = op;
}
// The main function that returns value of a given postfix expression
int evaluatePostfix(char* exp) {
// Create a stack of capacity equal to expression size
struct Stack* stack = createStack(strlen(exp));
struct node *stack = createNode(strlen(exp));
if (!stack) return -1;
// Scan all characters one by one
for (int i = 0; exp[i]; ++i){
// If the scanned character is an operand or number,
// push it to the stack.
if ((exp[i])== ' ') continue;
else if (isdigit(exp[i]))
push(stack, exp[i] - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int val1 = pop(stack);
int val2 = pop(stack);
switch (exp[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2/val1); break;
}
}
}
return pop(stack);
}
I can't write the whole thing for you but can point you in the right direction. Firstly, when someone says "library function XYZ() would be helpful for you" then you should go and read about that function in the manual pages. For example, from a Linux shell run: man atoi to read about the atoi function.
For your particular problem, it boils down to parsing strings and converting them to numbers and operators. Some helpful library functions would thus be:
strtok: Extracts delimited string tokens from a longer string. You can use this to get each of the seperate inputs.
atoi: This can convert a string representation of a number to its integer equivalent. Does not allow for error checking.
strtol: Can do the same as atoi (and more) and also allows for error checking.
With that information in mind, here is a code snippet that may be useful for you:
int evaluatePostfix(char* exp)
{
char *token;
long int number;
/* strtok will keep extracting the next token delimited by space */
while (token = strtok(exp, " ")) {
/* Now parse the token and process it */
if (is_operator(token)) {
/* do operator processing */
} else {
number = strtol(token, NULL, 10);
/* do number processing */
}
}
/* strtok returns NULL when no more tokens. So
we are done when the while loop exits */
}
Note that the above code does not do error checking on strtol. You probably want to do that. Read the manual pages for strtol to understand how error checking is done for it.

pointer to dynamic array of pointers to dynamic array of pointer that points to strings

i have a problem with the initialization of the values inside the first dynamic array of pointers
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char*** GetIndexes()
{
int n = 0;
char ***index;
printf("please insert the number of words you want to add to dictionary\n");
scanf("%d", &n);
index = (char***)calloc(n, sizeof(char));
if (index == NULL)
{
printf("allocation Failed");
return;
}
return index;
}
char** GetDefinitions()
{
int n = 0;
char **definition;
printf("please insert the number of defintions you want to add to the word\n");
scanf("%d", &n);
definition = (char**)calloc(n+1, sizeof(char));
if (definition == NULL)
{
printf("allocation failed");
return;
}
return definition;
}
int main()
{
char *** dptr = GetIndexes();
if (dptr == NULL)
{
printf("memory Allocation failed");
}
int indexcount = sizeof(dptr) / sizeof(char),i;
for (i = 0; i < indexcount; i++)
{
printf("word number %d\n", i + 1);
*dptr[i] = GetDefinitions();
}
printf("%p",dptr);
}
i tried running the debugger in VS2013 and after i enter the number of defintions i want it crashed with this message:
Unhandled exception at 0x01103FB0 in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x00000000.
i missed an allocation of something but i cant quite figure out what i missed,
thanks in advance
Your program is very broken
You allocate n char ***s but only request space for n chars and also do it for char **, to prevent this kind of mistake you may use the sizeof operator this way
char ***index;
index = calloc(n, sizeof(*index));
and
char **definition;
definition = calloc(n, sizeof(*definition));
and as you see casting calloc makes it harder and it's not necessary.
You have a return statement that doesn't return anything an GetIndexes() as well as one in GetDefinitions.
They should return NULL if you want to handle failure in the caller function
return NULL;
You erroneously use the sizeof operator to determine the number of char *** pointer allocated in
int indexcount = sizeof(dptr) / sizeof(char)
this will be either 4 or 8 depending on the architecture i.e. the size of a pointer divided by 1 sizeof(char) == 1 always.
You can't compute that value, you simply have to keep track of it. The size
You dereference the triple pointer twice and try to assign a double pointer to it
*dptr[i] = GetDefinitions();
here the operator precedence is also an issue, but regardless of that, this is wrong, may be what you meant was
dptr[i] = GetDefinitions();
This is not going to make your program crash, but it's certainly important to free all malloced pointers before exiting the program.
Here is a suggestion for your code to work, ignore it's purpose since it's not clear what you are trying to do
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char ***GetIndexes(unsigned int *count)
{
char ***index;
printf("please insert the number of words you want to add to dictionary > ");
scanf("%u", count);
index = calloc(*count, sizeof(*index));
if (index == NULL)
{
printf("allocation Failed");
return NULL;
}
return index;
}
char **GetDefinitions(unsigned int *count)
{
char **definition;
printf("please insert the number of defintions you want to add to the word > ");
scanf("%u", count);
definition = calloc(*count + 1, sizeof(*definition));
if (definition == NULL)
{
printf("allocation failed");
return NULL;
}
return definition;
}
int main()
{
unsigned int indexCount, i;
char ***dptr = GetIndexes(&indexCount);
if (dptr == NULL)
{
printf("memory Allocation failed");
}
for (i = 0; i < indexCount; i++)
{
unsigned int definitionsCount;
printf("Word number %u\n", i + 1);
dptr[i] = GetDefinitions(&definitionsCount);
if (dptr[i] != NULL)
{
/* use dptr[i] here or maybe somewhere else, but when you finish */
free(dptr[i]);
}
}
printf("%p", dptr);
/* now if you are done using dptr */
free(dptr);
return 0;
}
As already mentioned in the comment this is a very bad idea and just using double pointers is good here. But the below fixes should be done if you want to use pointers to allocate memory
index = calloc(n, sizeof(char));
should be
index = calloc(n, sizeof(char **));
and
definition = calloc(n+1, sizeof(char));
should be
definition = calloc(n+1, sizeof(char *));

realloc() invalid nxt size

I use this code, with this structure, im trying to make function to add item into array of this structure
typedef struct goods{
char *name;
int num;
} goods;
void addWord(char *what, goods *where, int pnr, int *arrsize, int n){
if (pnr >= *arrsize){
where = (goods*)realloc(where,*arrsize*2*sizeof(goods*));
*arrsize*=2;
}
where[pnr].name = (char*)malloc(strlen(what)*sizeof(char));
strcpy(where[pnr].name,what);
where[pnr].num = n;
}
in main function i have this:
int extstore = 1;
goods *store = (goods*)malloc(1*sizeof(goods*));
addWord(line, store, nr, &extstore, n);
Why am I getting an "invalid next size" runtime-error on the line where = (goods*)realloc(where,*arrsize*2*sizeof(goods*)); in addWord()?
EDIT:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct goods{
char *name;
int r;
} goods;
int main()
{
int linelen, i, nr = 0, current_r;
char *line = NULL;
size_t len = 0;
int extstore = 1;
goods *store;
store = malloc(extstore*sizeof(goods*));
while (1){
while ((linelen = getline(&line, &len, stdin)) != -1){
if (line[linelen - 1] == '\n'){
line[linelen - 1] = '\0';
}
linelen = strlen(line);
if (line[0] == '#'){
if (sscanf(line,"#%d",&current_r) != 1){
printf("bad input.");
return 0;
} else continue;
}
if (nr >= extstore){
store = realloc(store,extstore * sizeof(goods*) * 2);
extstore*=2;
}
store[nr].name = malloc(strlen(line)*sizeof(char));
strcpy(store[nr].name,line);
store[nr].r = current_r;
nr++;
}
if (linelen == -1) break;
}
printf("\n");
for (i = 0;i < nr;i++){
printf("%s, [id:%d]\n", store[i].name, store[i].r);
}
return 0;
}
extstore * sizeof(goods*) * 2
should be extstore * sizeof(goods) * 2 because the space for structures should be allocated - not just for pointers.
There is a fundamental problem in your code. You are passing pointer by value, which means that any change made to a pointer (not the variable pointed to, but the pointer itself) will not be visible from outside the function. You should pass a pointer by pointer instead, and you should check the result returned from realloc. Secondly, don't assign result of realloc back to same pointer - in case of failure you will lost pointer to memory -> thus, memory leak will occur.
To pass pointer by pointer:
void addWord( char *what, goods **where, size, ...) {
if ( *where == NULL) return; // nothing to do
if ( size < 1) return; // it would result in realloc=free call
goods *res = NULL;
res = realloc( *where, size * sizeof( goods));
if ( res != NULL) {
*where = res;
}
else {
// Error (re)allocating memory
// If realloc() fails the original block is left untouched,
// it is not freed or moved, so here *where is unchanged
}
And there is no need in C to cast a result from malloc.
* Error in `path': realloc(): invalid next size: 0x0000000000ec8010 *
This failure must be because "where" is invalid due to a heap corruption earlier in the execution.
C is pass-by-value.
Which means changing an argument in the function does not change the expression it was initialized from.
Thus, the first time realloc moves the memory, the pointer in main will be bad.
To correct that, either use an extra level of indirection, or preferably return the new value as the result.
(Anyway, you should check for allocation failure (malloc and realloc),
and you should not cast from void* to any pointer-type in C.)

single Queue and multiple Queue using same program not work

here is two c program to implement queue data structure in simple form
the first:
define one queue and it's work perfectly
the second:
define multiple queues and it's crash at execution
functions are the same in both programs except the main() were the implementation
are different little bit.
So the question here: why the second code not working?
* here is the codes *
code 1:
/*
Single queue -- this work perfectly
*/
#include <stdio.h>
#define Q_MAX_SIZE 255
struct queue {
int* pointer;
int* currentValue;
int max, count, theQueue[Q_MAX_SIZE];
};
//prototyps
void initQueue(struct queue*);
unsigned short pushQueue(struct queue*, int);
int* popQueue(struct queue*);
int main(void) {
int j;
struct queue q;
initQueue(&q);
for (j = 0; j < 6; j++)
pushQueue(&q, j);
int* inputobj = popQueue(&q);
while (inputobj != NULL)
{
printf("%d ", *inputobj);
inputobj = popQueue(&q);
}
printf("\n\ndone..Queue is empty\n");
return 0;
}
//#################################
void initQueue(struct queue *Q)
{
Q->pointer = Q->theQueue;
Q->max = Q_MAX_SIZE;
Q->count = 0;
}
unsigned short pushQueue(struct queue *Q, int input) {
if (Q->count < Q->max)
{
*Q->pointer = input;
Q->pointer++;
Q->count++;
return 1;
}
else
return 0;
}
//#################################
int* popQueue(struct queue *Q) {
int i;
if (Q->count > 0)
{
*Q->currentValue = *Q->theQueue;
Q->pointer--;
Q->count--;
for (i = 0; i < Q->count; i++)
{
int* currentPtr = Q->theQueue + i;
int* nextPtr = currentPtr + 1;
*currentPtr = *nextPtr;
}
return Q->currentValue;
}
else
NULL;
}
code 2:
/*
Multiple queues -- this not work and crash at execution
*/
#include <stdio.h>
#define Q_MAX_SIZE 255
struct queue {
int* pointer;
int* currentValue;
int max, count, theQueue[Q_MAX_SIZE];
};
//prototyps
void initQueue(struct queue*);
unsigned short pushQueue(struct queue*, int);
int* popQueue(struct queue*);
int main(void) {
int i, j;
struct queue obj[5];
for(i=0; i<5; i++)
{
initQueue(&obj[i]);
for(j = 0; j<3; j++)
{
pushQueue(&obj[i], j);
}
}
for(i=0; i<5; i++)
{
printf("Queue[%d]:\n", i);
int* inputobj;
inputobj = popQueue(&obj[i]);
while(inputobj != NULL)
{
printf("Queue[No.%d] = %d\n", i, *inputobj);
inputobj = popQueue(&obj[i]);
}
putchar('\n');
}
return 0;
}
//#################################
void initQueue(struct queue *Q)
{
Q->pointer = Q->theQueue;
Q->max = Q_MAX_SIZE;
Q->count = 0;
}
unsigned short pushQueue(struct queue *Q, int input) {
if (Q->count < Q->max)
{
*Q->pointer = input;
Q->pointer++;
Q->count++;
return 1;
}
else
return 0;
}
//#################################
int* popQueue(struct queue *Q) {
int i;
if (Q->count > 0)
{
*Q->currentValue = *Q->theQueue;
Q->pointer--;
Q->count--;
for (i = 0; i < Q->count; i++)
{
int* currentPtr = Q->theQueue + i;
int* nextPtr = currentPtr + 1;
*currentPtr = *nextPtr;
}
return Q->currentValue;
}
else
NULL;
}
Update: the problem was in initQueue() and it's solved by allocating memory
for Q->currentValue here is the function after editing:
void initQueue(struct queue *Q)
{
Q->currentValue = malloc(sizeof(int));
Q->pointer = Q->theQueue;
Q->max = Q_MAX_SIZE;
Q->count = 0;
}
As both answers already stated, the problem is that Q->current_value has never been assigned a value to and so it points to an undefined address and every dereferencing like *Q->currentValue = .. is undefined behaviour. The fact that code 1 seemingly works doesn't prove anything else because due to the nature of UB no behaviour is guaranteed, your program may or may not crash (or your dog may explode, dragons fly out of your nose ... :-) )
Of course there are multiple solutions that all mean something different:
if currentValue should just hold a copy of a certain value, it could be int currentValue instead of int *... and the assignnment would be
Q->currentValue = *Q->theQueue;
and the return statement would be return &Q->currentValue. In that case you would return a pointer to the original value of theQueue[0]
if you want to point to the location in theQueue, Jim's anser tells the correct way:
Q->currentValue = Q->theQueue;
In that case you yould return a pointer to the new value of theQueue[0] (which may be what you don't want)
you could allocate memory to Q->currentValue my malloc( sizeof (int) ); and then leave the assignment as it is. In that case you would return a pointer to the original value of theQueue[0] like in (1)
This is actually are really subtle problem, I think. The problem (I THINK) is this line in popqueue():
*Q->currentValue = *Q->theQueue;
I double-checked and your initial code (no array) also seg faults. It does not work as you have said. You should have written:
Q->currentValue = Q->theQueue;
C can be a bit understanding with pointers and assign things appropriately, but when you added another level (arrays) I think the assignment was forced into something that didn't work. That's my take on it. I think I will put up a bounty so you can get a better answer.
first of all, I would not try to put code like this one in production. Things can be done more simple, clear, performant and less error prone.
I've "fixed" your program by changing things in as little as possible places. It must be clear that this doesn't make things more elegant. Only rethinking and rewriting would make things more elegant.
The error you have (both in the first and the second program) is the routine popQueue.
You return nothing in the else clause. You should "return NULL". This is at least sloppy programming.
The routines return 1 2 3 4 5 5 and 1 2 2 for a queue. This is because Q->CurrentValue points to the first place in the theQueue array, and you shift up all values. This means that CurrentValue defacto points to the next value.
The solution (again: it's not elegant, nor would I put it in production, but it is with minimal change to the original) to your problem is:
Change in the struct (to hold the real CurrentValue)
struct queue
{
int* pointer;
int currentValue;
int max, count, theQueue[Q_MAX_SIZE];
};
Change the routine popQueue
int* popQueue(struct queue *Q) {
int i;
if (Q->count > 0)
{
Q->currentValue = *Q->theQueue;
Q->pointer--;
Q->count--;
for (i = 0; i < Q->count; i++)
{
int* currentPtr = Q->theQueue + i;
int* nextPtr = currentPtr + 1;
*currentPtr = *nextPtr;
}
return &(Q->currentValue);
}
else
return NULL;
}
Kind regards,
PB
The location Q->currentValue cannot be accessed, that is the problem. It is not allocated.
The solution is to allocate the right portion of memory in the init routine:
Q = malloc(sizeof(struct queue));
perhaps also initializing the value of all your variables after that.

Setting a double pointer array

I know there are a lot of double pointer questions, but I couldn't find one that pertained to starting an array.
In the code below, I can set pointers in main by ptrs[0] = &array[0];, but the code halts when enqueue() calls *queue[i] = p;. Why is that? I don't know if it matters, but ptrs[] is not initialized.
#define QUEUE_LEN 5
int *ptrs[5];
int array[5] = {1,2,3,4,5};
void enqueue(int *p, int **queue) {
int i = 0;
int *tmp;
// Find correct slot
while (*queue && *queue[i] >= *p) {
i++;
}
// Error no free slots
if (i == QUEUE_LEN) {
printf("No free slots.\r\n");
return;
}
// Insert process
if (!*queue) {
*queue[i] = p;
return;
}
else {
tmp = *queue[i];
*queue[i] = p;
}
// Increment the other processes
return;
}
int main(int argc, char** argv) {
int i;
for (i=0; i<5; i++) {
enqueue(&array[i], ptrs);
}
for (i=0; i<QUEUE_LEN; i++)
printf("%d\n", *(ptrs[i]));
return 0;
}
After first loop, i will remain zero. Here:
if (!*queue) {
*queue[i] = p;
return;
}
You check, that *queue is 0 and dereference it as well. It is UB.
PS. Btw, this:
*queue[i] = p;
Will not compiles, since *queue[i] has type int, but p has type int*.
// Find correct slot
while (*queue && *queue[i] >= *p) {
i++;
}
This will access some random memory address taken from uninitialized ptrs value.
Your check for *queue != 0 is not enough, you need to initialize array with zeores as:
int *ptrs[5] = {0};
And you still need to allocate memory you are attempting to write later when inserting.

Resources