This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

is it a bug or am I dumb


I am not sure if this is a bug or not, but, it is driving me crazy!

I am running Red Hat Linux 7.3 on a Celeron 466 with 256 MB of ram. I am
using the 3.2 release of gcc and glibc version 2.2.5

Here is the code that produces my
problem---------------------------------------------------------------------
--------------------------------
#include <stdio.h>
#include <string.h>

int main()
{

        char            *p;
        char            test_string[14];

        sprintf( test_string, "testing" );

        p = (char *) malloc( strlen( test_string ) + 1 );      //This works
fine and the program runs correctly
        if ( p != NULL )
                strcpy( p, test_string );

        printf( "%s\n", p );
        fflush(stdout);

        free( p );

        sprintf( test_string, "testing again" );

        if ( p = (char *) malloc( strlen( test_string ) + 1 ) != NULL )
//This produces a message during compile
                strcpy( p, test_string );

        printf( "%s\n", p );
        fflush(stdout);

        return(0);

}
----------------------------------------------------------------------------
-----------------------------
here is the invocation of gcc and the results:

buzzhead.net-:/root> gcc -o ctest ctest.c
ctest.c: In function `main':
ctest.c:23: warning: assignment makes pointer from integer without a cast

here is the result when the program is run:

buzzhead.net-:/root> ctest
testing
Memory fault

----------------------------------------------------------------------------
-----------------------------

>From debugging code I have placed in the program, it appears that the
allocation operation is working fine, it is getting the memory fault from
the second call to strcpy, which doesn't change between the two calls.

As I said, I am not sure if this is a bug or not, it definitely is not what
I have come to expect from the compilers I use on SCO Unix. I copied that
syntax from the book "The C Programming Language" by K & R.

Thank you for any help you can give me with this
John Mackey



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]