News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Segmentation fault when using pointers.

Started by compiler-dev, April 28, 2006, 12:09:46 PM

Previous topic - Next topic

compiler-dev

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.

Michael

Hello,

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

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


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


Best wishes,
Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

thomas

#2
....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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Bug

Quote from: 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.

Sorry.
Thanks a lot.

Bug

Quote from: 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:


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


Best wishes,
Michael


Sorry.
Thanks a lot.