logo
down
shadow

using strtol on a string literal causing segmentation fault


using strtol on a string literal causing segmentation fault

Content Index :

using strtol on a string literal causing segmentation fault
Tag : c , By : user107021
Date : November 24 2020, 03:41 PM

this one helps. the issue is that ptr isn't initialised. So when strtol tries to write at the address ptr, it crashes (or undefined behaviour).
You have to pass the valid address of a pointer to store the last unprocessed char, like:
char *ptr;
long ret = strtol(temp, &ptr, 10);

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Why does modifying a string literal cause a segmentation fault?


Tag : c , By : wpoch
Date : March 29 2020, 07:55 AM
this will help it is in a read only section of the process's address space because the string literal is a constant. (the str variable is not in read only space but it should be made to point to a valid writable location before you can use it the way you do.)

Segmentation fault with strtol


Tag : c , By : Tom Berthon
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Your crash has nothing to do with strtol. The problem is that you dereference a pointer that has the value NULL. That is illegal and causes a crash (seg fault).
Your problem is here:
b=strtol(*endptr,endptr,0);
         ^^^^^^^
         Dereference a NULL leads to a crash
char** endptr=NULL;
char* p = *endptr;  // Crash!!
char a[]=" 0xa this is a343 good";
char* p;   // Notice: just one * as you need a pointer to char
long int b=0;
b=strtol(a, &p,0);
            ^^
            Notice: & (aka address of). So you pass the address of 
                    a pointer to char. Equivalent to char** as expected
                    by strtol

strtol()/atol() causeing segmentation fault


Tag : c , By : deanschang
Date : March 29 2020, 07:55 AM
This might help you So I pigured out what caused the problem. I found out that fgets didn't return EOF when I thought it would, because it catches '\n' before EOF, so I ended up handling a string '\n' like it was a true line of information what caused strtol get a NULL pointer, which caused a Segmentation Fault error. The fix was relatively easy; I had to prevent from this while loop:
while(fgets(line, 100, resfile) != NULL){
    char* token;
    refer = line;
    token = strsep(&refer, "\t");
    long ID = atoi(token);
    char* name = strsep(&refer, "\t");
    token = strsep(&refer, "\t");
    if(token = NULL){
        printf("X\n");
        return 0;
    }
    long qty = atol(token);
}

Segmentation fault reversing a string literal


Tag : c , By : Robby
Date : March 29 2020, 07:55 AM

Segmentation Fault on strtol with in pthread function


Tag : c , By : phil
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Parenthesis operator in C. What is the effect in the following code
  • Is there table with timing(cost) of C functions?
  • How can I access a global pointer outside of a C function?
  • Are fopen/fread/fgets PID-safe in C?
  • How can multiple variables be passed to a function cleanly in C?
  • How to use pthread_atfork() and pthread_once() to reinitialize mutexes in child processes
  • Find function call in a list of binaries
  • How to return a single variable from a CUDA kernel function?
  • How to get number of cores in Win32?
  • Handling aces and finding a segfault in a blackjack program
  • function prototype declarations
  • How to convert from Hex to Decimal using only Integers in C
  • How to parse from char array using yacc?
  • I can not work with linked list inside linked list
  • Finding the index of largest number
  • Decompose a number to product of prime numbers and print it like 18=2*3^2
  • programming EXACT timer which calles a function in c language every x seconds
  • Combining macros generate errors
  • Why am I not getting error when I store values of different types in an array in C language?
  • What is the purpose of the base case dp[0] = -1 of this DP array in the palindromic partitions problem?
  • Having function choose largest number of given ones
  • Data structures layout
  • How to retrieve probuf from C library
  • Printf float number in hexadecimal format returns random vlaue
  • Memory allocation in C from 1 to N
  • Emit signal from separate thread in glib/gtk
  • How to fix Valgrind "Conditional Jump..." error
  • The text to be printed is being printed on the command line rather than print space while using fork on terminal
  • Deleting an element from an array of structures
  • How to read time stamp data from text file using C code?
  • I can't understand this code? Is it normal or I'm not ready?
  • GTK character count vs byte index
  • Summing up integers in an array and storing the result in an array
  • JITtting with limited knowledge of the target architecture
  • Why does global pointers behave erratically in this function?
  • Write a basic half pyramid pattern program
  • Read memory in C
  • void affecting output of the program
  • How to limit scanf while reading input from file
  • Why does the C runtime not call my exit()?
  • Can anyone say why the array is not changed when passed to a function
  • Integer overflow (Smallest file size that will make the integer in program become negative in bits)
  • Array of pointers to strings(base type of pointers)
  • Is there any way to limit integer datatype value range
  • How to print 2-byte unicode characters
  • The execution of open when using O_CREAT
  • Parsing Command Line Arguments for Shell
  • Undefined length of character arrays
  • Do condition formats in if/else if statements have to match in C?
  • Generate ordered passwords for brute forcing in C
  • Problems with pointer into structure
  • Why does this C program print output "10" irrespective of the for loop?
  • taking output 2D array in matrix form
  • sizeof char pointer and pointer to pointer
  • does the following program cause memory leak?
  • Innacurate file readings from fopen and/or fscanf
  • Searching an element in an Array using Recursive Function in C Language
  • Most fastest C code to count recursively directories in Linux ( without files )
  • Why does C not offer syntactically transparent references like C++ and Java do?
  • Code doesn't get excuted after using continue in while loop
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com