Matt Zimmerman
2004-Sep-10 16:45 UTC
[Flac-dev] const issue in FLAC__lpc_compute_residual_from_qlp_coefficients (libFLAC/lpc.c:233)
On Tue, Jan 13, 2004 at 02:04:48PM -0800, Josh Coalson wrote:> --- Denis Chatelain <listes@octopodus.com> wrote: > > Hello, > > > > > > I just tried to compile libFLAC (using Borland C++ Builder 6 on > > Windows). > > > > The compilers yells at me on line 233 of libFLAC/lpc.c > > > > *(residual++) = *(data++) - (sum >> lp_quantization); > > --> data is const and cannot be modified > > > > Funny thing is, if data is declared: > > const FLAC__int32 *data > > instead of > > const FLAC__int32 data[] > > > > everything is ok. > > > > Is this a bug in my compiler, or a personnal lack of understanding of > > the difference in C between *data and data[] ? > > hmmm, not sure what the standard says on this. but it's > OK to change it to > > const FLAC__int32 *data > > in order to get it to compile.I haven't checked, and I don't recall whether it covers the case of const arrays, but this article has helped me in the past: http://www.parashift.com/c++-faq-lite/const-correctness.html -- - mdz
Denis Chatelain
2004-Sep-10 16:45 UTC
[Flac-dev] const issue in FLAC__lpc_compute_residual_from_qlp_coefficients (libFLAC/lpc.c:233)
Hello, I think you solved the mystery. You're right, the basic problem is not a const problem, but a * [] problem As a test.c: int main (void) { int data[3]; *(data++); return 1; } gcc test.c gives: -- wrong type argument to increment But: int main (void) { int *data; *(data++); return 1; } Compiles just fine with gcc. I didn't take a look at the latest CVS snapshot, but this might be a good thing to fix it so that the gcc build doesn't break in the future and others spend time asking themselves what's so funny about this */[] things :) I'll post a patch later if anyone is interested, this kind of problem arises in a few lines. Denis Christian Hudon wrote:> >>>> The compilers yells at me on line 233 of libFLAC/lpc.c >>>> >>>> *(residual++) = *(data++) - (sum >> lp_quantization); >>>> --> data is const and cannot be modified >>>> >>>> Funny thing is, if data is declared: >>>> const FLAC__int32 *data >>>> instead of >>>> const FLAC__int32 data[] >>>> >>>> everything is ok. >>>> >>>> Is this a bug in my compiler, or a personnal lack of understanding of >>>> the difference in C between *data and data[] ? >>>> >>> > > I'm not a great language lawyer, but I don't think you can modify the > pointer when something is declared int foo[]. You can modify the data > pointed to, with foo[0] = 42, etc. but you can't do stuff like foo++ > or foo = (int*)something. The same error would show up with > FLAC__int32 * const data. If you want to change the pointer, you need > to declare it as a pointer with int *foo (or const int *foo, which > means you can modify the pointer, but not the data it points to). > > My 2 cents. I'll go back to lurking now. :-) > > Christian > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Perforce Software. > Perforce is the Fast Software Configuration Management System offering > advanced branching capabilities and atomic changes on 50+ platforms. > Free Eval! http://www.perforce.com/perforce/loadprog.html > _______________________________________________ > Flac-dev mailing list > Flac-dev@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/flac-dev
Christian Hudon
2004-Sep-10 16:45 UTC
[Flac-dev] const issue in FLAC__lpc_compute_residual_from_qlp_coefficients (libFLAC/lpc.c:233)
>>>The compilers yells at me on line 233 of libFLAC/lpc.c >>> >>>*(residual++) = *(data++) - (sum >> lp_quantization); >>>--> data is const and cannot be modified >>> >>>Funny thing is, if data is declared: >>> const FLAC__int32 *data >>>instead of >>> const FLAC__int32 data[] >>> >>>everything is ok. >>> >>>Is this a bug in my compiler, or a personnal lack of understanding of >>>the difference in C between *data and data[] ? >>> >>>I'm not a great language lawyer, but I don't think you can modify the pointer when something is declared int foo[]. You can modify the data pointed to, with foo[0] = 42, etc. but you can't do stuff like foo++ or foo = (int*)something. The same error would show up with FLAC__int32 * const data. If you want to change the pointer, you need to declare it as a pointer with int *foo (or const int *foo, which means you can modify the pointer, but not the data it points to). My 2 cents. I'll go back to lurking now. :-) Christian