how to prevent array of ints from edits

Have array of ints, dynamically filled...
Need to use the data from array, but prevent it from writing.
Can't use const.
Kindly advise
Need to use the data from array, but prevent it from writing.
Can't use const.

Find the contradiction in your request ;-)

Really, you should be using const here. Otherwise, the best you could do is passing a copy of the original data. This won't prevent modifications, but only the copy, not the "original", would be modified.
Last edited on
Wrap it in a class, filled during construction.

Then make sure that any getter (or [] operation for a single item) doesn't amend the contents.
Im 100% sure nothing amends the original contents, it's magic number or stackoverflow...
How can I use const for things i get dynamically?
This is very importan for my work, would be very appreciated any help!
1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>

std::vector<int> create_magic_numbers()
{
    return { 12, 456, 7890, /* etc */ } ; // magic numbers
}

int main()
{
    const std::vector<int> magic_numbers = create_magic_numbers() ; // immutable numbers
    // ...
}
Have array of ints, dynamically filled...


What do you mean by this? A std::vector, a std::array, a c-style array, a pointer to memory allocated by new, a std::unique_ptr to memory or...

What can be done will depend upon the type of the 'array'.
how important?

a determined idiot can mess with most containers, but if your coders are not intentionally being determined idiots, the above is fine. Pointer shuffling can get past a lot of protections, and classically lets you modify or print private class members or constants etc that you were locked out of touching. Here, I simply clean the raw pointer address by passing it through an integer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include<iostream>
std::vector<int> create_magic_numbers()
{
    return { 12, 456, 7890 /* etc */ } ; // magic numbers
}

int main()
{
    const std::vector<int> magic_numbers = create_magic_numbers() ; // immutable numbers
    std::cout << magic_numbers[1] <<std::endl;
    unsigned long long addr = (unsigned long long)(&magic_numbers[0]);
    int* ip = (int*)(addr);
    ip[1] = 42;
    std::cout << magic_numbers[1] <<std::endl;
}


If your peers are doing stuff like this without cause* they need to be disciplined and possibly relieved of their duties depending on what they were doing.

** this can be legit in extreme circumstances like a 3rd party library that hides something you require.

it may also be worth noting that buffer overflows can touch other memory and read or modify it. Usually by accident, eg off by one in an array processing loop changes the integer stored after the array...
Last edited on
Keep your work out of main() too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>

std::vector<int> create_magic_numbers()
{
    return { 12, 456, 7890, /* etc */ } ; // magic numbers
}

void my_work( const std::vector<int>& data )
{
  // use data
}

int main()
{
    const auto magic_numbers = create_magic_numbers() ;
    my_work( magic_numbers ) ;
}



IIRC, Herb Sutter wrote about a long list of dirty tricks (to bypass class interface) that nobody should use.
Im 100% sure nothing amends the original contents, it's magic number or stackoverflow...
Are we dealing with an X-Y problem here? Are you really saying "something is changing my array and I want it to stop?" That's a completely different issue and means that you need to debug your code.
Topic archived. No new replies allowed.