简体   繁体   中英

operations on STACK of array of characters

i am getting a runtime error for the following program which is pretty self explanatory (see main() first)

GDB trace: Reading symbols from solution...done. [New LWP 17326] Core was generated by `solution'. Program terminated with signal SIGSEGV, Segmentation fault.

0 __strcat_sse2_unaligned ()

 at ../sysdeps/x86_64/multiarch/strcat-sse2-unaligned.S:46 

0 __strcat_sse2_unaligned ()

 at ../sysdeps/x86_64/multiarch/strcat-sse2-unaligned.S:46 

1 0x000055de73f4999a in strcat (__src=,

 __dest=<optimized out>) at /usr/include/x86_64-linux-gnu/bits/string_fortified.h:128 

2 append (w=, sptr=0x55de7530c260)

 at solution.c:31 

3 0x000055de73f49808 in main () at solution.c:90

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

typedef struct stack
{
    char** stk;
    int top;
}STACK;

void pop(STACK* s)
{
    s -> top--; 
}

char* peek(STACK* sptr)
{
    char* x = sptr -> stk[sptr -> top]; 
    return x;
}

void push(char* w, STACK* sptr)
{
    sptr -> stk[++sptr -> top] = w; 
}

void append(char* w, STACK* sptr)
{
    char* s1 = peek(sptr);
    strcat(s1, w);
    push(s1, sptr);

}

void del(int k, STACK* sptr)
{
    char* s = peek(sptr);
    char* s1;

    for(int i = 0; i < strlen(s) - k; ++i)
    {
        s1[i] = s[i];
    }

    push(s1, sptr);
}

void undo(STACK* sptr)
{
    pop(sptr);
}

char print(int k, STACK* sptr)
{
    char* s = peek(sptr);
    return s[k - 1];
}


int main()
{

    int times;
    int fn;
    char s[21];
    int k;
    STACK* sptr = (STACK*)malloc(sizeof(STACK));
    sptr -> stk = (char**)malloc(100 * sizeof(char*));


    // 1 append string s to existing
    // 2 delete last k chars
    // 3 print kth character of string
    // 4 undo

    // input times
    scanf("%d", &times);

    for(int i = 0; i < times; ++i)
    {
        // enter function number
        scanf("%d", &fn);

        switch(fn)
        {
            case 1: // take string

                    scanf("%s", s);
                    append(s, sptr);
                    break;

            case 2: // take k int

                    scanf("%d", &k);
                    del(k, sptr);
                    break;

            case 3: // k int

                    scanf("%d", &k);
                    char k = print(k, sptr);
                    printf("%c", k);
                    break;

            case 4: // just call pop
                    pop(sptr);
                    break;
        }
    }


    return 0;
}

edit 1: changed char* s to char s[21]; as pointed out by @Sourav Ghosh.

but getting another error as following ....... :/

Process 35026 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0) frame #0: 0x0000000100000c57 a.out`peek(sptr=0x0000000100100320) at undo.c:19 16 17 char* peek(STACK* sptr) 18 { -> 19 return sptr -> stk[sptr -> top]; 20 } 21 22 void push(char* w, STACK* sptr) Target 0: (a.out) stopped. (lldb)

The problem is, in your code

 scanf("%s", s);

s does not point to any valid memory. It is a local variable (pointer) with auto storage duration and not initialized explicitly, thus is holds an indeterminate value, which is an invalid address in context of your program.

Attempt to access that invalid memory invoke undefined behavior .

You need to either

  • make s point to a valid memory (you can use allocator functions like malloc() and family)
  • make s an array with predefined length, and length limit your input (much preferable, IMHO).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM