Wycięcie kawałka tekstu z tablicy char C

0

Witam!

Chciałbym wyciąć z tablicy (napisu) wszystko, co jest za znakiem ":" i zapisać to do innej tablicy (mniejszej odpowiednio). Ten kod rzuca mi seg fault i nie wiem czemu :< (Chcę wyciąć tylko 123)

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

static char* cutter(char *in)
{
    char *text;
    int index;
    const char *find = strchr(in, ':');
    if(find)
    {
        index = find - in;
        text = malloc(sizeof(char)*(sizeof(in)-index-2));
        memset(text, 0, sizeof(in)-index-2);
        memcpy(text, in+index+2, sizeof(in)-index-2);
    }
    else
    {
        exit(-1);
    }
    return text;
}

int main()
{
    char *text = cutter("sdisjfis: 123");
    printf("%s\n", text);
    return 0;
}
0

Bo ten kod to jakis WTF?
Nie masz debugera? Zobacz sobie jaki rozmiar mallocujesz. sizeof na wskaźniku zwróci ROZMIAR WSKAŹNIKA. Dlugość stringa zwróci ci strlen()

0

@Shalom: tak, masz rację, dzisiaj padam już i piszę głupoty. Poprawiłem kod:

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

static char* cutter(char *in)
{
    char *text;
    int index;
    const char *find = strchr(in, ':');
    if(find)
    {
        index = find - in;
        text = malloc(sizeof(char)*(strlen(in)-index-2));
        memset(text, 0, strlen(in)-index-2);
        memcpy(text, in+index+2, strlen(in)-index-2);
    }
    else
    {
        exit(-1);
    }
    return text;
}

int main()
{
    char *text = cutter("sdisjfis: 123");
    printf("%s\n", text);
    free(text);
    return 0;
}

i działa (wyświetla 123), aczkolwiek valgrind pokazuje mi:

