Link to home
Start Free TrialLog in
Avatar of jl66
jl66Flag for United States of America

asked on

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?
Avatar of jl66
jl66
Flag of United States of America image

ASKER

Arr\[i\] = Arr bracket i end bracket
SOLUTION
Avatar of richard_hughes
richard_hughes
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jl66

ASKER

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???
Avatar of jl66

ASKER

--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?
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

Avatar of jl66

ASKER

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.
I'm sorry, I though you didn't want to change the array?
Avatar of jl66

ASKER

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.
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