|
Home > Archive > Unix Programming > February 2006 > "struct rusage r" is ok,but struct rusage* r isn't
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
"struct rusage r" is ok,but struct rusage* r isn't
|
|
| DaVinci 2006-02-23, 2:54 am |
| void test_getrusage()
{
struct rusage* r; //take care of here...............
int who =3D RUSAGE_SELF;
if(getrusage(who,r) < 0)//here
{
printf("getrusage error\n");
abort();
}
long maxrss =3D r->ru_maxrss;//and here
printf("max resident set size is %f\n",maxrss);
}
int main()
{
test_getrusage();
exit(0);
}
then I execute it:
apple@40years:~/study/c/book$ ./thread1
getrusage error
=D2=D1=B7=C5=C6=FA
But If I use "struct usage r " instead of " struct usage* r" and some
other corresponding change,then all is ok .
like this:
void test_getrusage()
{
struct rusage r;//take care of here
int who =3D RUSAGE_SELF;
if(getrusage(who,&r) < 0)//here
{
printf("getrusage error\n");
abort();
}
long maxrss =3D r.ru_maxrss;//and here
printf("max resident set size is %f\n",maxrss);
}
int main()
{
test_getrusage();
exit(0);
}
apple@40years:~/study/c/book$ ./thread1
max resident set size is -0.330291
what is the problem?
In my option there is no different of the two programme.
| |
| Barry Margolin 2006-02-23, 2:54 am |
| In article <1140668094.705756.197120@z34g2000cwc.googlegroups.com>,
"DaVinci" <apple.davinci@gmail.com> wrote:
> void test getrusage()
> {
> struct rusage* r; //take care of here...............
> int who = RUSAGE SELF;
> if(getrusage(who,r) < 0)//here
> {
> printf("getrusage error\n");
> abort();
> }
> long maxrss = r->ru maxrss;//and here
> printf("max resident set size is %f\n",maxrss);
> }
>
> int main()
> {
> test getrusage();
> exit(0);
> }
> then I execute it:
> apple@40years:~/study/c/book$ ./thread1
> getrusage error
You've never assigned a value to r. You need to allocate space for the
structure and assign r, e.g.
struct rusage *r = malloc(sizeof (struct rusage));
...
free (r);
BTW, instead of using printf() to report an error, you should use
perror(), so that you'll see what error was reported.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| DaVinci 2006-02-23, 2:54 am |
|
Barry Margolin =E5=86=99=E9=81=93=EF=BC=9A
> In article <1140668094.705756.197120@z34g2000cwc.googlegroups.com>,
> "DaVinci" <apple.davinci@gmail.com> wrote:
>
>
> You've never assigned a value to r. You need to allocate space for the
> structure and assign r, e.g.
>
> struct rusage *r =3D malloc(sizeof (struct rusage));
> ...
> free (r);
>
Thanks.you are right.
But I met another problem when I do what you said.when I execute it
40years:/home/apple/study/c/book# ./thread1
sizeof (struct rusage)72
max resident set size is -0.198844
the problem is that the size of is a negative number.
> BTW, instead of using printf() to report an error, you should use
> perror(), so that you'll see what error was reported.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***
Thanks very much.
| |
| Paul Pluzhnikov 2006-02-23, 2:54 am |
| "DaVinci" <apple.davinci@gmail.com> writes:
> max resident set size is -0.198844
> the problem is that the size of is a negative number.
Garbage in, garbage out. Do "man printf", and read '%f' part and
what type of argument it expects. Hint: it doesn't expect a "long".
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| DaVinci 2006-02-23, 2:54 am |
|
Paul Pluzhnikov =E5=86=99=E9=81=93=EF=BC=9A
> "DaVinci" <apple.davinci@gmail.com> writes:
>
>
> Garbage in, garbage out. Do "man printf", and read '%f' part and
> what type of argument it expects. Hint: it doesn't expect a "long".
>
Thanks. I use 'ld'
printf("%ld",r->ru_maxrss);It works well.
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.
|
|
|
|
|