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 ?
struct fulist { char curdir[140]; LONG dirtime; }*fls; int main(void) ///char *dirname) { Printf("size fulist %ld\n", sizeof(struct fulist)); fls->curdir[0] = 0; //crash }
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.
@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.
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 ->
@billyfish
thanks i go things mixed up.
But why do i get a crash here
I do see the OK being printed though
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).
@JosDuchIt
This line:
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:
You might also write it like this:
N.b.: a macro that I use for allocating structures is this one:
I think it makes code a bit more readable.
The example above is then rewritten like this:
OldFart
@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
I am glad to see this form. Till now i was convinced you had to use the AVT_Type tag too.