{"action":"created","issue":{"url":"https://api.github.com/repos/awslabs/s2n/issues/155","labels_url":"https://api.github.com/repos/awslabs/s2n/issues/155/labels{/name}","comments_url":"https://api.github.com/repos/awslabs/s2n/issues/155/comments","events_url":"https://api.github.com/repos/awslabs/s2n/issues/155/events","html_url":"https://github.com/awslabs/s2n/pull/155","id":96414796,"number":155,"title":"Adding 'const' to various write routines within stuffer","user":{"login":"Wiladams","id":457634,"avatar_url":"https://avatars.githubusercontent.com/u/457634?v=3","gravatar_id":"","url":"https://api.github.com/users/Wiladams","html_url":"https://github.com/Wiladams","followers_url":"https://api.github.com/users/Wiladams/followers","following_url":"https://api.github.com/users/Wiladams/following{/other_user}","gists_url":"https://api.github.com/users/Wiladams/gists{/gist_id}","starred_url":"https://api.github.com/users/Wiladams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Wiladams/subscriptions","organizations_url":"https://api.github.com/users/Wiladams/orgs","repos_url":"https://api.github.com/users/Wiladams/repos","events_url":"https://api.github.com/users/Wiladams/events{/privacy}","received_events_url":"https://api.github.com/users/Wiladams/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-07-21T20:58:36Z","updated_at":"2015-07-22T07:08:19Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/awslabs/s2n/pulls/155","html_url":"https://github.com/awslabs/s2n/pull/155","diff_url":"https://github.com/awslabs/s2n/pull/155.diff","patch_url":"https://github.com/awslabs/s2n/pull/155.patch"},"body":"Adding 'const' to the API makes the data contract more explicit, and gives the compiler some more information with which to optimize if it can.\r\n\r\nThis kind of change should be applied to all places across the entirety of the API, but starting with the stuffer is useful due to its central role, and relatively small size."},"comment":{"url":"https://api.github.com/repos/awslabs/s2n/issues/comments/123585033","html_url":"https://github.com/awslabs/s2n/pull/155#issuecomment-123585033","issue_url":"https://api.github.com/repos/awslabs/s2n/issues/155","id":123585033,"user":{"login":"Wiladams","id":457634,"avatar_url":"https://avatars.githubusercontent.com/u/457634?v=3","gravatar_id":"","url":"https://api.github.com/users/Wiladams","html_url":"https://github.com/Wiladams","followers_url":"https://api.github.com/users/Wiladams/followers","following_url":"https://api.github.com/users/Wiladams/following{/other_user}","gists_url":"https://api.github.com/users/Wiladams/gists{/gist_id}","starred_url":"https://api.github.com/users/Wiladams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Wiladams/subscriptions","organizations_url":"https://api.github.com/users/Wiladams/orgs","repos_url":"https://api.github.com/users/Wiladams/repos","events_url":"https://api.github.com/users/Wiladams/events{/privacy}","received_events_url":"https://api.github.com/users/Wiladams/received_events","type":"User","site_admin":false},"created_at":"2015-07-22T07:08:19Z","updated_at":"2015-07-22T07:08:19Z","body":"const is just part of the contract in this case.  The function implementer is telling the consumer \"I'm not going to do anything to this value\".  Granted, it probably is more appropriate for cases where you're passing pointers, rather than intrinsic integer types.\r\n\r\nIn terms of data contract, it's explicit what will happen here.  You can pass in literals, and other values, because they won't be changed from within the function.  In terms of what the compiler is going to do to achieve this, it just depends on the compiler.\r\n\r\nSo, it's safe to make these things const, because you're just being explicit about the contract.  In your example:  s2n_stuffer_alloc(stuffer, 5);\r\n\r\nWould be equivalent.\r\n\r\nHere's a case where it actually matters:\r\n\r\nvoid foo(int n)\r\n{\r\n  n = n + 1;\r\n  dosomethingwithn(n);\r\n}\r\n\r\nIn this case, the compiler does something, because it has to be able to change the value of 'n'.\r\n\r\nNow if we do this:\r\n\r\nvoid foo(const int n)\r\n{\r\n  n = n + 1;\r\n  dosomethingwithn(n);\r\n}\r\n\r\nWe should get a compiler error, because since 'n' was declared 'const', your code should not be able to change it.  To remedy this situation, you'd have to do:\r\n\r\nvoid foo(const int cn)\r\n{\r\n  int n = cn + 1;\r\n  dosomethingwithn(n);\r\n}\r\n\r\nThis forces the code to be explicit about making a temporary variable for the purposes of manipulation.  There is actually an instance of this in the PR I submitted.\r\n\r\nBottom line, everything should be const until proven otherwise, and that proof should come in the form of an explicit articulation of the data contract.\r\n"}}