473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Losing a variable

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
Nov 14 '05 #1
6 1288
Marcia Hon <Ma********@hotmail.com> scribbled the following:
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.


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am lying."
- Anon
Nov 14 '05 #2
Marcia Hon wrote:
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

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Nov 14 '05 #3
Ma********@hotmail.com (Marcia Hon) writes:
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
Nov 14 '05 #4
Marcia Hon wrote:

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 */

--
pete
Nov 14 '05 #5

"Marcia Hon" <Ma********@hotmail.com> a écrit ...
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

Nov 14 '05 #6
Artie Gold wrote:
Marcia Hon wrote:

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;
}


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Lyon | last post by:
I'm maintaining (read: I didn't write it, nor do I have the time to spend to rewrite it) an application that is suddenly giving me grief. The reason I say suddenly, is because we're in the...
2
by: James | last post by:
Session("UserName") is set when someone logs in. Get to a page that displays a record where you can type a comment. The form has an onSubmit event that calls a javascript function. That function...
6
by: Colleyville Alan | last post by:
I seem to recall reading somewhere that there was a keyword that allowed a variable to have data added to it without losing its contents. a = "Hi " and b = "There" myvar = a myvar = b the...
4
by: Stephen | last post by:
I have a .NET (1.1 framework) application that is losing a session variable on only a few PC's. The main page is loading up in a frame in a Portal application. On the Page_Load it stores an...
4
by: Keith-Earl | last post by:
I have been writing ASP.NET apps since the RTM build and have never seen this. I built a simple app that uses session variables on my DEV laptop. All runs well. I have a simple toggle routine...
9
by: Adrian Parker | last post by:
We have a website that works everywhere but on a few PCs on this one site.. Asp.Net 1.1 Server = Windows 2003 Client = XP In the web.config we use - cookieless="false" in the browser settings...
0
by: Frank Bishop | last post by:
Can someone tell me why I'm losing my session variable when using Response.Redirect? When I use the RedirectFromLoginPage method(currently remmed out below), my session variable works fine, but I'm...
0
by: Jimmy Reds | last post by:
Hi, Sorry if this appears twice but I post through Google Groups and it had a funny 5 minutes and didn't appear to post this message the first time. I am setting session variables on a page...
2
by: Jimmy Reds | last post by:
Hi, I am setting session variables on a page then doing a header/location redirect to a second page however I am losing one of my session variables. Not all of them, just one. Here are some...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.