Skip to main content
Post Closed as "Not suitable for this site" by Eric Postpischil, Zephyr, Rohit Gupta
code block for error message
Source Link
mch
  • 9.8k
  • 3
  • 33
  • 46

Here's the exact message: Program received signal SIGTRAP, Trace/breakpoint trap.

#13 0x00007ffd7348d235 in ucrtbase!_realloc_base () from C:\WINDOWS\System32\ucrtbase.dll

#14 0x00007ff7727c175b in resizeQueue (queue=0x5ffeb0) at queue.c:45

Program received signal SIGTRAP, Trace/breakpoint trap.

#13 0x00007ffd7348d235 in ucrtbase!_realloc_base () from C:\WINDOWS\System32\ucrtbase.dll

#14 0x00007ff7727c175b in resizeQueue (queue=0x5ffeb0) at queue.c:45

Here's the exact message: Program received signal SIGTRAP, Trace/breakpoint trap.

#13 0x00007ffd7348d235 in ucrtbase!_realloc_base () from C:\WINDOWS\System32\ucrtbase.dll

#14 0x00007ff7727c175b in resizeQueue (queue=0x5ffeb0) at queue.c:45

Here's the exact message:

Program received signal SIGTRAP, Trace/breakpoint trap.

#13 0x00007ffd7348d235 in ucrtbase!_realloc_base () from C:\WINDOWS\System32\ucrtbase.dll

#14 0x00007ff7727c175b in resizeQueue (queue=0x5ffeb0) at queue.c:45
added 81 characters in body
Source Link
Ted Lyngmo
  • 123.7k
  • 7
  • 93
  • 155
typedef struct Queue
{
    int tail; // index of the last element
    int head; // index of the first element
    int capacity;
    char** head_pt;
} Queue;


