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

Mike Lui

This builds off of my previous question: Is this an appropriate use of const qualifiers in C?

In Vector.h:

typedef struct _Vector Vector;
Vector* vector_init(const UInt32 size);
void*  vector_get(const Vector *v, UInt32 idx);
void   vector_add(Vector *v, void* const elem);
void   vector_set(Vector *v, const UInt32 idx, void* const elem);
...etc

In Vector.c:

struct _Vector{
    UInt32 used;
    UInt32 size;
    void** arr;
};

Vector* vector_init(const UInt32 size){
    Vector* v = malloc(sizeof(Vector));
    v->used = 0;
    v->size = size;
    v->arr = malloc(size*sizeof(void*));
    return v;
}

void* vector_get(const Vector *v, const UInt32 idx){
    if ( idx >= v->used )
        exitAtError("Vector","Array out of bounds");
    return v->arr[idx];
}

void vector_add(Vector *v, void* const elem){
    if( v->used == v->size )
        vector_resize(v);
    v->arr[v->used++] = elem;
}
...etc

I want to prevent void** arr in _Vector from being accidentally modified by my implementation, as a warning/error at compile time. I can't make arr a const void** because I don't want the vector to be permanent, and want to avoid casting away constness.

The idea here is that used and size are data that are directly related to the scope of my functions, whereas arr is just a kind of metadata that I don't want to modify. arr is opaque, so I can't directly modify it, but I can cast it or directly overwrite in bad ways such as memcpy.

In this example it seems unnecessary to enforce this access since each function is straight-forward, but this example mostly for demonstration purposes.

I have two related questions that can be answered at the same time:

  1. Is this access restriction possible with the language support in C, and if not, is the opaqueness of the pointer considered a strong enough discouragement of directly modifying the data?
  2. Am I just chasing my tail here and instead should focus on generating code that is well documented and well structured, where this wouldn't even become an issue?

I see a related question was asked in: Is there a way to protect a class variable from being modified outside of a function

That user seems to be in a similar predicament in C#; my question #2 is relevant in that scenario.

EDIT: I think from everyone's input, it's becoming clear that I'm pushing the language outside of its original design by looking for some type of magical keyword to document things for me. Either hiding my data behind another abstraction, or structurally separating out my functionality to prevent the temptation to poke at the data, seems to be the best solutions.

Steephen

Since you were looking for a solution either in C/C++, I created a layer of abstraction in C++ to protect the void** const arr as follows. Please check does it meet your requirement.

class  array{
       public:
        void setArray(void** key)
        {
            arr=key;
        }

        void** getArray()
        {
            return &(*arr);
        }
    private:
     void** arr;
};
struct Vector: public array{
    int  used;
    int  size;

};

Vector* vector_init(const int  size){
    Vector* v =(Vector*) malloc(sizeof(Vector));
    v->used = 0;
    v->size = size;
    void** tem=v->getArray();
    tem=(void**) malloc(size*sizeof(void*));
    v->setArray(tem);
     return v;
}

Demo: http://coliru.stacked-crooked.com/a/005b335e5fb372ce

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a way to (actually) protect an object 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 protect a class variable from being modified outside of a function

From Dev

Double Pointer being modified in an array mysteriously

From Dev

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

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

Module Pattern: Restrict a module from being extended

From Dev

Restrict property from being rdf:type

From Dev

How to prevent class instance from being modified?

From Dev

How to prevent files from being modified in elFinder?

From Dev

Handling checkbox values being modified from 'true'

From Dev

Will this approach protect my database from being modified?

From Dev

How to prevent class instance from being modified?

From Dev

Modified struct in some functions changes pointer and causes free() to fail

From Dev

Calling Functions from a Pointer Vector

From Dev

Calling Functions from a Pointer Vector

From Dev

Is there a way to restrict the packages installable from a repository?

From Dev

Is there a way to restrict the packages installable from a repository?

From Dev

How to restrict access to specific work item functions in VSTS

From Dev

Restrict certain postcodes from being entered into form input

From Dev

How to restrict the same character from being used consecutively?

From Dev

In c# how to prevent a class from being modified

From Dev

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

From Dev

Powershell Variable data from Functions being lost

From Dev

Restrict MATLAB to call functions from same folder as running file

From Dev

An efficient way to call type specific functions

From Dev

Is there a way to keep a backup of only the modified files in a directory (when being compared to another directory)?

From Dev

Prevent an observable from being set in specific cases

From Dev

Exclude a specific table from being created by hibernate?

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    Double Pointer being modified in an array mysteriously

  5. 5

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

  6. 6

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

  7. 7

    Module Pattern: Restrict a module from being extended

  8. 8

    Restrict property from being rdf:type

  9. 9

    How to prevent class instance from being modified?

  10. 10

    How to prevent files from being modified in elFinder?

  11. 11

    Handling checkbox values being modified from 'true'

  12. 12

    Will this approach protect my database from being modified?

  13. 13

    How to prevent class instance from being modified?

  14. 14

    Modified struct in some functions changes pointer and causes free() to fail

  15. 15

    Calling Functions from a Pointer Vector

  16. 16

    Calling Functions from a Pointer Vector

  17. 17

    Is there a way to restrict the packages installable from a repository?

  18. 18

    Is there a way to restrict the packages installable from a repository?

  19. 19

    How to restrict access to specific work item functions in VSTS

  20. 20

    Restrict certain postcodes from being entered into form input

  21. 21

    How to restrict the same character from being used consecutively?

  22. 22

    In c# how to prevent a class from being modified

  23. 23

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

  24. 24

    Powershell Variable data from Functions being lost

  25. 25

    Restrict MATLAB to call functions from same folder as running file

  26. 26

    An efficient way to call type specific functions

  27. 27

    Is there a way to keep a backup of only the modified files in a directory (when being compared to another directory)?

  28. 28

    Prevent an observable from being set in specific cases

  29. 29

    Exclude a specific table from being created by hibernate?

HotTag

Archive