Thread: segmentation violation??

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    45

    segmentation violation??

    Hi, I'm on excersise 5-6 in The C Programming Language book and while making the itoa function program encountered a seg fault...
    Code:
    /* itoa.c */
    /* second version of itoa
    * this time using pointers
    * instead of array indexing
    */
    
    /* includes */
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    
    /* function prototypes */
    int batoi(char *s);
    char *bitoa(int n);
    void reverse(char *s);
    int bstrlen(char *s);
    
    int main(int argc, char **argv)
    {
    	if (argc != 2) {         /* print usage and exit */
    		fprintf(stderr, "Usage: %s <num>\n", argv[0]);
    		return EXIT_FAILURE;
    	}
    	char *string = argv[1];
    	printf("\n\nConverting string to integer...\n\n");
    	sleep(5);
    	int integer = batoi(string);
    	printf("Just to clarify here is the number (+1 to prove its a number): %d\n", integer + 1);
    	sleep(5);
    	printf("\n\nConverting back to string format...\n\n");
    	sleep(5);
    	string = bitoa(integer);
    	printf("And here's the resulting string: %s\n", string);
    	return EXIT_SUCCESS;
    }
    
    int batoi(char *s)
    {	
    	int num, sign;
    	sign = (*s == '-') ? -1 : 1;
    	for (num = 0; *s != '\0'; s++)            /* get number */
    		num = 10 * num + (*s - '0');
    	return num * sign;
    }
    
    char *bitoa(int n)
    {
    	int sign = n;
    	char *buffer, *ptr;          /* used for the resulting string */
    	ptr = buffer;
    	if (n < 0)
    		n = -n;                  /* make positive for manipulation */
    	do {
    		*buffer = n % 10 + '0';                  /* get digit */
    		++buffer;
    	} while (n /= 10);
    	if (sign < 0) {             /* if original number was negative then add the '-' symbol to the string */
    		*buffer = '-';
    		++buffer;
    	}
    	*buffer = '\0';
    	reverse(buffer);
    	return ptr;
    }
    
    void reverse(char *s)
    {
    	int temp;
    	int count_s, count_t;
    	char *t = s;
    	for (*t += bstrlen(s), count_s = 0, count_t = bstrlen(s); count_s < count_t; ++s, ++count_s, --t, --count_t) {          /* reverse the string, swapping each char on opposite sides until s and t meet halfway */
    		temp = *s;
    		*s = *t;
    		*t = temp;
    	}
    }
    
    int bstrlen(char *s)
    {
    	int num = 0;
    	while (*s) {      /* count chars */
    		++num;
    		++s;
    	}
    	return num;
    }
    I'm sure its the bitoa function and I've tried allsorts to solve the problem.
    I got rid of the seg fault by just returning a pointer to a normal string like this...
    Code:
    char *bitoa(int n)
    {
    	char buffer[] = "hello world";
    	char *ptr = buffer;
    	return ptr;
    }
    but that results in printing random binary chars, I don't know what else to try??

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Most compilers will give you an error message that conatains a line number in your source code...
    Post your errors and, if possible, mark the line that's giving you the error...

    In bitoa() you are not allocating memory for your string. A pointer is not a string.

    For your smaller code segment, you need to know that C cannot return an array or a pointer to the array. The char array you created in your function is destroyed as the function exits. If you want to return a pointer to a string you need to use malloc() to allocate space for it and then you will need to free() that memory when you are done with it.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippet:
    Code:
    char *bitoa(int n)
    {
    	char buffer[] = "hello world";
    	char *ptr = buffer;
    	return ptr;
    }
    You are trying to return an address to a local variable. When the function returns to the calling function this local variable is no longer valid.

    Jim

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    ok, thanks i will try malloc() and free()

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    Instead of returning a pointer I have just added a string as an extra parameter to the bitoa() function.
    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation violation
    By gooseantlers in forum C Programming
    Replies: 5
    Last Post: 04-20-2011, 10:41 PM
  2. Access Violation(Segmentation Fault) error
    By nirvana619 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 08:43 AM
  3. An Access Violation Segmentation Fault. Need Help ASAP
    By darknessfalls in forum C Programming
    Replies: 2
    Last Post: 08-22-2010, 05:56 AM
  4. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM