How to make the values of an array unchange in C/C++

Experienced with changeable values in an array. For example, in one method, assign some value to the array and then print it. The values are correct. However in another method, try to print the array again, the values have been changed. Would like to get the consistgent values.

Here is what has been done.

Consider only one class: test.cpp and test.h

In test.h
static char Arr[1000000];
class TEST
{
   public:
   int method1();
   int method2();
};

in test.cpp
#include "test.h"
...
int TEST::method1()
{
   for (unsigned int i =0; i < 1000000; i++)
     Arr\[i\] = 0x31;
//1st print
   for (unsigned int i =0; i < 100; i++)
     printf("%x ", Arr\[i\]);
   printf("\n");
     ...
   return 0;
}

int TEST::method2()
{
  //2nd print
   for (unsigned int i =0; i < 100; i++)
     printf("%x ", Arr\[i\]);
   printf("\n");
   ...
   return 0;
}

Real case is more complex than this one, but like this. The 2 prints are not always same. Since there are no places to change the values of Arr, one expects the same values will be printed.

The problem is how to declare the array Arr to make something like fixed memory location to hold it. I tried to define it within the class, it did not make any difference. Can any gurus shed some light on it?
jl66Asked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
you should put the static array into the class Test as a public member. that way you could access it by Test::Arr from everywhere and it is only one instance.

never define static (global) variables in  a header cause in case you would include the header from two cpp files the linker would complain because of duplicates. so if you don't want it a static class member you have to move it into a cpp file. if you need the same array in another cpp file use the extern keyword and same name.  

extern char Arr[10000];

Open in new window


the static is only in the definition and you may not initialize variables declared extern (cause initialization is done with definition not declaration).

next is that any static variable should be initialized. otherwise the array values are different each time you start the program.

static char Arr[1000000] = { '\0' }; // all elements zero 

Open in new window


if you make the array a static member of class Test, the compiler would require an initialization in one of the cpp files - not in the header for the same reason to not produce duplicate symbols.

char Test::Arr[1000000] = { '\0' }; // all elements zero 

Open in new window


but in none of the above cases you would have different output like you described. in your case where Arr was not initialized you would/could get different outputs when method2 was called before method1 or in case of multi-threading.  you also could get bad values if some other loop would write beyond boundaries of another array and this other array was located in memory before the static array.

Sara
0
 
jl66Author Commented:
Arr\[i\] = Arr bracket i end bracket
0
 
richard_hughesConnect With a Mentor Commented:
Hello jl66

What you are doing is actually assigning to the array - which will overwrite any existing values.

The two options which may be of use are:

1) Create a local array and copy your global array into that and change the values of the local array.

2) Define the array in a class and pass that class into your method as a copy - not as a pointer or a reference.

To help any more we really need a bit more context as to what you are trying to accomplish.

Thanks,

Richard Hughes
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
phoffricConnect With a Mentor Commented:
I didn't know what that Arr\[i\] syntax was, so I changed it to more familiar syntax. Both printf's in this single .cpp file produce the same output:
#include <stdio.h>

static char Arr[1000000];
class TEST
{
   public:
   int method1();
   int method2();
};

int TEST::method1()
{
   for (unsigned int i =0; i < 1000000; i++)
     Arr[i] = 0x31;
//1st print
   for (unsigned int i =0; i < 100; i++)
     printf("%x ", Arr[i]);
     printf("\n\n");
   return 0;
}

int TEST::method2()
{
  //2nd print
   for (unsigned int i =0; i < 100; i++)
     printf("%x ", Arr[i]);
   printf("\n\n");
   return 0;
}

int main() {
   TEST test;
   test.method1();
   test.method2();
}

Open in new window

0
 
jl66Author Commented:
Thank everyone for the info.
The situation is that at the beginning, I used static array within class as mentioned before, assigned the values as Sara did. However it did not work. I tried it again. No luck. The linux box currently used has >1GB physical memory. In this program, a few arrays with 1 million-characters are going to be stored in memory to process quickly. Currently this program is only one to run. Is this shortage of physical memory or any other methods can be used???
0
 
jl66Author Commented:
--Richard,
>>1) Create a local array and copy your global array into that and change the values of the local array.

The purpose of this global array is that it can share among many methods/functions. Some methods only use it while other ones update/use it. Once updated, the revised values in the array can be used among the methods. 1) did not seem to fit the case.
 
>>2) Define the array in a class and pass that class into your method as a copy - not as a pointer or a reference.

Can Richard give me a simply example to serve the purpose mentioned in the above?
0
 
richard_hughesCommented:
Hello

Sure, something like this would be OK:

class MyArrayClass
{
    public: char m_MyArray[100000];
};

// respective methods somewhere...
void MyMethod1(MyArrayClass mac)
{
     // process array here
     // as the value 'mac' is copied, any changes to the array will not affect the original class that was passed in
}

Open in new window

0
 
jl66Author Commented:
Richard, thanks for quick response. However, I want to make the array change globally, which is shared immediately by the other methods. In MyMethod1 you listed, it does not seem to serve this purpose or I miss something.
0
 
richard_hughesCommented:
I'm sorry, I though you didn't want to change the array?
0
 
jl66Author Commented:
Thanks a lot for everyone's info. Actually it was a bug in Fedora v11. After the kernel was recompiled, the values in global array were consistent.
0
 
sarabandeCommented:
many compilers have limitations of array sizes when the array is on the stack (as it is when you define a local or global array). if a compiler has stack limitations it also has an option to increase the maximum stack used. however, even with nowadays memory sizes you better would use dynamic arrays (on the heap by using 'new' operator) or far better use standard containers. the latters not only would use heap memory but with view exceptions wouldn't depend on contiguous memory, such that they could make much better and more efficient usage of memory.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.