segmentation fault on assigning char to a char of a string at index

This is a discussion on segmentation fault on assigning char to a char of a string at index within the C Programming forums, part of the General Programming Boards category; Hey folks sup! I have a segmentation fault here: Code: char * inttobinstr(int n) { char *binstr; binstr = (char*)malloc(sizeof(char)*8); ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    46

    segmentation fault on assigning char to a char of a string at index

    Hey folks sup!
    I have a segmentation fault here:
    Code:
    char * inttobinstr(int n)
    {
    	char *binstr;
    	binstr = (char*)malloc(sizeof(char)*8);
    	binstr = "00000001";
    	binstr[6]='s';  <---------this assignment gives me the error
    	return binstr;
    }
    what could be the problem??

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Actually it appears in this line
    Code:
    binstr = "00000001"; /* goes out of bounds because binstr needs space for 9 chars due to null terminator */

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by itCbitC View Post
    Actually it appears in this line
    Code:
    binstr = "00000001"; /* goes out of bounds because binstr needs space for 9 chars due to null terminator */
    Incorrect.

    binstr will now point to the address of "00000001", and binstr[6] is assigning +6 past this already read-only address.

    You cannot modify string literals, and by assigning the address of "00000001" to binstr you have leaked the memory you allocated using malloc().
    Last edited by zacs7; 12-08-2009 at 01:19 AM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by zacs7 View Post
    Incorrect.

    binstr will now point to the address of "00000001", and binstr[6] is assigning +6 past this already read-only address.
    Dang! you're right, didn't notice no square brackets

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    so how to solve this problem?
    ok... if i declare it as an array of char like char binstr[8]="00000000";
    then i want to return pointer to that string....

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by r00t View Post
    so how to solve this problem?
    ok... if i declare it as an array of char like char binstr[8]="00000000";
    then i want to return pointer to that string....
    Why not just copy the string into the memory you allocated with malloc()? Note that "00000000" takes 9 bytes (including the null terminator) not 8 bytes.

    Or just pass in a pointer as an argument, and let the caller supply the memory. For example:

    Code:
    void inttobinstr(const int n, char * str, size_t size)
    {
       ...
    }

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    but the problem is occuring on the line i marked red....why is that happening? i couldn't get it...

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by r00t View Post
    but the problem is occuring on the line i marked red....why is that happening? i couldn't get it...
    You cannot modify string literals, the standard dictates that doing so is implementation defined.

    "00000001" in this case is a string literal, you are assigning its address to 'binstr' and by setting binstr[6] to 's' you are modifying the string literal. Your implementation defines this as illegal (or doesn't define behaviour at all), hence the segmentation fault.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    ah ok thnx, i'll try to pass it as you wrote..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  5. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21