Author Topic: Segmentation fault when using pointers.  (Read 2923 times)

compiler-dev

  • Guest
Segmentation fault when using pointers.
« 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.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Segmentation fault when using pointers.
« Reply #1 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: [Select]
void
test( string )
char *string; /* the list of tag value items */
{

Best wishes,
Michael

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Segmentation fault when using pointers.
« Reply #2 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.
« Last Edit: April 28, 2006, 01:12:03 pm by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Bug

  • Guest
Re: Segmentation fault when using pointers.
« Reply #3 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.

Bug

  • Guest
Re: Segmentation fault when using pointers.
« Reply #4 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: [Select]
void
test( string )
char *string; /* the list of tag value items */
{

Best wishes,
Michael


Sorry.
Thanks a lot.