auto allocation

8 posts / 0 new
Last post
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 2 years 8 months ago
Joined: 2012-01-08 20:57
auto allocation
Since in the code below struct fulist gets allocated its memory automatically, (144 bytes) as the Printf confirms, why do i have a crash on the 2d line of main ?
  1. struct fulist {
  2. char curdir[140];
  3. LONG dirtime;
  4.  
  5. }*fls;
  6.  
  7. int main(void) ///char *dirname)
  8. {
  9. Printf("size fulist %ld\n", sizeof(struct fulist));
  10. fls->curdir[0] = 0; //crash
  11. }
billyfish
billyfish's picture
Offline
Last seen: 5 years 11 months ago
Joined: 2012-01-23 20:45
The Printf doesn't confirm
The Printf doesn't confirm you've allocate the space, it simply calculates the size of the structure. You'll need to use malloc, AllocMem, etc. to get it to point at accessible memory e.g.
  1. if (fls = (struct fulist *) AllocVec (sizeof (struct fulist)))
  2. {
  3. fls->curdir[0] = 0;
  4.  
  5. FreeVec (fls);
  6. }
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 2 years 8 months ago
Joined: 2012-01-08 20:57
@billyfish Must i understand
@billyfish Must i understand that structures can never be seean as variables that are auro allocated when entering a bloc or declared as extern? That was what i was trying to test.
billyfish
billyfish's picture
Offline
Last seen: 5 years 11 months ago
Joined: 2012-01-23 20:45
If you want to allocate it
If you want to allocate it from the stack then you'd have something like the code below. Notice that fls is no longer a pointer and you'd access the elements using . rather than ->
  1. struct fulist
  2. {
  3. ...
  4. };
  5.  
  6. int main (void)
  7. {
  8. struct fulist fls;
  9. }
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 2 years 8 months ago
Joined: 2012-01-08 20:57
@billyfish thanks i go things
@billyfish thanks i go things mixed up. But why do i get a crash here
  1. #include <exec/types.h>
  2. #include <proto/exec.h>
  3. #include <exec/exec.h>
  4. #include <dos/dos.h>
  5. #include <dos/datetime.h>
  6. #include <proto/dos.h>
  7.  
  8. struct GCmain ///def/glob_GCmain.h
  9. {
  10. //UBYTE dateformat; // default = 3 (dd-mm-yy)
  11. uint8 dateformat;
  12. char something[2000];
  13. };
  14.  
  15. int main(void)
  16. {
  17. struct GCmain *gd;
  18. gd->dateformat = FORMAT_CDN; //3; crashes
  19. IDOS->Printf("OK\n");
  20. return 1 ;
  21. }
I do see the OK being printed though
Belxjander
Belxjander's picture
Offline
Last seen: 3 years 7 months ago
Joined: 2011-02-25 11:53
is FORMAT_CDN #define'd as a
is FORMAT_CDN #define'd as a direct object itself or the pointer to a static instance of the object? when you put "struct name {};" you are only declaring a class "class" change "struct GCMain {} gd;" and make reference to the "gd" global structure using dynamic constructs inside it. otherwise using "struct GCMain *gd" anywhere in code is asking for a pointer which does not point to anything initially (you trigger the access violation as you don't say *what* to point to that I can see) so either fill the pointer in by Allocation of a Memory block to contain the structure, or use the above change I suggested to have it allocated when the program is loaded into memory as part of the .data or .bss sections of your program (I don't know specifically where it is).
OldFart
OldFart's picture
Offline
Last seen: 7 months 3 weeks ago
Joined: 2010-11-30 14:09
@JosDuchIt This
@JosDuchIt This line:
  1. struct GCmain *gd;
tells the whole story: it is a pointer to an area which, when existed, would be treated as a structure of type GCmain. The point being that that area does not exist anywhere, so the pointer is probably pointing to NULL, which will lead to a crash. QED. Allocating the struct on the stack:
  1. struct GCmain GD; // Now the structure is allocated some space
  2. struct GCmain *gd = &GD; // And now the pointer points to the address of that space
You might also write it like this:
  1. struct GCmain *gd = (struct GCmain *)IExec->AllocVecTags(sizeof(struct GCmain), AVT_ClearWithValue, 0, TAG_END);
  2.  
  3. if (gd)
  4. {
  5. gd->dateformat = FORMAT_CDN; //3; NO LONGER crashes
  6. IDOS->Printf("OK\n");
  7.  
  8. IExec->FreeVec(gd);
  9. }
  10. else
  11. {
  12. IDOS->Printf("ERROR: Failed to Allocate Memory (struct GCmain)\n");
  13. }
N.b.: a macro that I use for allocating structures is this one:
  1. #define ALLOCATE_STRUCT(StrucType) (StrucType *)IExec->AllocVecTags(sizeof(StrucType), AVT_ClearWithValue, 0, TAG_END)
I think it makes code a bit more readable. The example above is then rewritten like this:
  1. struct GCmain *gd = ALLOCATE_STRUCT(struct GCmain);
  2.  
  3. if (gd)
  4. {
  5. gd->dateformat = FORMAT_CDN; //3; NO LONGER crashes
  6. IDOS->Printf("OK\n");
  7.  
  8. IExec->FreeVec(gd);
  9. }
  10. else
  11. {
  12. IDOS->Printf("ERROR: Failed to Allocate Memory (struct GCmain)\n");
  13. }
OldFart
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 2 years 8 months ago
Joined: 2012-01-08 20:57
@Belxjander thanks for
@Belxjander thanks for expanations and @OldFart for the very helpfull examples. I do see now that i did copy wrongly the example given by BillyFish.w
AllocVecTags(sizeof(StrucType), AVT_ClearWithValue, 0, TAG_END)
I am glad to see this form. Till now i was convinced you had to use the AVT_Type tag too.
Log in or register to post comments