Solved

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

Posted on 2012-04-04
11
451 Views
Last Modified: 2012-04-10
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?
0
Comment
Question by:jl66
  • 5
  • 3
  • 2
  • +1
11 Comments
 

Author Comment

by:jl66
ID: 37809446
Arr\[i\] = Arr bracket i end bracket
0
 
LVL 9

Assisted Solution

by:richard_hughes
richard_hughes earned 40 total points
ID: 37809462
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
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 40 total points
ID: 37809593
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 33

Accepted Solution

by:
sarabande earned 420 total points
ID: 37810523
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
 

Author Comment

by:jl66
ID: 37814783
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
 

Author Comment

by:jl66
ID: 37814850
--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
 
LVL 9

Expert Comment

by:richard_hughes
ID: 37814891
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
 

Author Comment

by:jl66
ID: 37814945
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
 
LVL 9

Expert Comment

by:richard_hughes
ID: 37818766
I'm sorry, I though you didn't want to change the array?
0
 

Author Closing Comment

by:jl66
ID: 37822980
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
 
LVL 33

Expert Comment

by:sarabande
ID: 37826714
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question