News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Pointer arithmetic bug

Started by kolo32, September 08, 2011, 10:55:22 AM

Previous topic - Next topic

kolo32

#include <stdio.h>

int main(void)
{
char * p = "bugfix";
p += 4;
p--;
puts(p);
return 0;
}


This program under Pelles C 6.50.8 RC #4 prints an empty line :( Expected result: "fix" must be printed.

After changing to:

static const char * p = "bugfix";


it works as expected.

AlexN

Ro much optimized by the compiler. If it stores the string as locale string, it seems to optimize the storage for the string. So there is only "ix" on the stack and p points to the not initialized character before the 'i', which is in this case a 0x00, and the output for this is an empty line.
best regards
Alex ;)

MichaelT

#2
Edit:

I decided to retract my statement earlier.

This because I investigated the issue further and you're quite right.
It seems the optimization is premature.