From: Rami Chowdhury on
On Thursday 01 July 2010 16:50:59 Lawrence D'Oliveiro wrote:
> Nevertheless, it it at least self-consistent. To return to my original
> macro:
>
> #define Descr(v) &v, sizeof v
>
> As written, this works whatever the type of v: array, struct, whatever.
>

Doesn't seem to, sorry. Using Michael Torrie's code example, slightly
modified...

[rami(a)tigris ~]$ cat example.c
#include <stdio.h>

#define Descr(v) &v, sizeof v

int main(int argc, char ** argv)
{
char *buf = malloc(512 * sizeof(char));
const int a = 2, b = 3;
snprintf(Descr(buf), "%d + %d = %d\n", a, b, a + b);
fprintf(stdout, buf);
free(buf);
return 0;
} /*main*/

[rami(a)tigris ~]$ clang example.c
example.c:11:18: warning: incompatible pointer types passing 'char **', expected
'char *' [-pedantic]
snprintf(Descr(buf), "%d + %d = %d\n", a, b, a + b);
^~~~~~~~~~
example.c:4:18: note: instantiated from:
#define Descr(v) &v, sizeof v
^~~~~~~~~~~~
<<snip>>
[rami(a)tigris ~]$ ./a.out
Segmentation fault


----
Rami Chowdhury
"Passion is inversely proportional to the amount of real information available."
-- Benford's Law of Controversy
+1-408-597-7068 / +44-7875-841-046 / +88-01819-245544
From: Lawrence D'Oliveiro on
In message <mailman.136.1278040489.1673.python-list(a)python.org>, Rami
Chowdhury wrote:

> On Thursday 01 July 2010 16:50:59 Lawrence D'Oliveiro wrote:
>
>> Nevertheless, it it at least self-consistent. To return to my original
>> macro:
>>
>> #define Descr(v) &v, sizeof v
>>
>> As written, this works whatever the type of v: array, struct, whatever.
>
> Doesn't seem to, sorry. Using Michael Torrie's code example, slightly
> modified...
>
> char *buf = malloc(512 * sizeof(char));

Again, you misunderstand the difference between a C array and a pointer.
Study the following example, which does work, and you might grasp the point:

ldo(a)theon:hack> cat test.c
#include <stdio.h>

int main(int argc, char ** argv)
{
char buf[512];
const int a = 2, b = 3;
snprintf(&buf, sizeof buf, "%d + %d = %d\n", a, b, a + b);
fprintf(stdout, buf);
return
0;
} /*main*/
ldo(a)theon:hack> ./test
2 + 3 = 5

From: Rami Chowdhury on
On Friday 02 July 2010 19:20:26 Lawrence D'Oliveiro wrote:
> In message <mailman.136.1278040489.1673.python-list(a)python.org>, Rami
> Chowdhury wrote:
> > On Thursday 01 July 2010 16:50:59 Lawrence D'Oliveiro wrote:
> >> Nevertheless, it it at least self-consistent. To return to my original
> >>
> >> macro:
> >> #define Descr(v) &v, sizeof v
> >>
> >> As written, this works whatever the type of v: array, struct, whatever.
> >
> > Doesn't seem to, sorry. Using Michael Torrie's code example, slightly
> > modified...
> >
> > char *buf = malloc(512 * sizeof(char));
>
> Again, you misunderstand the difference between a C array and a pointer.
> Study the following example, which does work, and you might grasp the
> point:
>
> ldo(a)theon:hack> cat test.c
> #include <stdio.h>
>
> int main(int argc, char ** argv)
> {
> char buf[512];
> const int a = 2, b = 3;
> snprintf(&buf, sizeof buf, "%d + %d = %d\n", a, b, a + b);
> fprintf(stdout, buf);
> return
> 0;
> } /*main*/
> ldo(a)theon:hack> ./test
> 2 + 3 = 5

I'm sorry, perhaps you've misunderstood what I was refuting. You posted:
> >> macro:
> >> #define Descr(v) &v, sizeof v
> >>
> >> As written, this works whatever the type of v: array, struct, whatever.

With my code example I found that, as others have pointed out, unfortunately it
doesn't work if v is a pointer to a heap-allocated area.

----
Rami Chowdhury
"A man with a watch knows what time it is. A man with two watches is never
sure". -- Segal's Law
+1-408-597-7068 / +44-7875-841-046 / +88-01819-245544
From: David Cournapeau on
On Mon, Jun 28, 2010 at 6:44 PM, Gregory Ewing
<greg.ewing(a)canterbury.ac.nz> wrote:
> Carl Banks wrote:
>
>> Indeed, strncpy does not copy that final NUL if it's at or beyond the
>> nth element.  Probably the most mind-bogglingly stupid thing about the
>> standard C library, which has lots of mind-boggling stupidity.
>
> I don't think it was as stupid as that back when C was
> designed

Actually, strncpy had a very specific use case when it was introduced
(dealing with limited-size entries in very old unix filesystem). It
should never be used for C string handling, and I don't think it is
fair to say it is stupid: it does exactly what it was designed for. It
just happens that most people don't know what it was designed for.

David
From: Lawrence D'Oliveiro on
In message <mailman.182.1278126257.1673.python-list(a)python.org>, Rami
Chowdhury wrote:

> I'm sorry, perhaps you've misunderstood what I was refuting. You posted:
>> >> macro:
>> >> #define Descr(v) &v, sizeof v
>> >>
>> >> As written, this works whatever the type of v: array, struct,
>> >> whatever.
>
> With my code example I found that, as others have pointed out,
> unfortunately it doesn't work if v is a pointer to a heap-allocated area.

It still correctly passes the address and size of that pointer variable. It
that's not what you intended, you shouldn't use it.