Giter VIP home page Giter VIP logo

Comments (6)

laurencelundblade avatar laurencelundblade commented on May 27, 2024

The const-ness of UsefulBufC is a core part of the intentional design. We have const char * so there should be const byte strings. I wouldn't want to give that up without a good reason.

I would think having more things const helps the compiler optimize.

From your other PR, I suspect this relates to -Wcast-qual. Is that right? Is there any particular reason this warning is important other than basic general code hygiene? I want to be sure I understand what's going on here.

from qcbor.

mcr avatar mcr commented on May 27, 2024

Yes, const helps the compiler optimize, as long as the code follows the contract, and in the case of using a UsefulBufC as a result parameter, that definitely breaks things.

For instance in the case of:

const int A = 10;
function(&A);
B=A+1;

The compile is free to optimize "B=A+1" to be "B=11", because A is a constant, and can not change.
If function uses a const busting pragma/cast to change A, which means that the optimized code is incorrect.
This is not about warnings. This is about correctness.
I have been down this rabbit hole 20 years ago with C, and with multiple PhD compiler designers. We tried to make certain code make sense, we ought to be able to mark certain things as const, but often you just can't.

from qcbor.

laurencelundblade avatar laurencelundblade commented on May 27, 2024

Sorry, if I'm being dense, but I don't know what is broken yet. How is this broken

QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR);

but this

QCBOREncode_Finish(QCBOREncodeContext *pCtx, const char ** pEncodedCBOR, size_t * pnEncodedCBORsize);

is not broken? Or are they both broken?

I'm not sure what the breakage / incorrectness is.

Is it just that a compiler can't optimize this? I'd be a bit surprised at that. LLVM and GCC are very good these days, better than old compilers. I've looked at their output a little for usage of UsefulBuf and UsefulBufC as a parameter and return value and it seems OK.

from qcbor.

mcr avatar mcr commented on May 27, 2024

from qcbor.

laurencelundblade avatar laurencelundblade commented on May 27, 2024

I can't imagine there's danger of compilers generating incorrect code, code that doesn't execute correctly, because of the const. Thousands of C programmers would have noticed.

So maybe it is not as const as expected. It is still more const that it would be without the const declaration so it is still doing good for the coder.

Can you give a concrete example of some code where something breaks? Then tell me why removing the const would result in less breakage.

from qcbor.

mcr avatar mcr commented on May 27, 2024

Hi, I guess I will fork QCBOR then.
I don't have time to read the C-specification to you.

from qcbor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.