Thread: Difference between using char [] and malloc-ed char* in this code

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    5

    Question Difference between using char [] and malloc-ed char* in this code

    Hello folks! I am currently practicing my C skills by coding a calculator that takes infix input and turns it into postfix format then uses stack to calculate the expression.

    This is the transition function that takes in infix format and outputs postfix format.

    Code:
    int getPriority(char* symbol){
        if(!strcmp(symbol, "(")) return 0;
        if(!strcmp(symbol, "+") || !strcmp(symbol, "-")) return 1;
        if(!strcmp(symbol, "*") || !strcmp(symbol, "/")) return 2;
        return 3;
    }
    
    
    char* transition(Stack *stack, char* input){
    
    //    char* postfix = (char*)malloc(sizeof(char)*10000);
    //    memset(postfix, 0x00, 10000);
        char postfix[10000] = "";
        char* token = strtok(input, " ");
        while(token != NULL){
            if(!strcmp(token, "(")){
                push(stack, token);
            }
            else if(!strcmp(token, ")")){
                while(strcmp(getTop(stack), "(")){
    
                    strcat(postfix, pop(stack));
                    strcat(postfix, " ");
                }
    
                pop(stack);
            }
            else if(!strcmp(token, "+") || !strcmp(token, "-") || !strcmp(token, "*") || !strcmp(token, "/")){
    
                while(stack->top != NULL && getPriority(getTop(stack)) >= getPriority(token)){
    
                    strcat(postfix, pop(stack));
                    strcat(postfix, " ");
                }
                push(stack, token);
            }
            else{
    
                strcat(postfix, token);
                strcat(postfix, " ");
            }
            token = strtok(NULL, " ");
    
        }
        while(stack->top != NULL ){
    
            strcat(postfix, pop(stack));
            strcat(postfix, " ");
        }
        return postfix;
    }
    This function returns postfix character array, which is currently implemented in the function as
    Code:
    char postfix[10000] = "";
    I've tried the malloc method which is right above the above code, and works equally fine.

    What is the difference between these two implementations? I am suspecting that it has to do something with the memory layout of the program, since malloc will definitely create space for postfix pointer in the heap.

    But I am not sure where the character array postfix will leave its mark in the memory layout. If it is stored in the stack, then I am not sure why it won't return memory error during runtime since the stack memory is freed after its return in the main function.

    Thanks for helping out.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    You're right.
    As written (with an array), the string in postfix will be lost when the function returns.

    You either need to use malloc (in which case you need to free the returned pointer),
    or make the array static (as in static char postfix[1000];)
    But the static array creates some re-entrancy problems.

    The best approach is to supply the char pointer (and it's size) as parameters for maximum flexibility.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    5
    Thanks for your prompt reply.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    5
    Then can you or anyone knowledgeable tell me why the integer variable defined in the lpop function won't return NULL in the main function like the transition function in the thread, if both int data variable and char postfix[10000] variable are located in the stack region?

    Code:
    int data = node->data;
    //...
    return data;

    Code:
    //linked list based queue
    
    typedef struct Node{
        int data;
        struct Node *next;
    }Node;
    
    typedef struct Queue{
        Node* front;
        Node* rear;
    }Queue;
    
    void lpush(Queue *queue, int data){
        Node *node = (Node*)malloc(sizeof(Node));
        node->data = data;
        Node* cur = queue->rear;
        if(cur == NULL){
            queue->front = node;
        }
        else{
            cur->next = node;
        }
        queue->rear = node;
        return;
    }
    
    int lpop(Queue *queue){
    
        Node* node = queue->front;
        int data = node->data;
        queue->front = node->next;
        free(node);
        return data;
    }
    
    int main()
    {
        Queue queue;
        queue.front = NULL;
        queue.rear = NULL;
        lpush(&queue, 1);
        lpush(&queue, 2);
        lpush(&queue, 3);
        lpush(&queue, 4);
        lpush(&queue, 5);
        for(int i = 0; i < 5; i++){
            printf("%d\n",lpop(&queue ));
        }
    
    
        system("pause");
        return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    Because your int is a scalar, so you get a copy of the variable returned.

    Whereas your postfix is an array, so the return is actually a pointer to the first element of the array.
    In the same way you get a pointer to the first element when you pass an array parameter into a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between char * and char [] arguments?
    By stevenswj in forum C Programming
    Replies: 4
    Last Post: 11-28-2010, 08:36 AM
  2. Difference between char *str1 & const char *str2
    By Tigers! in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 04:04 AM
  3. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  4. Replies: 16
    Last Post: 04-01-2008, 10:15 AM
  5. difference between: const char *, char *
    By creeping_death in forum C Programming
    Replies: 3
    Last Post: 08-04-2003, 11:51 PM

Tags for this Thread