0

I'm writing a BST and have to print it in order, to do fulfill the nodes I'm using strcpy, what givemes a SIGSEGV error and a message in valgrid:

==104951== Memcheck, a memory error detector
==104951== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==104951== Using Valgrind-3.20.0 and LibVEX; rerun with -h for copyright info
==104951== Command: ./bst.o
==104951== 
==104951== Use of uninitialised value of size 8
==104951==    at 0x484975C: strcpy (vg_replace_strmem.c:558)
==104951==    by 0x40115A: main (in /home/lukaswsantos/Documents/algorithms/bst.o)
==104951== 
==104951== Invalid write of size 1
==104951==    at 0x484975C: strcpy (vg_replace_strmem.c:558)
==104951==    by 0x40115A: main (in /home/lukaswsantos/Documents/algorithms/bst.o)
==104951==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==104951== 
==104951== 
==104951== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==104951==  Access not within mapped region at address 0x0
==104951==    at 0x484975C: strcpy (vg_replace_strmem.c:558)
==104951==    by 0x40115A: main (in /home/lukaswsantos/Documents/algorithms/bst.o)
==104951==  If you believe this happened as a result of a stack
==104951==  overflow in your program's main thread (unlikely but
==104951==  possible), you can try to increase the size of the
==104951==  main thread stack using the --main-stacksize= flag.
==104951==  The main thread stack size used in this run was 8388608.
==104951== 
==104951== HEAP SUMMARY:
==104951==     in use at exit: 0 bytes in 0 blocks
==104951==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==104951== 
==104951== All heap blocks were freed -- no leaks are possible
==104951== 
==104951== Use --track-origins=yes to see where uninitialised values come from
==104951== For lists of detected and suppressed errors, rerun with: -s
==104951== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
fish: Job 1, 'valgrind  ./bst.o' terminated by signal SIGSEGV (Address boundary error)

The code:


#include "stdio.h"
#include "string.h"

struct node {
    char data[255];
    struct node *node_left;
    struct node *node_rigth 
};

int number = 0;
int pointer = 0;

const char *NODE_0 = "node 0";


int main (int number, struct node *leaf, struct node *tree) {
    struct node node_zero;
    strcpy(node_zero.node_left->data, NODE_0);

    return 0;
}

With the error message by valgrind and the code I expect to compile, but gets and error.

What I could doing wrong?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105

0 Answers0