M
Magnus Malm
Hello
I am not quite sure if this is the right place to post this question, but I
hope this group is one of the rare ones that forgive less wise ones like
me.
I have quite a hard time trying to figure out what I am doing wrong.
Pointers are involved, and structures, and functions taking pointers as
parameters and returning pointers. It's one big pointer mess really.
I am coding and compiling under Win98 (MS-DOS) using DJGPP, GCC version
3.22 with the -g switch (though debugging won't work ). The machine is an
AMD Athlon 1.2GHz, 512MB SDRAM. Hope I diden't miss any important specs.
Now to the problem at hand. I successfully manage to compile the following
program:
typedef struct {
char *name;
char *msg;
char *desc;
} testt;
testt *init_test(testt *test, char *name, char *msg, char *desc);
int main (int argc, char **argv){
testt *test;
test = init_test(test, "Test name", "Test msg", "Test desc");
free(test->name);
free(test->msg);
free(test->desc);
free(test);
return 0;
}
testt *init_test(testt *test, char *name, char *msg, char *desc){
test = (testt *)malloc(sizeof(testt *));
test->name = malloc(sizeof(char) * (strlen(name)));
test->msg = malloc(sizeof(char) * (strlen(msg)));
test->desc = malloc(sizeof(char) * (strlen(desc)));
strcpy(test->name, name);
strcpy(test->msg, msg);
strcpy(test->desc, desc);
return test;
}
Obviously, this is no real program, only the basic principle that I use in
the real project. When I run this program, it crashes with the following
ouput:
Exiting due to signal SIGSEGV
General Protection Fault at eip=000036d5
eax=ffffffd4 ebx=ffffffcc ecx=ffffffcc edx=00000000 esi=0008dda4
edi=ffffffd4
ebp=0008d938 esp=0008d92c
program=F:\CODING\PROJECTS\C\TW\CORE\TEST\PROBE.EXE
cs: sel=00a7 base=83ed2000 limit=0009ffff
ds: sel=00b7 base=83ed2000 limit=0009ffff
es: sel=00b7 base=83ed2000 limit=0009ffff
fs: sel=0087 base=00019190 limit=0000ffff
gs: sel=00c7 base=00000000 limit=0010ffff
ss: sel=00b7 base=83ed2000 limit=0009ffff
App stack: [0008d978..0000d978] Exceptn stack: [0000d8d8..0000b998]
Call frame traceback EIPs:
0x000036d5
0x00001727
0x00003068
I am guessing that it has something to do with illegal memory access or the
like. But that is just a guess.
I really hope someone have the kindness to shed some light on this problem.
Because I am clueless.
Many thanks in advance.
/Locke
I am not quite sure if this is the right place to post this question, but I
hope this group is one of the rare ones that forgive less wise ones like
me.
I have quite a hard time trying to figure out what I am doing wrong.
Pointers are involved, and structures, and functions taking pointers as
parameters and returning pointers. It's one big pointer mess really.
I am coding and compiling under Win98 (MS-DOS) using DJGPP, GCC version
3.22 with the -g switch (though debugging won't work ). The machine is an
AMD Athlon 1.2GHz, 512MB SDRAM. Hope I diden't miss any important specs.
Now to the problem at hand. I successfully manage to compile the following
program:
typedef struct {
char *name;
char *msg;
char *desc;
} testt;
testt *init_test(testt *test, char *name, char *msg, char *desc);
int main (int argc, char **argv){
testt *test;
test = init_test(test, "Test name", "Test msg", "Test desc");
free(test->name);
free(test->msg);
free(test->desc);
free(test);
return 0;
}
testt *init_test(testt *test, char *name, char *msg, char *desc){
test = (testt *)malloc(sizeof(testt *));
test->name = malloc(sizeof(char) * (strlen(name)));
test->msg = malloc(sizeof(char) * (strlen(msg)));
test->desc = malloc(sizeof(char) * (strlen(desc)));
strcpy(test->name, name);
strcpy(test->msg, msg);
strcpy(test->desc, desc);
return test;
}
Obviously, this is no real program, only the basic principle that I use in
the real project. When I run this program, it crashes with the following
ouput:
Exiting due to signal SIGSEGV
General Protection Fault at eip=000036d5
eax=ffffffd4 ebx=ffffffcc ecx=ffffffcc edx=00000000 esi=0008dda4
edi=ffffffd4
ebp=0008d938 esp=0008d92c
program=F:\CODING\PROJECTS\C\TW\CORE\TEST\PROBE.EXE
cs: sel=00a7 base=83ed2000 limit=0009ffff
ds: sel=00b7 base=83ed2000 limit=0009ffff
es: sel=00b7 base=83ed2000 limit=0009ffff
fs: sel=0087 base=00019190 limit=0000ffff
gs: sel=00c7 base=00000000 limit=0010ffff
ss: sel=00b7 base=83ed2000 limit=0009ffff
App stack: [0008d978..0000d978] Exceptn stack: [0000d8d8..0000b998]
Call frame traceback EIPs:
0x000036d5
0x00001727
0x00003068
I am guessing that it has something to do with illegal memory access or the
like. But that is just a guess.
I really hope someone have the kindness to shed some light on this problem.
Because I am clueless.
Many thanks in advance.
/Locke