Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Seg fault while parsing a line into ***char

[too long for a comment]

This line

*argv++ = line; /* with char ** argv */

refers to invalid memory, as the code does *argv[n] (with char **argv[64]) which refers nothing.

The namings you use do not make live easier.

Try the following naming:

void parse(char *line, char **cmd)
    while (*line != '') {     
        while (*line == ' ' || *line == '	' ||
*line == '
            *line++ = ''; 
        *cmd++ = line;     
        while (*line != '' && *line != '
' && *line != '	' && *line != '
    *argv = '';  

void parsePipe(char *line, char ***cmdline)
    char *cmd = strtok(line, "|");
    int word_counter = 0;

    while (cmd != NULL) 
       printf("Printing word -> %s
", cmd);
       parse(cmd, *cmdline++);

       cmd = strtok(NULL, "|");

    printf("This string contains %d words
separated with |

void  main(void)
    char  line[1024];          
    char  **cmdline[64]; 
    while (1) {    
        printf("lsh -> ");   
        parsePipe(line, cmdline);

For none of the cmds used memory had been allocated.


*cmd++ = line;

fails, as cmd points nowhere, but gets dereferenced and the code tries to write to where it's pointing, which is nowhere, that is invalid memory.

Fixing this can be done by passing char*** to parse(char *** pcmd) and counting the tokens found

size_t nlines = 0;



and the doing a

*pcmd = realloc(*pcmd, nlines + 1); /*
Allocate one more as needed to later find the end
of the array. */
(*pcmd)[nlines -1] = line;
(*pcmd)[nlines] = NULL; /* Initialise the stopper,
marking the end of the array. */

for each token found.

Obviously you need to call it like this:

 parse(cmd, cmdline++);

To have all this work the inital array needs to initialised properly (as you should have done anyway):

char  **cmdline[64] = {0};

Categories : C

Related to : Seg fault while parsing a line into ***char
GSON parsing with Retrofit parsing Flickr JSON response
Simply add the nojsoncallback param You can find it documented here

Categories : Java
I do not know where my fault is
Make sure each class is in separate file. They have to be since they're public. Besides, make sure all the classes are in the same package. Otherwise the r1.radius and r2.radius or c.Sum(r1, r2) calls won't work. Of course, you also miss two } at the end of the snippet. Not sure if it's only matter of pasting. However, in all cases you should get compilation error, so probably it's a matter of

Categories : Java
AVL Tree Insertion Causes Seg Fault
This is because every time you insert a function, the following codes are executed: if(!(*tree)) { *tree = item; printf("Root: "); /*Every node seems to get printed here */ (*tree)->height = 0; return; } The reason is that insert() is called recursively. if(strcmp(item->key,(*tree)->key) < 0) { insert(&(*tree)->left, item); } else

Categories : C
Segment fault with execvp
I'm seeing a problem with the following code: char *args[6] = {"tar","-xzvf", strncpy(args[2],argv[0]), "-C", "/home/bg/Desktop /Project/", NULL}; Specifically, with the strncpy for two reasons: 1) strncpy() takes three arguments, and you have only specified 2, and 2) it is attempting to copy argv[0] (the program name) into args[2], which is an array of pointers, not a buffer to contain the pr

Categories : C
Segmentation fault on glShaderSource()
You are using the C syntax incorrectly: char buffer[1024]; ... glShaderSource(..., (const char **)&buffer, ...); This is not a pointer to a pointer to the real string data. By definition, &buffer is the same as buffer here - a pointer to the first char in buffer, of type char *. Casting that to something else only conceals the error here. This construct can not work in principle. There

Categories : C++
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 Publishing Limited. All rights reserved.