Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: compiler-dev on April 28, 2006, 12:09:46 pm

Title: Segmentation fault when using pointers.
Post by: compiler-dev on April 28, 2006, 12:09:46 pm
Hi!

/*  CodeBlocks v1.0
    GNU GCC compiler
 */

void
test( string )
char *string; /* the list of tag value items */
{
    char *s1 = "123";
   char *s2;

   s2 = string;

    while( *s1 != '\0' )
    {
        *s2 = *s1;  /* !!!  Program received signal (SIGSEGV)
                            Segmentation fault  */
        s1++;
        s2++;
    }

}

int main()
{
    char* string = "321";

    test(string);
}

Segmentation fault when using pointers.
Is it bug of CodeBlocks or GNU GCC compiler?

Thanks.
Title: Re: Segmentation fault when using pointers.
Post by: Michael on April 28, 2006, 12:35:32 pm
Hello,

Which C::B revision do you use? Which OS?

Sorry, but your program do not even compile. Especially here:

Code
void
test( string )
char *string; /* the list of tag value items */
{

Best wishes,
Michael
Title: Re: Segmentation fault when using pointers.
Post by: thomas on April 28, 2006, 01:05:24 pm
....and it is in no way related to Code::Blocks.


EDIT:
To elaborate, you are writing to a read-only memory page, which of course causes a segmentation fault, this is normal.
Such issues, however, belong to a "C programming" or "General programming" forum, not to the Code::Blocks forums.
Title: Re: Segmentation fault when using pointers.
Post by: Bug on April 30, 2006, 06:10:38 pm
....and it is in no way related to Code::Blocks.


EDIT:
To elaborate, you are writing to a read-only memory page, which of course causes a segmentation fault, this is normal.
Such issues, however, belong to a "C programming" or "General programming" forum, not to the Code::Blocks forums.

Sorry.
Thanks a lot.
Title: Re: Segmentation fault when using pointers.
Post by: Bug on April 30, 2006, 06:12:10 pm
Hello,

Which C::B revision do you use? Which OS?

Sorry, but your program do not even compile. Especially here:

Code
void
test( string )
char *string; /* the list of tag value items */
{

Best wishes,
Michael


Sorry.
Thanks a lot.