RE: [Dev-C++] Bugs: Unallocated Memory
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2001-09-20 06:54:15
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Nurhidayat
> Sent: Thursday, September 20, 2001 7:06 AM
> To: dev...@li...
> Subject: [Dev-C++] Bugs: Unallocated Memory
>
>
> I've found bugs(?) when trying to code linked list record.
> Compiler cannot allocated base type variable within
> structure, but array of char work fine.
>
> Here is I simplified code:
>
> #include <stdlib.h>
> #include <stdio.h>
>
> struct mystruct {
> char name[8];
> int age;
> };
>
> int main()
> {
> mystruct *ptr; //test dynamic allocation
> int *page;
> ptr = new mystruct; //compiler must allocate memory
> page = new int;
>
> printf("Address of name : %p\n", ptr->name); //allocated
> printf("Address of age : %p\n", ptr->age); //give us
> null pointer, why?
> printf("Address of page : %p\n", page); //allocated
> (for comparison)
These 3 give to me:
Address of name : 025D2440
Address of age : 72676F72
Address of page : 025D2470
But, this is not the address of age. Address of age is
printf("Address of age : %p\n", &ptr->age);
>
> //so ...
> printf("Enter name:"); scanf("%s", ptr->name); //success
> printf("Enter age :"); scanf("%s", ptr->age); //raised error!!!
The correct is scanf("%d", &ptr->age);
> delete ptr;
> delete page;
> system("PAUSE");
> return 0;
> }
>
> ___Nurhidayat___
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|