* naive GDB programming style questions
@ 2002-09-09 16:14 David Carlton
2002-09-09 18:45 ` Andrew Cagney
2002-09-10 8:56 ` Tom Tromey
0 siblings, 2 replies; 7+ messages in thread
From: David Carlton @ 2002-09-09 16:14 UTC (permalink / raw)
To: gdb
1) It seems to me that some parts of GDB's source use NULL while other
parts use 0. Is one or the other of these generally preferred?
2) Am I correct in observing that GDB frowns on code like the
following:
char *p = calculate_p ();
if (!p)
p_is_zero ();
preferring this instead?
char *p = calculate_p ();
if (p == NULL)
p_is_zero ();
Does it matter that p in a pointer rather than an integer, or that
the code is testing for zeroness rather than nonzeroness?
3) Is it possible to get CC Mode to indent in the way that GDB seems
to prefer? I'm having a hard time getting structs to be indented
as follows:
struct foo
{
int mem;
};
without screwing up my preferred indentation when doing non-GDB
programming, namely
struct foo {
int mem;
};
Of course, I can write functions to toggle between the two; but
given that both styles seem to be able to coexist for enums, maybe
it's possible to get them to coexist for enums. (I'm certainly no
CC Mode expert...)
David Carlton
carlton@math.stanford.edu
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: naive GDB programming style questions
2002-09-09 16:14 naive GDB programming style questions David Carlton
@ 2002-09-09 18:45 ` Andrew Cagney
2002-09-09 20:52 ` David Carlton
2002-09-10 8:56 ` Tom Tromey
1 sibling, 1 reply; 7+ messages in thread
From: Andrew Cagney @ 2002-09-09 18:45 UTC (permalink / raw)
To: David Carlton; +Cc: gdb
> 1) It seems to me that some parts of GDB's source use NULL while other
> parts use 0. Is one or the other of these generally preferred?
>
> 2) Am I correct in observing that GDB frowns on code like the
> following:
>
> char *p = calculate_p ();
>
> if (!p)
> p_is_zero ();
I'll assume that you ment ``p_is_null()''.
> preferring this instead?
>
> char *p = calculate_p ();
>
> if (p == NULL)
> p_is_zero ();
>
> Does it matter that p in a pointer rather than an integer, or that
> the code is testing for zeroness rather than nonzeroness?
If you've a copy of the ISO C and C++ manuals, have a look at what they
have to say about ``NULL'' pointers. It's weird.
Anyway, for GDB, ``p == NULL'' is recommended to make it clear that the
pointer is being tested and not the underlying value.
> 3) Is it possible to get CC Mode to indent in the way that GDB seems
> to prefer? I'm having a hard time getting structs to be indented
> as follows:
>
> struct foo
> {
> int mem;
> };
Just use:
struct foo
{
int mem;
};
which is what is output by gdb_indent.sh and emacs. The strange
indentation that you've encountered dates back to an earlier version of
indent.
Must get around to sending the FSF coding standards group a patch that
documents this (it was agreed to in principle some time ago).
> without screwing up my preferred indentation when doing non-GDB
> programming, namely
>
> struct foo {
> int mem;
> };
>
> Of course, I can write functions to toggle between the two; but
> given that both styles seem to be able to coexist for enums, maybe
> it's possible to get them to coexist for enums. (I'm certainly no
> CC Mode expert...)
Andrew
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: naive GDB programming style questions
2002-09-09 18:45 ` Andrew Cagney
@ 2002-09-09 20:52 ` David Carlton
2002-09-10 11:39 ` David Carlton
2002-09-12 11:35 ` Andrew Cagney
0 siblings, 2 replies; 7+ messages in thread
From: David Carlton @ 2002-09-09 20:52 UTC (permalink / raw)
To: Andrew Cagney; +Cc: gdb
On Mon, 09 Sep 2002 21:45:54 -0400, Andrew Cagney <ac131313@ges.redhat.com> said:
> I'll assume that you ment ``p_is_null()''.
Right.
> If you've a copy of the ISO C and C++ manuals, have a look at what
> they have to say about ``NULL'' pointers. It's weird.
Really? Hmm. I know that NULL doesn't work as well in C++ as it does
in C, so I've gotten used to using 0 there.
Though if you're referring to the fact that NULL's underlying bit
representation might not be 0, I'm not sure that's a big deal here.
> Anyway, for GDB, ``p == NULL'' is recommended to make it clear that
> the pointer is being tested and not the underlying value.
Okay, then that's what I'll do.
> Just use:
> struct foo
> {
> int mem;
> };
> which is what is output by gdb_indent.sh and emacs.
Excellent.
David
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: naive GDB programming style questions
2002-09-09 16:14 naive GDB programming style questions David Carlton
2002-09-09 18:45 ` Andrew Cagney
@ 2002-09-10 8:56 ` Tom Tromey
1 sibling, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2002-09-10 8:56 UTC (permalink / raw)
To: David Carlton; +Cc: gdb
>>>>> "David" == David Carlton <carlton@math.stanford.edu> writes:
David> 3) Is it possible to get CC Mode to indent in the way that GDB seems
David> to prefer? I'm having a hard time getting structs to be indented
David> as follows:
David> without screwing up my preferred indentation when doing non-GDB
David> programming, namely
Change your settings depending on the file you are editing.
Here is a piece of code I used for this purpose a long time ago. For
C source files in a particular directory, it uses the Tcl style (a
variant of the BSD style); for other files it uses GNU. Rewrite as
appropriate and attach to your c-mode-hook.
(defun tjt-set-c-style ()
(if (and (buffer-file-name)
(string-match "/home/syzygy/tclstuff" (buffer-file-name)))
(progn
(c-set-style "BSD")
(make-local-variable 'c-basic-offset)
(setq c-basic-offset 4))
(c-set-style "GNU")))
There are probably lots of other ways to accomplish the same thing.
And of course you can set anything there; for instance the behavior of
electric braces (that sounds like what you want).
Tom
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: naive GDB programming style questions
2002-09-09 20:52 ` David Carlton
@ 2002-09-10 11:39 ` David Carlton
2002-09-10 16:15 ` Andrew Cagney
2002-09-12 11:35 ` Andrew Cagney
1 sibling, 1 reply; 7+ messages in thread
From: David Carlton @ 2002-09-10 11:39 UTC (permalink / raw)
To: Andrew Cagney; +Cc: gdb
On 09 Sep 2002 20:52:14 -0700, David Carlton <carlton@math.Stanford.EDU> said:
> On Mon, 09 Sep 2002 21:45:54 -0400, Andrew Cagney <ac131313@ges.redhat.com> said:
>> If you've a copy of the ISO C and C++ manuals, have a look at what
>> they have to say about ``NULL'' pointers. It's weird.
> Though if you're referring to the fact that NULL's underlying bit
> representation might not be 0, I'm not sure that's a big deal here.
Actually, that raises another question: if I'm allocating an array of
pointers that I want initialized to NULL, am I allowed to use
xcalloc() to handle that, or do I have to loop through the memory
myself to set all the pointers to NULL? Because the former is
technically incorrect, but I don't know if GDB runs on any platforms
for which it wouldn't work; I think I've seen parts of GDB initialize
pointers to NULL using xcalloc or memset, but maybe I'm wrong.
David Carlton
carlton@math.stanford.edu
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: naive GDB programming style questions
2002-09-10 11:39 ` David Carlton
@ 2002-09-10 16:15 ` Andrew Cagney
0 siblings, 0 replies; 7+ messages in thread
From: Andrew Cagney @ 2002-09-10 16:15 UTC (permalink / raw)
To: David Carlton; +Cc: gdb
> On 09 Sep 2002 20:52:14 -0700, David Carlton <carlton@math.Stanford.EDU> said:
>
>> On Mon, 09 Sep 2002 21:45:54 -0400, Andrew Cagney <ac131313@ges.redhat.com> said:
>
>
>>> If you've a copy of the ISO C and C++ manuals, have a look at what
>>> they have to say about ``NULL'' pointers. It's weird.
>
>
>> Though if you're referring to the fact that NULL's underlying bit
>> representation might not be 0, I'm not sure that's a big deal here.
>
>
> Actually, that raises another question: if I'm allocating an array of
> pointers that I want initialized to NULL, am I allowed to use
> xcalloc() to handle that, or do I have to loop through the memory
> myself to set all the pointers to NULL? Because the former is
> technically incorrect, but I don't know if GDB runs on any platforms
> for which it wouldn't work; I think I've seen parts of GDB initialize
> pointers to NULL using xcalloc or memset, but maybe I'm wrong.
You can use memset() and xcalloc().
Andrew
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: naive GDB programming style questions
2002-09-09 20:52 ` David Carlton
2002-09-10 11:39 ` David Carlton
@ 2002-09-12 11:35 ` Andrew Cagney
1 sibling, 0 replies; 7+ messages in thread
From: Andrew Cagney @ 2002-09-12 11:35 UTC (permalink / raw)
To: David Carlton; +Cc: gdb
PS: If you encounter a file that has the old ``dud'' struct style
indentation then running it (otherwize unchanged) through gdb_indent.sh
comes under the ``obvious fix rule''. It lets you get any indentation
issues out of the way up before you start making changes.
Andrew
> On Mon, 09 Sep 2002 21:45:54 -0400, Andrew Cagney <ac131313@ges.redhat.com> said:
>
>
>> I'll assume that you ment ``p_is_null()''.
>
>
> Right.
>
>
>> If you've a copy of the ISO C and C++ manuals, have a look at what
>> they have to say about ``NULL'' pointers. It's weird.
>
>
> Really? Hmm. I know that NULL doesn't work as well in C++ as it does
> in C, so I've gotten used to using 0 there.
>
> Though if you're referring to the fact that NULL's underlying bit
> representation might not be 0, I'm not sure that's a big deal here.
>
>
>> Anyway, for GDB, ``p == NULL'' is recommended to make it clear that
>> the pointer is being tested and not the underlying value.
>
>
> Okay, then that's what I'll do.
>
>
>> Just use:
>
>
>> struct foo
>> {
>> int mem;
>> };
>
>
>> which is what is output by gdb_indent.sh and emacs.
>
>
> Excellent.
>
> David
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2002-09-12 18:35 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-09 16:14 naive GDB programming style questions David Carlton
2002-09-09 18:45 ` Andrew Cagney
2002-09-09 20:52 ` David Carlton
2002-09-10 11:39 ` David Carlton
2002-09-10 16:15 ` Andrew Cagney
2002-09-12 11:35 ` Andrew Cagney
2002-09-10 8:56 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox