* structure size
@ 2003-03-05 7:02 Mohammed Khalid Ansari
2003-03-05 8:27 ` Helmut Djurkin
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Mohammed Khalid Ansari @ 2003-03-05 7:02 UTC (permalink / raw)
To: linux c programming mailing list
Hi,
why the sizeof sturcture is alway greater than the collective sizes of all
the elements in it.
eg
struct node {
int a;
char c;
int b;
};
int main()
{
printf ("%d\n", sizeof (struct node));
return 0;
}
it prints 12 in my case.
where is the catch.
--
**************************************************************************
Mohammed Khalid Ansari Tel (res) : 0091-022-3051360
Assistant Manager II (off) : 0091-022-2024641
National Centre for Software Technology Fax : 0091-022-2049573
8th flr,Air India Build. Nariman Point, E-Mail : khalid@ncst.ernet.in
Mumbai 400021.
Homepage : http://soochak.ncst.ernet.in/~khalid
**************************************************************************
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: structure size
2003-03-05 7:02 structure size Mohammed Khalid Ansari
@ 2003-03-05 8:27 ` Helmut Djurkin
2003-03-25 10:00 ` strange SIGSEGV Alexi Jordanov
2003-03-05 9:06 ` (F)structure size Steven Smith
2003-03-12 22:49 ` getlogin(), prints ((NULL)) when printing multiple times in a row J.
2 siblings, 1 reply; 7+ messages in thread
From: Helmut Djurkin @ 2003-03-05 8:27 UTC (permalink / raw)
To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list
hi,
it's the alignment.
with gcc and this:
struct node {
/* member here */
} __attribute__((packed));
you will get the your "collective sizes".
Mohammed Khalid Ansari wrote:
> Hi,
>
> why the sizeof sturcture is alway greater than the collective sizes of all
> the elements in it.
>
> eg
> struct node {
> int a;
> char c;
> int b;
> };
>
> int main()
> {
> printf ("%d\n", sizeof (struct node));
> return 0;
> }
>
>
> it prints 12 in my case.
>
> where is the catch.
>
^ permalink raw reply [flat|nested] 7+ messages in thread* strange SIGSEGV
2003-03-05 8:27 ` Helmut Djurkin
@ 2003-03-25 10:00 ` Alexi Jordanov
0 siblings, 0 replies; 7+ messages in thread
From: Alexi Jordanov @ 2003-03-25 10:00 UTC (permalink / raw)
To: linux-c-programming
Hello to all,
I have a strange problem with listening for file change notification.
Everything is fine in my handler fuction till the moment when I try to send
this information through pipes:
This works fine:
===========
static void handler(int sig, siginfo_t *si, void *data)
{
fprintf(stderr, "Got an event: %d.\n", si->si_fd);
}
This cause SIGSEGV:
===============
static void handler(int sig, siginfo_t *si, void *data)
{
fprintf(stderr, "Got an event: %d.\n", si->si_fd);
if (write(event_pipe[1], &si->si_fd, sizeof(int)) != sizeof(int))
{
fprintf(stderr, "Write failed (handler) - %s.\n", errstr);
fflush(stderr);
}
}
I try to start debugger to look for the problem, but all that I was found
was:
==================================================
Program received signal SIGSEGV, Segmentation fault.
0x00000004 in ?? ()
(gdb) where
#0 0x00000004 in ?? ()
#1 0x90c3c9ec in ?? ()
Cannot access memory at address 0x5d8bc031
(gdb)
The problem appears when I try to watch simultaneously two times one and the
same directory. But I don't think that this is 100% true, because I don't
have problem with other directories.
Can somebody advise me how to deal with this problem?!
Regards, Alex
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: (F)structure size
2003-03-05 7:02 structure size Mohammed Khalid Ansari
2003-03-05 8:27 ` Helmut Djurkin
@ 2003-03-05 9:06 ` Steven Smith
2003-03-12 22:49 ` getlogin(), prints ((NULL)) when printing multiple times in a row J.
2 siblings, 0 replies; 7+ messages in thread
From: Steven Smith @ 2003-03-05 9:06 UTC (permalink / raw)
To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list
[-- Attachment #1: Type: text/plain, Size: 839 bytes --]
> why the sizeof sturcture is alway greater than the collective sizes of all
> the elements in it.
>
> eg
> struct node {
> int a;
> char c;
> int b;
> };
It is not, in general, possible to (portably) estimate the size of a
structure just by looking at its fields, as the compiler is free to add
padding if that allows better code.
In particular, most systems can only efficiently access memory
locations if the address of the location is a multiple of its size
(i.e. if its naturally aligned). This means that if the structure is
to be stored in an array, its size must be a multiple of the size of
the largest single member, so that every element has proper alignment.
In this case, the largest member is an int, and so the whole
structure's size must be a multiple of four.
Steven Smith,
sos22@cam.ac.uk.
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread* getlogin(), prints ((NULL)) when printing multiple times in a row.
2003-03-05 7:02 structure size Mohammed Khalid Ansari
2003-03-05 8:27 ` Helmut Djurkin
2003-03-05 9:06 ` (F)structure size Steven Smith
@ 2003-03-12 22:49 ` J.
2003-03-12 23:14 ` Glynn Clements
2 siblings, 1 reply; 7+ messages in thread
From: J. @ 2003-03-12 22:49 UTC (permalink / raw)
To: linux-c-programming
Hello,
I have ran into undefined behaviour. Not from my girl-friend this time,
but from my C program. (It's not that time of month, yet... :()
But, Everytime when I execute the following C code (bottom) from within a
shell loop as in:
while : ; do ./program ; done
it prints: ((NULL))
But when I excute it in the normal way:
./program
It prints perfectly ok: myusername
C Code.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void) {
char *username = NULL;
username = getlogin();
/* for(;;) */
printf("%s\n", username);
return 0;
}
When I call the printf from a for loop in the native C code it also
starts to print ((NULL)) after a couple of loops.
I am writing a network program that gives a lot of status output when
working verbose. It doesn't look very good ((NULL)) everytime when the
output is above average.
Anyone ?
Thnkx
J.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: getlogin(), prints ((NULL)) when printing multiple times in a row.
2003-03-12 22:49 ` getlogin(), prints ((NULL)) when printing multiple times in a row J.
@ 2003-03-12 23:14 ` Glynn Clements
2003-03-13 0:18 ` getlogin(), prints ((NULL)) solved.. thnkx.. glen J.
0 siblings, 1 reply; 7+ messages in thread
From: Glynn Clements @ 2003-03-12 23:14 UTC (permalink / raw)
To: J.; +Cc: linux-c-programming
J. wrote:
> But, Everytime when I execute the following C code (bottom) from within a
> shell loop as in:
>
> while : ; do ./program ; done
>
> it prints: ((NULL))
>
> But when I excute it in the normal way:
> ./program
> It prints perfectly ok: myusername
>
> C Code.
> #include <stdio.h>
> #include <unistd.h>
> #include <sys/types.h>
>
> int main(void) {
> char *username = NULL;
>
> username = getlogin();
> /* for(;;) */
> printf("%s\n", username);
>
> return 0;
> }
>
> When I call the printf from a for loop in the native C code it also
> starts to print ((NULL)) after a couple of loops.
Odd; for me, it's consistent: it either always prints "glynn" or
always prints "(null)".
But is getlogin() (i.e. the name from the utmp entry for the
controlling tty) really the information that you want? Or would you be
better off with:
struct passwd *pw = getpwuid(getuid());
pw ? pw->pw_name : "<none>"
?
Bear in mind that not all processes have a controlling terminal, and
even for those that do, there may not be an associated utmp entry.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: getlogin(), prints ((NULL)) solved.. thnkx.. glen.
2003-03-12 23:14 ` Glynn Clements
@ 2003-03-13 0:18 ` J.
0 siblings, 0 replies; 7+ messages in thread
From: J. @ 2003-03-13 0:18 UTC (permalink / raw)
To: linux-c-programming
On Wed, 12 Mar 2003, Glynn Clements wrote:
>
> Jeroen Reynders. wrote:
>
> > But, Everytime when I execute the following C code (bottom) from within a
> > shell loop as in:
> >
> > while : ; do ./program ; done
> >
> > it prints: ((NULL))
> >
> > But when I excute it in the normal way:
> > ./program
> > It prints perfectly ok: myusername
> >
> > C Code.
> > #include <stdio.h>
> > #include <unistd.h>
> > #include <sys/types.h>
> >
> > int main(void) {
> > char *username = NULL;
> >
> > username = getlogin();
> > /* for(;;) */
> > printf("%s\n", username);
> >
> > return 0;
> > }
> >
> > When I call the printf from a for loop in the native C code it also
> > starts to print ((NULL)) after a couple of loops.
>
> Odd; for me, it's consistent: it either always prints "glynn" or
> always prints "(null)".
Cool thankx for trying...
> But is getlogin() (i.e. the name from the utmp entry for the
> controlling tty) really the information that you want? Or would you be
> better off with:
>
> struct passwd *pw = getpwuid(getuid());
> pw ? pw->pw_name : "<none>"
>
> ?
>
> Bear in mind that not all processes have a controlling terminal, and
> even for those that do, there may not be an associated utmp entry.
I havent thought of that one, it's indeed a good one to remember...
I have searched all the documentation I could find this evening and
eventually going back to the getlogin() manual page. It seems that I have
overlooked the `BUGS' section in the end of the manual page.
BUGS
Unfortunately, it is often rather easy to fool getlogin(). Sometimes
it does not work at all, because some program messed up the utmp
file. Often, it gives only the first 8 characters of the login
name. The user currently logged in on the controlling tty of our
program need not be the user who started it. Avoid getlogin() for
security-related purposes.
Hmmm... Again a type of lesson I am not going to forgett easily ... :)
Anyway I solved the problem the way you suggested, the following works
fine on my system.
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void) {
uid_t uid;
struct passwd *pw;
uid = getuid();
pw = getpwuid(uid);
printf("my name is: %s\n", pw->pw_name);
return 0;
}
Thank you for helping me out.
> --
> Glynn Clements <glynn.clements@virgin.net>
Jeroen Reynders
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2003-03-25 10:00 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-03-05 7:02 structure size Mohammed Khalid Ansari
2003-03-05 8:27 ` Helmut Djurkin
2003-03-25 10:00 ` strange SIGSEGV Alexi Jordanov
2003-03-05 9:06 ` (F)structure size Steven Smith
2003-03-12 22:49 ` getlogin(), prints ((NULL)) when printing multiple times in a row J.
2003-03-12 23:14 ` Glynn Clements
2003-03-13 0:18 ` getlogin(), prints ((NULL)) solved.. thnkx.. glen J.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).