Computing.Net > Forums > Programming > static arrays C/C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

static arrays C/C++

Reply to Message Icon

Name: Gauthier
Date: October 29, 2002 at 00:10:17 Pacific
OS: win98
CPU/Ram: 500MHz, 128MB
Comment:

Hi!

Now that I'm (at last) starting to deal better with vc++, I go back to more fundamental problems.

I have a function (member of a class, but whatever) dealing with an array. I would like, in this function, to have a memory that keeps track of the array as it was in the last call of the function.

Typically, if my variable had been an int, i would have declared a static int in the function, and saved the old value in that variable.
But when it comes to an array, it does not work anymore! i tried:

static int arr[CONST];

But that does not seem to work (I guess that the static refers to a pointer to int, or some strange stuff related to how is declared an array in c++).

Someone who knows how I can tell my computer to consider the CONTENT of the array as static?

Thanks a lot for any help!



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: October 29, 2002 at 09:04:50 Pacific
Reply:

The definition that you show is correct and should work. It doesn't matter if it's an array or a scalar variable. Possibly VC++ doesn't implement this correctly, but I doubt that is the problem.

Try the following program to see if it works for you in VC++ (I ran it on a linux box and you can see the ouptut below).

#include stdio.h

#define CONST 5

void myfunc(int);

void main(void) {
myfunc(2);
myfunc(5);
}


void myfunc(int increment) {
static int arr[CONST];
int i;

printf("BEFORE: ");
for (i=0; i printf("%d ",arr[i]);
}
printf("\n");


for (i=0; i arr[i] += increment;
}

printf("\n\nAFTER: ");
for (i=0; i printf("%d ",arr[i]);
}
printf("\n\n\n");

}

OUTPUT:
BEFORE: 0 0 0 0 0


AFTER: 2 2 2 2 2


BEFORE: 2 2 2 2 2


AFTER: 7 7 7 7 7


0

Response Number 2
Name: Gauthier
Date: October 30, 2002 at 01:29:55 Pacific
Reply:

As you may have expected, you're perfectly right.

And I also found why it was not working (I fell quite ridicullous about it, by the way)...

I had several calls (say : call A, call B, call C, call D) to that function in a loop. I was stupidly expecting the function to keep a memory of an array (in a static int[]), and that the memory saved in the call A would be kept until the next call A.

Which is obviously not really clever, for the other calls overwrote it.

Now I did it in a cleaner way.

And I achieved what I wanted to.

Thanks a lot!


0

Response Number 3
Name: mm_freak
Date: October 30, 2002 at 09:26:19 Pacific
Reply:

Also remember, that your Class may have more than one instance, but all of them write to the same static memory. Use a class member for cleaner code. You can also make class members static, even function members.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: static arrays C/C++

Change value in char array in C www.computing.net/answers/programming/change-value-in-char-array-in-c/10213.html

How to use string array in C++? www.computing.net/answers/programming/how-to-use-string-array-in-c/9478.html

C++ Dynamic Array www.computing.net/answers/programming/c-dynamic-array/11481.html