$ valgrind --tool=memcheck --leak-check=yes -v ./Untitled1
==3168== Memcheck, a memory error detector
==3168== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3168== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==3168== Command: ./Untitled1
==3168==
--3168-- Valgrind options:
--3168-- --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--3168-- --tool=memcheck
--3168-- --leak-check=yes
--3168-- -v
--3168-- Contents of /proc/version:
--3168-- Linux version 3.8.0-29-generic (buildd@akateko) (gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) ) #42-Ubuntu SMP Tue Aug 13 23:12:18 UTC 2013
--3168-- Arch and hwcaps: X86, x86-sse1-sse2
--3168-- Page sizes: currently 4096, max supported 4096
--3168-- Valgrind library directory: /usr/lib/valgrind
--3168-- Reading syms from /lib/i386-linux-gnu/ld-2.17.so
--3168-- Considering /lib/i386-linux-gnu/ld-2.17.so ..
--3168-- .. CRC mismatch (computed a0fb8cb1 wanted 57bd7c4c)
--3168-- Considering /usr/lib/debug/lib/i386-linux-gnu/ld-2.17.so ..
--3168-- .. CRC is valid
--3168-- Considering /usr/lib/debug/lib/i386-linux-gnu/ld-2.17.so ..
--3168-- .. CRC is valid
--3168-- Reading syms from /home/dom/Pobrane/Untitled1
--3168-- Reading syms from /usr/lib/valgrind/memcheck-x86-linux
--3168-- Considering /usr/lib/valgrind/memcheck-x86-linux ..
--3168-- .. CRC mismatch (computed 58cc7e37 wanted aecfbbb2)
--3168-- object doesn't have a symbol table
--3168-- object doesn't have a dynamic symbol table
--3168-- Scheduler: using generic scheduler lock implementation.
--3168-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--3168-- Reading suppressions file: /usr/lib/valgrind/default.supp
==3168== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-3168-by-dom-on-???
==3168== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-3168-by-dom-on-???
==3168== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-3168-by-dom-on-???
==3168==
==3168== TO CONTROL THIS PROCESS USING vgdb (which you probably
==3168== don't want to do, unless you know exactly what you're doing,
==3168== or are doing some strange experiment):
==3168== /usr/lib/valgrind/../../bin/vgdb --pid=3168 ...command...
==3168==
==3168== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==3168== /path/to/gdb ./Untitled1
==3168== and then give GDB the following command
==3168== target remote | /usr/lib/valgrind/../../bin/vgdb --pid=3168
==3168== --pid is optional if only one valgrind process is running
==3168==
--3168-- REDIR: 0x40185b0 (strlen) redirected to 0x3806af24 (???)
--3168-- REDIR: 0x4018330 (index) redirected to 0x3806aeff (???)
--3168-- Reading syms from /usr/lib/valgrind/vgpreload_core-x86-linux.so
--3168-- Considering /usr/lib/valgrind/vgpreload_core-x86-linux.so ..
--3168-- .. CRC mismatch (computed 8fb3125a wanted 0d923127)
--3168-- object doesn't have a symbol table
--3168-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so
--3168-- Considering /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so ..
--3168-- .. CRC mismatch (computed efa74f7b wanted 304220ae)
--3168-- object doesn't have a symbol table
--3168-- Reading syms from /lib/i386-linux-gnu/libc-2.17.so
--3168-- Considering /lib/i386-linux-gnu/libc-2.17.so ..
--3168-- .. CRC mismatch (computed cd57cc0a wanted 572e0357)
--3168-- Considering /usr/lib/debug/lib/i386-linux-gnu/libc-2.17.so ..
--3168-- .. CRC is valid
--3168-- Considering /usr/lib/debug/lib/i386-linux-gnu/libc-2.17.so ..
--3168-- .. CRC is valid
--3168-- REDIR: 0x40ca780 (strnlen) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x40cbd00 (strncasecmp) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x40d09c0 (memrchr) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x40e5230 (wcslen) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x40ca920 (__GI_strrchr) redirected to 0x402c880 (__GI_strrchr)
--3168-- REDIR: 0x40c9cd0 (index) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x40e2230 (__strchr_sse2_bsf) redirected to 0x402c940 (index)
--3168-- REDIR: 0x40ca670 (strlen) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x40d1190 (__strlen_sse2_bsf) redirected to 0x402cd20 (strlen)
--3168-- REDIR: 0x40c5cd0 (malloc) redirected to 0x402c3b0 (malloc)
--3168-- REDIR: 0x40cb7d0 (memset) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x4182a90 (__memset_sse2) redirected to 0x402ef60 (memset)
--3168-- REDIR: 0x40cbe30 (memcpy) redirected to 0x4024580 (_vgnU_ifunc_wrapper)
--3168-- REDIR: 0x4183ba0 (__memcpy_ssse3) redirected to 0x402e050 (memcpy)
--3168-- REDIR: 0x40ca6c0 (__GI_strlen) redirected to 0x402cd40 (__GI_strlen)
==3168== Invalid read of size 1
==3168== at 0x402CD53: __GI_strlen (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3168== by 0x40B3F99: puts (ioputs.c:36)
==3168== by 0x4065934: (below main) (libc-start.c:260)
==3168== Address 0x420102b is 0 bytes after a block of size 3 alloc'd
==3168== at 0x402C418: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3168== by 0x80485BB: cutter (in /home/dom/Pobrane/Untitled1)
==3168== by 0x8048641: main (in /home/dom/Pobrane/Untitled1)
==3168==
123
--3168-- REDIR: 0x40c60d0 (free) redirected to 0x402b160 (free)
==3168==
==3168== HEAP SUMMARY:
==3168== in use at exit: 0 bytes in 0 blocks
==3168== total heap usage: 1 allocs, 1 frees, 3 bytes allocated
==3168==
==3168== All heap blocks were freed -- no leaks are possible
==3168==
==3168== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==3168==
==3168== 1 errors in context 1 of 1:
==3168== Invalid read of size 1
==3168== at 0x402CD53: __GI_strlen (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3168== by 0x40B3F99: puts (ioputs.c:36)
==3168== by 0x4065934: (below main) (libc-start.c:260)
==3168== Address 0x420102b is 0 bytes after a block of size 3 alloc'd
==3168== at 0x402C418: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3168== by 0x80485BB: cutter (in /home/dom/Pobrane/Untitled1)
==3168== by 0x8048641: main (in /home/dom/Pobrane/Untitled1)
==3168==
==3168== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
dom@komputer:~/Pobrane$

Nie bardzo mogę zrozumieć jaki to error?

0
char *cutter(char *in)
  {
   const char *find=strchr(in,':');
   return find?strdup(find+1):0;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1