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

Function call sequence

mjm
Folks,

I have the following problem: when using arrays or matrices I often
write past the index bounds. To prevent this from happening I check
the bounds and return an error message such as

Matrix: index i=... not in [....]

However this is only of limited help since all I know now is that the
subscripting operator of Matrix was applied incorrectly.

What I really would like to know is what function called this
subscripting operator so I can tell where the mistake happened.

Is there a good solution to this?
Jul 19 '05 #1
8 4648


mjm wrote:

Folks,

I have the following problem: when using arrays or matrices I often
write past the index bounds. To prevent this from happening I check
the bounds and return an error message such as

Matrix: index i=... not in [....]

However this is only of limited help since all I know now is that the
subscripting operator of Matrix was applied incorrectly.

What I really would like to know is what function called this
subscripting operator so I can tell where the mistake happened.

Is there a good solution to this?


No.
The best thing is dependent on your development system.
Figure out how to stop program execution and switch to
a debugger. The debugger then can be used to walk back the call
stack.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2


Karl Heinz Buchegger wrote:

mjm wrote:

Folks,

I have the following problem: when using arrays or matrices I often
write past the index bounds. To prevent this from happening I check
the bounds and return an error message such as

Matrix: index i=... not in [....]

However this is only of limited help since all I know now is that the
subscripting operator of Matrix was applied incorrectly.

What I really would like to know is what function called this
subscripting operator so I can tell where the mistake happened.

Is there a good solution to this?


No.
The best thing is dependent on your development system.
Figure out how to stop program execution and switch to
a debugger. The debugger then can be used to walk back the call
stack.


Actually there is a way, which involves use of the preprocessor.

Assuming that your access to the matrix is enclosed in a function.

int GetValue( int x, int y );

you can add additional arguments to this function:

int GetValue( int x, int y, const char* File, const char* Line )
{
if( x_or_y_are_out_of_bounds ) {
cout << "out of bounds, called from " << File << " Line: " << Line << endl;
...
}
}

Normally you would use this function as in

z = GetValue( 3, 4, __FILE__, __LINE__ );

But since this gets boring, you use the preprocessor
to create a macro which brings in the last 2 arguments.

#define GetValue(a,b) GetValue(a,b,__FILE__,__LINE__)

z = GetValue(3, 4);

This also has the advantage that you can create different versions
for the debug and release builds easily.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
MJM>all I know now is that the subscripting operator of
MJM>Matrix was applied incorrectly.

How does the subscripting operator of Matrix looks like? Or,
if that is easier, the whole of class Matrix? I need some context
to think about your question..

-X

Jul 19 '05 #4
Karl Heinz Buchegger wrote:
Actually there is a way, which involves use of the preprocessor.

Assuming that your access to the matrix is enclosed in a function.

int GetValue( int x, int y );

you can add additional arguments to this function:

int GetValue( int x, int y, const char* File, const char* Line )
{
if( x_or_y_are_out_of_bounds ) {
cout << "out of bounds, called from " << File << " Line: " << Line <<
endl; ...
}
}

Normally you would use this function as in

z = GetValue( 3, 4, __FILE__, __LINE__ );

But since this gets boring, you use the preprocessor
to create a macro which brings in the last 2 arguments.

#define GetValue(a,b) GetValue(a,b,__FILE__,__LINE__)

z = GetValue(3, 4);

This also has the advantage that you can create different versions
for the debug and release builds easily.


Since __FILE__ and __LINE__ maybe provided by the preprocessor You should
use __func__ instead which is guaranteed to be a local variable (according
to C99).
--
Best Regards
Sven
Jul 19 '05 #5


Sven Gohlke wrote:


Since __FILE__ and __LINE__ maybe provided by the preprocessor You should
use __func__ instead which is guaranteed to be a local variable (according
to C99).


as you say: C99
but this is comp.lang.c++

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #6
mjm
You can download the whole class from
http://martingale.berlios.de/Martingale.html
by following the link to CVS
(don't go to download -- that's Java code).

The subscript checking is in Array.h.

I don't think the context is necessary though.
The problem is to identify from within a call to a C++ function g(...)
the function which made the call to g(...).

I don't understand the solutions offered so far.
The function g could look like

void g(const RealArray1D& x)
{
for(int i=0;i<n;i++) {/*do something with x[i]*/}
}

The perpetrator i is local and does not show up in the
signature of g. I guess stopping execution and using the debugger
is the only solution.

For example in Java you get the call sequence
when an exception is thrown.
Jul 19 '05 #7
mjm
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
Sven Gohlke wrote:


Since __FILE__ and __LINE__ maybe provided by the preprocessor You should
use __func__ instead which is guaranteed to be a local variable (according
to C99).


as you say: C99
but this is comp.lang.c++


OK, I believe I get it now.
The const char* go into the subscripting operator which is then always
called as

X(i,j,__FILE__,__LINE__)

I learned something.
What happens when the strings are passed.
If the whole string is copied this could slow down things by several
orders of magnitude since subscripting typically happens in the
innermost loops.

Anyway I like it.

Many thanks.
Jul 19 '05 #8


mjm wrote:

Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
Sven Gohlke wrote:


Since __FILE__ and __LINE__ maybe provided by the preprocessor You should
use __func__ instead which is guaranteed to be a local variable (according
to C99).
as you say: C99
but this is comp.lang.c++


OK, I believe I get it now.
The const char* go into the subscripting operator which is then always
called as

X(i,j,__FILE__,__LINE__)


yep. The additional macro just hides this. So the programmer
doesn't need to remember to add those 2 arguments.

I learned something.
What happens when the strings are passed.
If the whole string is copied this could slow down things by several
orders of magnitude since subscripting typically happens in the
innermost loops.
Look again at the function signature. There are no strings
involved, only character pointers.

Anyway I like it.


It's a littel bit of help. But breaking to the debugger is
usually better. The reason is that knowing where this specific
call came from often isn't very usefully. The calling function
was called from somewhere else, which was called elsewhere, etc.
So a complete call stack is needed often but not provided with this
technique.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #9

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

Similar topics

0
by: schaf | last post by:
Hi ! I'm writing a new xsl:function, which uses two other functions. But by the call of the first function, it would be abort just after the call. Not even the xsl:param would be set. I don't...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
20
by: sam_cit | last post by:
Hi Everyone, I have the following code, int main() { int p = {10,20,30,40,50}; int *i = &p; printf("before : %p\n",(void*)i); printf("1 %d %d\n",*++i,*i++);
14
by: subramanian100in | last post by:
In the following link, http://www.c-faq.com/malloc/retaggr.html The following ANSWER is given to a question(comp.lang.c FAQ list · Question 7.5a) : Whenever a function returns a pointer, make...
3
by: joe | last post by:
Consider the following program: include <iostream> class Bar { public: int getData9() { m_data = 9; return m_data;} int getData11() { m_data = 11; return m_data;} int m_data;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.