473,378 Members | 1,360 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,378 software developers and data experts.

Subroutine uses wrong value

drhowarddrfine
7,435 Expert 4TB
I have a subroutine that receives a filename from another. This sub has worked with three other calling programs but, today, I added another and for some reason it finds some other file name to use. iow, I call the routine xml_load_file(DEALS) where DEALS is elsewhere defined as "../options/deals.xml". The argument is defined as a char*. If I print out the filename at the beginning of xml_load_file(), it shows an entirely different path and file: ../carts/12345 which is a legitimate filename but I don't have a clue how it got that.

What really throws me for a loop is I create "test_load(char *p)" which does nothing but print out what *p is, put it in the same file as xml_load_file() and that shows the correct file name.

Does this ring a bell with anyone? Using gcc on Linux.
Jul 12 '10 #1
14 1517
Banfa
9,065 Expert Mod 8TB
Sounds very strange to me Doc.

I would step through the program using gdb and see what is actually happening if it is not obvious from code examination.
Jul 12 '10 #2
donbock
2,426 Expert 2GB
Reading between the lines, I assume that the relevant portion of your program looks something like this:
Expand|Select|Wrap|Line Numbers
  1. #define DEALS "../options/deals.xml"
  2.  
  3. void test_load(char *p) {
  4.    printf("%s\n", p);
  5.    }
  6.  
  7. void xml_load_file(char *p) {
  8.    test_load(p);
  9.    printf("%s\n", p);
  10.    }
  11.  
  12. int main(void) {
  13.    xml_load_file(DEALS);
  14.    return 0;
  15.    }
And that your output looks something like this:
Expand|Select|Wrap|Line Numbers
  1. ../options/deals.xml
  2. ../carts/12345
Is this correct? Please be precise about any discrepancies.

I look forward to your response. In the absence of evidence, the only mechanism that comes to mind is if DEALS is a pointer variable rather than a macro, and if it points to freed memory. In that case it wouldn't be terribly surprising if the contents of the freed memory (ie, the string) changed spontaneously.
Jul 13 '10 #3
drhowarddrfine
7,435 Expert 4TB
Pretty much what you show. Calling it from main works. test_load(), though I show it differently, does not. Calling from this one particular subroutine does not. I also did a strcpy of the filename to a s[FILENAME] and called the sub with a pointer to that but it fails.

What mystifies me is if I put a print statement on the first line of xml_load_file(), it shows the wrong filename; it never receives the correct one.
Expand|Select|Wrap|Line Numbers
  1. #define DEALS "../options/deals.xml"
  2.  
  3. void test_load(char *p) {
  4.    printf("%s\n", xml_load_file(DEALS);
  5.    }
  6.  
  7. void xml_load_file(char *p) {
  8.    printf("%s\n", p);
  9.    }
  10.  
  11. int main(void) {
  12.    xml_load_file(DEALS);
  13.    return 0;
  14.    }
  15.  
Jul 13 '10 #4
donbock
2,426 Expert 2GB
Wow -- I'm baffled.

In the spirit of blindly poking around, why don't you try changing the way you pass the argument to xml_load_file. Try something like this, so we're passing an explicit pointer to string:
Expand|Select|Wrap|Line Numbers
  1. char *DEALS = "../options/deals.xml";
  2. ...
  3. xml_load_file(DEALS);
Perhaps it is a cut-and-paste error here in the forum, but you show test_load printing the return value from xml_load_file (which returns void!) rather than its pointer argument. For that matter, I don't see anybody calling test_load.

So, are you saying that xml_load_file gets the correct string if it is called directly by main; but that it gets the wrong string if it is called by test_load, which is called by main?

By the way, is the xml_load_file argument a char* or a const char*? If the former, then is there any chance that somebody is overwriting the string before you print it?
Jul 13 '10 #5
drhowarddrfine
7,435 Expert 4TB
If I call xml_load_file from main, it receives the correct file name. If I call xml_load_file from the problem module, it receives the incorrect file name.

I created test_load in the same file as xml_load_file just to see what would happen. From main it works. From the problem module it doesn't.

I'm thinking there must be something strange in that file causing this and I might spend time today moving that stuff to another file one at a time to see what happens.

I'd use gdb but it's a web app and I can't remember how I tie into that when it starts up. I've looked at the assembly and it appears to be pointing at a reasonable address but haven't had a chance to delve deeper into it.

I feel better having others say the same as I feel (baffled). Most of my posts here are when I'm panicing under pressure or stupid mistakes from exhaustion. This may be both but I just don't see it.
Jul 13 '10 #6
donbock
2,426 Expert 2GB
Different modules ... that sounds like a variable-scope issue.

Try changing test_load from
Expand|Select|Wrap|Line Numbers
  1. void test_load(char *p) { 
  2.    printf("%s\n", xml_load_file(DEALS)); 
  3.    } 
to
Expand|Select|Wrap|Line Numbers
  1. void test_load(char *p) {
  2.    printf("%s\n", p);
  3.    xml_load_file(p);
  4.    }
That is, have test_load use its pointer argument rather than be hard-coded for DEALS.

If you call test_load from main, then the scope of the DEALS passed to xml_load_file is the same regardless of whether main calls test_load or xml_load_file.

By the way, is DEALS defined using the __FILE__ built-in macro? That is one way to make sure it has a different value in different modules.
Jul 13 '10 #7
drhowarddrfine
7,435 Expert 4TB
I had already tried what you show. Don't use __FILE__.

I narrowed it down to a line where I can get it to work to that point but I have to leave for the day.
Jul 13 '10 #8
drhowarddrfine
7,435 Expert 4TB
Just passing through.

I created an exact copy of xml_load_file as xml_load_file2. In both routines I do:
Expand|Select|Wrap|Line Numbers
  1. xml_load_file(DEALS);
  2. xml_load_file2(DEALS);
  3.  
  4. void
  5. xml_load_file(char* p)
  6. {
  7.    printf("%s\n",p);
  8. }
  9.  
The first prints the wrong file name. The second works.

I have to leave but this is just too bizarre. I'm going to look at the assembly of that last compile tonight. I'll probably compile it on my FreeBSD box to see what's different. I even did a virus scan. I'm wondering if something's gone wrong with this install of the compiler or something but everything else I've done since works fine.
Jul 13 '10 #9
donbock
2,426 Expert 2GB
Are you certain that neither xml_load_file nor any of the library functions it calls write via the pointer argument? If so, then the first call to xml_load_file might corrupt the string, leading to the mysterious symptoms you're seeing.

Whether or not string literals are const is platform dependent, so it is not necessarily impossible to corrupt them.

It might be interesting to see what compiler warnings you get if you change xml_load_file to take a "const char*" argument.
Jul 13 '10 #10
Oralloy
985 Expert 512MB
Two questions:
1) Is it possible that you are linking with an out-of-date object module.
2) Are you sure that you aren't overriding the macro