void allocQueue(Queue* queue) 
{
    assert(queue->capacity != 0);
    queue->head_pt = (char**)malloc(sizeof(char*) * queue->capacity);

    for (int i = 0; i < queue->capacity; i++) 
    {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
}

void enQueue(Queue* queue, char* data) 
{
    if (queue->head == -1 && queue->tail == -1) {
        queue->head = 0;
        queue->tail = 0;
    }

    if(queue->tail == queue->capacity) 
    {
        resizeQueue(queue); 
    }

    strcpy(queue->head_pt[queue->tail], data);   
    queue->tail++;
}

void resizeQueue(Queue* queue) 
{
    printf("RESIZE\n");
    queue->capacity *= 2;
    queue->head_pt = (char**) realloc(queue->head_pt, queue->capacity);
    if (!queue->head_pt) {
        printf("Queue Error: failed reallocating memory for head");
        exit(1);
    }

    for(int i = queue->tail; i < queue->capacity; i++) {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
} 
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct Queue
{
    int tail; // index of the last element
    int head; // index of the first element
    int capacity;
    char** head_pt;
} Queue;

void allocQueue(Queue* queue) 
{
    assert(queue->capacity != 0);
    queue->head_pt = (char**)malloc(sizeof(char*) * queue->capacity);

    for (int i = 0; i < queue->capacity; i++) 
    {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
}

void resizeQueue(Queue* queue) 
{
    printf("RESIZE\n");
    queue->capacity *= 2;
    queue->head_pt = (char**) realloc(queue->head_pt, queue->capacity);
    if (!queue->head_pt) {
        printf("Queue Error: failed reallocating memory for head");
        exit(1);
    }

    for(int i = queue->tail; i < queue->capacity; i++) {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
}

void enQueue(Queue* queue, char* data) 
{
    if (queue->head == -1 && queue->tail == -1) {
        queue->head = 0;
        queue->tail = 0;
    }

    if(queue->tail == queue->capacity) 
    {
        resizeQueue(queue); 
    }

    strcpy(queue->head_pt[queue->tail], data);   
    queue->tail++;
}
int main(void) {
    printf("------test Queue-----\n");
    Queue queue = {
        .capacity = 5,
        .tail = -1,
        .head = -1,
    };
    allocQueue(&queue);

    assert(queue.head_pt != NULL);
    printf("PASSED: allocating memory\n");

    
    char string [] = "a,b,c,d,e,f,g,h,i,j,k,";

    for (char* data = strtok(string, ","); data != NULL; data = strtok(NULL, ",") ) {
        enQueue(&queue, data);
        // printQueue(&queue);
    }
 return 0
}

int main(void) {
    printf("------test Queue-----\n");
    Queue queue = {
        .capacity = 5,
        .tail = -1,
        .head = -1,
    };
    allocQueue(&queue);

    assert(queue.head_pt != NULL);
    printf("PASSED: allocating memory\n");

    
    char string [] = "a,b,c,d,e,f,g,h,i,j,k,";

    for (char* data = strtok(string, ","); data != NULL; data = strtok(NULL, ",") ) {
        enQueue(&queue, data);
        // printQueue(&queue);
    }
    return 0;
}
typedef struct Queue
{
    int tail; // index of the last element
    int head; // index of the first element
    int capacity;
    char** head_pt;
} Queue;


void allocQueue(Queue* queue) 
{
    assert(queue->capacity != 0);
    queue->head_pt = (char**)malloc(sizeof(char*) * queue->capacity);

    for (int i = 0; i < queue->capacity; i++) 
    {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
}

void enQueue(Queue* queue, char* data) 
{
    if (queue->head == -1 && queue->tail == -1) {
        queue->head = 0;
        queue->tail = 0;
    }

    if(queue->tail == queue->capacity) 
    {
        resizeQueue(queue); 
    }

    strcpy(queue->head_pt[queue->tail], data);   
    queue->tail++;
}

void resizeQueue(Queue* queue) 
{
    printf("RESIZE\n");
    queue->capacity *= 2;
    queue->head_pt = (char**) realloc(queue->head_pt, queue->capacity);
    if (!queue->head_pt) {
        printf("Queue Error: failed reallocating memory for head");
        exit(1);
    }

    for(int i = queue->tail; i < queue->capacity; i++) {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
} 
int main(void) {
    printf("------test Queue-----\n");
    Queue queue = {
        .capacity = 5,
        .tail = -1,
        .head = -1,
    };
    allocQueue(&queue);

    assert(queue.head_pt != NULL);
    printf("PASSED: allocating memory\n");

    
    char string [] = "a,b,c,d,e,f,g,h,i,j,k,";

    for (char* data = strtok(string, ","); data != NULL; data = strtok(NULL, ",") ) {
        enQueue(&queue, data);
        // printQueue(&queue);
    }
 return 0
}

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct Queue
{
    int tail; // index of the last element
    int head; // index of the first element
    int capacity;
    char** head_pt;
} Queue;

void allocQueue(Queue* queue) 
{
    assert(queue->capacity != 0);
    queue->head_pt = (char**)malloc(sizeof(char*) * queue->capacity);

    for (int i = 0; i < queue->capacity; i++) 
    {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
}

void resizeQueue(Queue* queue) 
{
    printf("RESIZE\n");
    queue->capacity *= 2;
    queue->head_pt = (char**) realloc(queue->head_pt, queue->capacity);
    if (!queue->head_pt) {
        printf("Queue Error: failed reallocating memory for head");
        exit(1);
    }

    for(int i = queue->tail; i < queue->capacity; i++) {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
}

void enQueue(Queue* queue, char* data) 
{
    if (queue->head == -1 && queue->tail == -1) {
        queue->head = 0;
        queue->tail = 0;
    }

    if(queue->tail == queue->capacity) 
    {
        resizeQueue(queue); 
    }

    strcpy(queue->head_pt[queue->tail], data);   
    queue->tail++;
}
int main(void) {
    printf("------test Queue-----\n");
    Queue queue = {
        .capacity = 5,
        .tail = -1,
        .head = -1,
    };
    allocQueue(&queue);

    assert(queue.head_pt != NULL);
    printf("PASSED: allocating memory\n");

    
    char string [] = "a,b,c,d,e,f,g,h,i,j,k,";

    for (char* data = strtok(string, ","); data != NULL; data = strtok(NULL, ",") ) {
        enQueue(&queue, data);
        // printQueue(&queue);
    }
    return 0;
}
Source Link

dynamically resizing array in queue implementation

I'm trying to write a queue that will store strings. Using GDB I can tell that I make a memory allocation error in the function resizeQueue.

Here's the exact message: Program received signal SIGTRAP, Trace/breakpoint trap.

#13 0x00007ffd7348d235 in ucrtbase!_realloc_base () from C:\WINDOWS\System32\ucrtbase.dll

#14 0x00007ff7727c175b in resizeQueue (queue=0x5ffeb0) at queue.c:45

I would appreciate if someone could look over my code and see if they see what's wrong. I attached a complete example below.

To clarify one point, I'm aware that the tail is 5 when there is no array index 5, that's intentional because it's that fact that triggers the condition to tell my program to resize. It's not dereferenced when it's out-of-bounds. However, I could be wrong in my logic... When stepping through it seemed like it failed allocating space for the 10 element (9 index).

Relevant queue code

typedef struct Queue
{
    int tail; // index of the last element
    int head; // index of the first element
    int capacity;
    char** head_pt;
} Queue;


void allocQueue(Queue* queue) 
{
    assert(queue->capacity != 0);
    queue->head_pt = (char**)malloc(sizeof(char*) * queue->capacity);

    for (int i = 0; i < queue->capacity; i++) 
    {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
}

void enQueue(Queue* queue, char* data) 
{
    if (queue->head == -1 && queue->tail == -1) {
        queue->head = 0;
        queue->tail = 0;
    }

    if(queue->tail == queue->capacity) 
    {
        resizeQueue(queue); 
    }

    strcpy(queue->head_pt[queue->tail], data);   
    queue->tail++;
}

void resizeQueue(Queue* queue) 
{
    printf("RESIZE\n");
    queue->capacity *= 2;
    queue->head_pt = (char**) realloc(queue->head_pt, queue->capacity);
    if (!queue->head_pt) {
        printf("Queue Error: failed reallocating memory for head");
        exit(1);
    }

    for(int i = queue->tail; i < queue->capacity; i++) {
        queue->head_pt[i] = (char*)malloc(sizeof(char) * QUEUE_ELEM_SIZE);
        if (!queue->head_pt[i]) {
            printf("Queue Error: failed reallocating memory for a string");
            exit(1); 
        }
    }
} 

This is the test file

int main(void) {
    printf("------test Queue-----\n");
    Queue queue = {
        .capacity = 5,
        .tail = -1,
        .head = -1,
    };
    allocQueue(&queue);

    assert(queue.head_pt != NULL);
    printf("PASSED: allocating memory\n");

    
    char string [] = "a,b,c,d,e,f,g,h,i,j,k,";

    for (char* data = strtok(string, ","); data != NULL; data = strtok(NULL, ",") ) {
        enQueue(&queue, data);
        // printQueue(&queue);
    }
 return 0
}

Thanks to anyone who actually read all of that :)