![]() |
|
|
#210 |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
263616 Posts |
|
|
|
|
|
|
#211 | |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
160658 Posts |
Quote:
Also, re: gdb: Code:
Where are we? If you want to know where you are in the program's execution (and how, to some extent, you got there), you can view the contents of the stack using the backtrace command as follows: (gdb) bt #0 ComputeFactorial (number=0) at broken.cpp:7 #1 0x08048907 in ComputeSeriesValue (x=3, n=2) at broken.cpp:21 #2 0x08048a31 in main () at broken.cpp:43 |
|
|
|
|
|
|
#212 |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
2·67·73 Posts |
Yes.
Yes. Of the stack frame for each function call which led you to where you are. Last fiddled with by chalsall on 2012-02-25 at 18:39 |
|
|
|
|
|
#213 | |
|
Aug 2005
Seattle, WA
3×19×31 Posts |
Quote:
Well no, I didn't say that. There are actually some structural problems with your ReadLine function, but I figured you would mostly find and fix those yourself while you were finding your bugs. I think that's especially likely to be true if you use a debugger to find your bugs, because then you will actually see your code in action and watch how it's jumping around. |
|
|
|
|
|
|
#214 |
|
Aug 2005
Seattle, WA
3×19×31 Posts |
Sure. I didn't point that out in part because that wasn't something he was asking about originally; i.e. he asked about doing line-based I/O and so I figured it was worth helping him do that right and learn as much as he could about it. But mostly it's because I'm confident that it's something he will eventually see for himself. Right now Dubslow is obviously still learning about dynamic allocation, string handling and I/O in C. Let him get his head around those and learn them well and he will be able to step back and think carefully about whether they're the best tool for a particular job.
|
|
|
|
|
|
#215 |
|
Aug 2005
Seattle, WA
110111001112 Posts |
To expand on what Christenson and chalsall have said about macros, there are what are called function-like macros, which can take arguments. They mostly do simple text replacement (like your old definition of MAXSTRLEN), but they can bind the arguments and do the Right Thing with them. As an example, consider the following simple macro and some code that uses it:
Code:
#define SQR(x) x * x // WARNING: don't use as is (more below)
void PrintSquare(int n)
{
int s;
s = SQR(n);
printf("The square of %d is %d\n", n, s);
}
Code:
void PrintSquare(int n)
{
int s;
s = n * n;
printf("The square of %d is %d\n", n, s);
}
On the other hand, macros can be dangerous if you don't know how to use or write them correctly. For example, the macro above has three big problems with how it works (vs. how the equivalent function would work). Can you figure out what any of them are? Hint: think about some more complicated ways that one might use such a macro in code. |
|
|
|
|
|
#216 |
|
Jan 2008
France
2×52×11 Posts |
|
|
|
|
|
|
#217 |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
2·67·73 Posts |
|
|
|
|
|
|
#218 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3·29·83 Posts |
@Christenson: That's what I get for modifying this from Java (this cypher problem was part of our weekly MP). Command line TextIO is a bit harder in Java (something about scanners) so instead we've been making use of the TextIO class that's out there on the interwebz, which means that to read input we were all basically pointed towards TextIO.getln();, which is more or less the same specification that jyb gave for ReadLine (excepting the null string terminator, of course). Now that you mention it (and now that I go back and reread your post) it does make more sense just to read one character at a time, encode and print. However, jyb is right in that at this point, it's just getting a handle on the concepts.
I did find a page about how to use gdb, which is where my question that chalsall answered came from; however, I understand neither the original answer or its correction. The only thing I don't know how to do is step out of a function (you mean skipping the rest of the code in the function, as opposed to just 'next' out of the last line?). However, I think a quick google ought to be able to shed at least a little bit of light on these things. (I did just try a 'man backtrace', but that pointed me to the functions in C, not what a backtrace is.) Note also that I haven't actually put any more work into this since... Friday? I was out (of my room, not of dorm) being social last night (a rare thing for me). Last fiddled with by Dubslow on 2012-02-26 at 19:53 |
|
|
|
|
|
#219 | |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3×29×83 Posts |
Argh. This might have to take precedence, but I'll try and put some work into C over the week.
Quote:
|
|
|
|
|
|
|
#220 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3×29×83 Posts |
Okay, re: gdb and backtrace and the stack and what not: Did a little bit of Wikipediaing, and this is what I understand:
Code:
(gdb) bt #0 ComputeFactorial (number=0) at broken.cpp:7 #1 0x08048907 in ComputeSeriesValue (x=3, n=2) at broken.cpp:21 #2 0x08048a31 in main () at broken.cpp:43 Edit: Found one bug, that I didn't update size. Code:
if( ((input[i]=getchar()) != '\n') && (input[i] != EOF) ) {
input = (char*)realloc( input, (size *= 2)*sizeof(char) ); // Follow bsquared's usual practice.
if( input == NULL ) {
printf("Error, out of memory!\n");
return NULL;
}
i++; // We're at i==size, continue reading at i==(size+1)
} // skip to end while, continue reading in input up above
Last fiddled with by Dubslow on 2012-02-26 at 22:53 |
|
|
|