Try using full compiler warnings and see if anything useful pops out.
Jul 13 '10 #11
weaknessforcats
9,208 Expert Mod 8TB
I am not able to reproduce your error. Your otiginal code works OK. So does a copy of xml_load_file as xml_load_file2.

I am using Visual C++.NET 2008 and I built both a C and C++ with no errors each time.
Jul 14 '10 #12
drhowarddrfine
7,435 Expert 4TB
This has gotten just too bizarre. I'm going to do some picking apart.

In the mean time, i've got a new puppy today!!!
Jul 14 '10 #13
Oralloy
985 Expert 512MB
@drhowarddrfine
Congratulations!
Jul 14 '10 #14
drhowarddrfine
7,435 Expert 4TB
Found it. Some older code I call free'd an allocation too early for this implementation. It's been sitting in this for ages so reusing it should cause no harm, I thought. Unfortunately, the string pointed to is in that area. The simple fix is to copy the string instead.
Jul 15 '10 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Koen | last post by:
Hi all, I created a function that updates a certain status field. The status can be influenced by four different other fields. So, on the form where I edit these fields I call the function (see...
2
by: Jack | last post by:
It appears that RecordsAffected reports the wrong value when a Null integer or date value is inserted using empty quotes. The insert works fine, but RecordsAffected reports 0 instead of 1. Here...
3
by: Stephan | last post by:
Hi, I'm working on a page with multiple Datalist-controls on it. The problem I'm facing is that when I try to update a record, the ItemIndex returns a value which is one lower than the Item I...
0
by: blekros | last post by:
All, I have a web service client written in VB.NET. It is a class library assembly (.dll file). I made a web reference to a web service and set it to be dynamic, so it puts the URL of the...
4
by: Jeff Boes | last post by:
(I thought I posted this yesterday from Google Groups, but it doesn't appear to have "taken".) I'm having a problem with a rule designed to log new rows inserted into one table. The base table...
9
by: PeterWellington | last post by:
I have a column in a data table that stores enum values and assigns a default value: Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek)) dc.DefaultValue = DayOfWeek.Thursday When I...
1
by: colleen1980 | last post by:
Hi: I try both of them but when i check in debugger both of them shows the wrong value. k = Combo2.Value k = Forms!frmDHG!Combo2.Value Thank You.
5
by: gabba | last post by:
Hi, is it possible to call a subroutine (or a function) using variable name? Sub a() Response.write("sub a") End sub Sub b() Response.write("sub b") End sub
2
by: james | last post by:
(Sorry if this is a double post) Hi Guys, I am creating a delegate a couple times and passing in a local variable. However, when the delegate is invoked it uses the variable passed into the...
4
by: jdom | last post by:
i have been struggling with this error for a while , i changed to many times to make sure no error but no luck Error on line 29 strsql.Parameters("@columna") = strmake <%
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.