Losing a variable

M

Marcia Hon

Hi,

I have a function that has a counter variable that I increment. It
increments after a function call within the first function. Somehow,
when I return from the second function to increment the counter, I
cannot access the memory of the variable.

Here is the functions:

function1(){
int variableCounter = 0;

function2();

variableCounter++;

return;

}

Please your help would be very much appreciated.

Thanks,
Marcia
 
J

Joona I Palaste

Marcia Hon said:
I have a function that has a counter variable that I increment. It
increments after a function call within the first function. Somehow,
when I return from the second function to increment the counter, I
cannot access the memory of the variable.
Here is the functions:
function1(){
int variableCounter = 0;




Please your help would be very much appreciated.

It should be perfectly OK to increment variableCounter in function1()
even after the call to function2(). Are you sure you're not trying to
increment variableCounter _in_ function2()? That would go against the
scoping rules.
 
A

Artie Gold

Marcia said:
Hi,

I have a function that has a counter variable that I increment. It
increments after a function call within the first function. Somehow,
when I return from the second function to increment the counter, I
cannot access the memory of the variable.

Here is the functions:

function1(){
int variableCounter = 0;

function2();

variableCounter++;

return;

}

Please your help would be very much appreciated.

I understand you're still new at this, but posting code like this makes
it very difficult to diagnose your problem (which is most likely located
in function2()).

The basic rule is to post minimal, compilable code that exhibits the
problem you're having -- otherwise all we can possibly do is guess.

HTH,
--ag
 
M

Martin Dickopp

I have a function that has a counter variable that I increment. It
increments after a function call within the first function. Somehow,
when I return from the second function to increment the counter, I
cannot access the memory of the variable.

What do you mean by "cannot access the memory"? Does the program crash,
or what happens?
Here is the functions:

function1(){
int variableCounter = 0;

function2();

variableCounter++;

return;

}

`variableCounter' is incremented and you return from `function1'
immediately afterwards. It makes no sense to increment `variableCounter'
(or to have this variable in the first place) if you don't use its value
for anything.

(Besides, since you implicitly declared `function1' to return `int' by
not specifing a return type, you should really return an `int'. Did you
mean to write `return variableCounter;', perhaps?)

Martin
 
P

pete

Marcia said:
Hi,

I have a function that has a counter variable that I increment. It
increments after a function call within the first function. Somehow,
when I return from the second function to increment the counter, I
cannot access the memory of the variable.

Here is the functions:

function1(){
int variableCounter = 0;

function2();

variableCounter++;

return;

}

/* BEGIN new.c */

#include <stdio.h>

void function1(int *counter)
{
putchar('x');
++*counter;
}

int main(void)
{
int x, counter;

for (counter = x = 0; 10 > x; ++x) {
function1(&counter);
}
printf("\n%d\n", counter);
return 0;
}

/* END new.c */
 
P

Pierre Maurette

Marcia Hon said:
Hi,

I have a function that has a counter variable that I increment. It
increments after a function call within the first function. Somehow,
when I return from the second function to increment the counter, I
cannot access the memory of the variable.

Here is the functions:

function1(){
int variableCounter = 0;

function2();

variableCounter++;

return;

}

Please your help would be very much appreciated.

What do you mean by "I cannot access the memory of the variable" ?
I assume you use a debugger, and cannot access to this variable, and even
put a BP on the line :
variableCounter++;
If your code is actually as above, likely your compiler generate nothing
about variableCounter ( = no machine code, no variable)
Try :
- insert a printf("%d\n",variableCounter);
- return variableCounter
- declare :
static int variableCounter = 0;
(like this, variableCounter will count the calls to function1()).

Pierre
 
C

CBFalconer

Artie said:
I understand you're still new at this, but posting code like this makes
it very difficult to diagnose your problem (which is most likely located
in function2()).

The basic rule is to post minimal, compilable code that exhibits the
problem you're having -- otherwise all we can possibly do is guess.

She (presumed) has been told this time and time again for the past
two weeks or so, yet insists on creating more incomprehensible
threads. At least this one is not crossposted to Timbuktoo. I
fear the only practical solution is plonking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top