p++ is something you do when you have allocated an array, to reach the
next object.
Never think that malloc() allocates objects of exact size. It may allocate
objects of any size >=3D the size you requested.
Never assume that multiple malloc() calls will allocate continuous memory.
They may be allocated from any part of the memory. Depending on
architecture and implementation of malloc() it might strive to allocate
new plocks with _lower_ address than previous allocations. Bseides, when
reusing previously released memory, the allocations might come at
completely random addresses.
/Per W
On Mon, 7 Jun 2004, [gb2312] Bruce jjs wrote:
> The pointer p is the head pointer of the link list.And the p1 is poin=
t to p's next link node.After I use the function malloc() to distribute con=
tinuous memory to p and p1.Why the the result of p1-p is 16 not 12 which is=
the size of the struct student?The p->next is a pointer to p's next link n=
ode,also ++p.But why p->next is not equal to ++p? Please forgive my ignoran=
ce,I hope someboby can tell me why.
> ---Thanks,
> Bruce.J
> /* ** ******************************** */
> The program run result is belove:
> The size of "int" is 4
> The size of "p->next" is 4
> The size of "struct student" is 12
> p->next =3D 8783360
> p1 =3D 8783360
> p =3D 8783344
> ++p =3D 8783356
> /* ******************************** */
>
> My program is list belove:
> #include <stdio.h>
> typedef struct student {
> int number;
> int grade;
> struct student *next;
> }student, *stuLink;
>
> main()
> {
> stuLink p, p1;
>
>
> p =3D (stuLink) malloc(sizeof(student));
> p1 =3D (stuLink) malloc(sizeof(student));
> p->next =3D NULL;
> p1->next =3D p->next;
> p->next =3D p1;
> printf("The size of int is %d\n", sizeof(int));
> printf("The size of p->next is %d\n", sizeof(p->next));
> printf("The size of struct student is %d\n", sizeof(Student));
> printf("p->next=3D%d\n", p->next);
> printf("p1=3D%d\n", p1);
> printf("p=3D%d\n", p);
> printf("++p=3D%d\n", ++p);
> free(p);
> free(p1);
> system("PAUSE");
> }
>
>
>
>
> ---------------------------------
> Do You Yahoo!?
> =CF=D3=D3=CA=CF=E4=CC=AB=D0=A1=A3=BF=D1=C5=BB=A2=B5=E7=D3=CA=D7=D4=D6=FA=
=C0=A9=C8=DD=A3=A1
|