Is there a way to (actually) protect an object from being modified?

user5619103

The const type qualifier causes the compiler to issue an error message in case an attempt to modify an object declared as const,but that is not enough protection.For example the following program modifies both elements of the array declared as const:

#include <stdio.h>

int main(void)
{
    const char buf[2] = { 'a','b' };
    const char *const ptr = buf;
    unsigned long addr = (unsigned long)ptr;

    *(char *)addr = 'c';
    addr = addr + 1;
    *(char *)addr = 'd';

    printf("%c\n", buf[0]);
    printf("%c\n", buf[1]);
    return 0;
}

So,it turns out to be that the compiler is not enough guard to protect the objects from being modified.How can we prevent this sort of thing?

nalzok

I don't think more protection can nor should be provided.

The C programming language lets you do almost everything you want freely, especially accessing objects from pointers. However, freedom is never free, so C programmers must always be careful (and avoid casting if it isn't necessary).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

protect images from being copied

From Dev

Protect PDF docs from being directly accessed in URL

From Dev

In c++, if a member pointer point to some data, how to protect that data from being modified?

From Dev

How to prevent files from being modified in elFinder?

From Dev

How to protect my Javascript from being accessed by other parties?

From Dev

How can I protect sqlite db in android from being stolen

From Dev

How can I protect a matrix in R from being altered by Rcpp?

From Dev

Handling checkbox values being modified from 'true'

From Dev

After copying the original object is still being modified

From Dev

How to prevent class instance from being modified?

From Dev

Is there a way to protect a class variable from being modified outside of a function

From Dev

Is there a way to restrict a pointer from being modified by specific functions?

From Dev

How to protect columns in hibernate from being read

From Dev

GitLab: Is there a way to protect a branch from command line?

From Dev

Is there a way to protect the master node from autoscalling?

From Dev

How to protect my Javascript from being accessed by other parties?

From Dev

Will sandboxing a program protect my computer from being damaged?

From Dev

How can i protect a property from being overwritten

From Dev

Will this approach protect my database from being modified?

From Dev

Is there a way to protect a file from being deleted, but not from being altered?

From Dev

How to prevent class instance from being modified?

From Dev

Is there a way to protect a class variable from being modified outside of a function

From Dev

How to Protect Windows OS Image From Being Copied?

From Dev

Is there any way we can prevent startup items in Windows 10 from being modified and/or deleted by using bootable media?

From Dev

How to protect bash function from being overridden?

From Dev

Best way to protect javascript code from working with modified parameters array

From Dev

Protect the data at the bottom of the page from being hidden under the fixed div?

From Dev

Protect Images from being public laravel 5

From Dev

Is there a way to protect all existing rows from being edited in an SQL table?

Related Related

  1. 1

    protect images from being copied

  2. 2

    Protect PDF docs from being directly accessed in URL

  3. 3

    In c++, if a member pointer point to some data, how to protect that data from being modified?

  4. 4

    How to prevent files from being modified in elFinder?

  5. 5

    How to protect my Javascript from being accessed by other parties?

  6. 6

    How can I protect sqlite db in android from being stolen

  7. 7

    How can I protect a matrix in R from being altered by Rcpp?

  8. 8

    Handling checkbox values being modified from 'true'

  9. 9

    After copying the original object is still being modified

  10. 10

    How to prevent class instance from being modified?

  11. 11

    Is there a way to protect a class variable from being modified outside of a function

  12. 12

    Is there a way to restrict a pointer from being modified by specific functions?

  13. 13

    How to protect columns in hibernate from being read

  14. 14

    GitLab: Is there a way to protect a branch from command line?

  15. 15

    Is there a way to protect the master node from autoscalling?

  16. 16

    How to protect my Javascript from being accessed by other parties?

  17. 17

    Will sandboxing a program protect my computer from being damaged?

  18. 18

    How can i protect a property from being overwritten

  19. 19

    Will this approach protect my database from being modified?

  20. 20

    Is there a way to protect a file from being deleted, but not from being altered?

  21. 21

    How to prevent class instance from being modified?

  22. 22

    Is there a way to protect a class variable from being modified outside of a function

  23. 23

    How to Protect Windows OS Image From Being Copied?

  24. 24

    Is there any way we can prevent startup items in Windows 10 from being modified and/or deleted by using bootable media?

  25. 25

    How to protect bash function from being overridden?

  26. 26

    Best way to protect javascript code from working with modified parameters array

  27. 27

    Protect the data at the bottom of the page from being hidden under the fixed div?

  28. 28

    Protect Images from being public laravel 5

  29. 29

    Is there a way to protect all existing rows from being edited in an SQL table?

HotTag

Archive