Magnus Malm <temp_locke@hotmail.com> scribbled the following:[color=blue]
> Hello[/color]
[color=blue]
> 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. :)[/color]
[color=blue]
> 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.[/color]
[color=blue]
> 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.[/color]
[color=blue]
> Now to the problem at hand. I successfully manage to compile the following
> program:[/color]
[color=blue]
> typedef struct {
> char *name;
> char *msg;
> char *desc;
> } testt;[/color]
[color=blue]
> testt *init_test(testt *test, char *name, char *msg, char *desc);[/color]
[color=blue]
> int main (int argc, char **argv){
> testt *test;[/color]
[color=blue]
> test = init_test(test, "Test name", "Test msg", "Test desc");[/color]
[color=blue]
> free(test->name);
> free(test->msg);
> free(test->desc);
> free(test);[/color]
[color=blue]
> return 0;
> }[/color]
[color=blue]
> testt *init_test(testt *test, char *name, char *msg, char *desc){[/color]
[color=blue]
> test = (testt *)malloc(sizeof(testt *));[/color]
Lose the cast, it's useless. Here's a better version:
test = malloc(sizeof *test);
[color=blue]
> test->name = malloc(sizeof(char) * (strlen(name)));
> test->msg = malloc(sizeof(char) * (strlen(msg)));
> test->desc = malloc(sizeof(char) * (strlen(desc)));[/color]
Lose the casts. Also sizeof(char) is always 1, so multiplying by it is
superfluous.
[color=blue]
> strcpy(test->name, name);
> strcpy(test->msg, msg);
> strcpy(test->desc, desc);[/color]
The problem here is that strlen() does not count the null terminator,
but strcpy() tries to copy it anyway. You have to add it yourself to
the length:
test->name = malloc(strlen(name)+1);
test->msg = malloc(strlen(msg)+1);
test->desc = malloc(strlen(desc)+1);
[color=blue]
> return test;
> }[/color]
[color=blue]
> 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:[/color]
(snip Windows-specific stuff)
[color=blue]
> I am guessing that it has something to do with illegal memory access or the
> like. But that is just a guess.[/color]
It's an illegal memory access. strcpy() is poking its nose where it does
not belong. You did not allocate enough memory for it to poke its nose
to.
[color=blue]
> I really hope someone have the kindness to shed some light on this problem.
> Because I am clueless.[/color]
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
|
http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio