|
From: Johannes P. <joh...@pr...> - 2001-06-25 10:13:18
|
To be honest gisan
------------------------------
Deleteing (Freeing ) the System Memory is not a Good Ide . Meaning freeing
argv is not a good Idea. As I
have understood in the time I have worked with C .. It is a System (OS)
specific Variable that you only have a pointer to to access. It is not a
copy of
it in memory that is made for you..
This is all that I can give an answer to for this is how I understand it..
Yes there is a tool that caches memory leaks.. or to put it this was shows
them possibly out.(that is
exept for the one between your ears) But I have to cehck If I can find the
name again and if it is for C for
I know it is for Delphi so far.
Also a note of warning.. The macros that you write there is maybe fancy but
it is
number one, harder to debug and number 2 an give problems if you put a
space between
NEWARRAY and the bracket or funny typo's like that. For I remmeber that in
some compiler they can result in a mispmatc and not be compiled. in..
But otherwise it is hard to debug for you cant trace into that code any
more. Only at lower debug level wise..
Well
good luck
Johannes
At 10:48 24/06/2001 +0200, you wrote:
>Hello !
>
>The small program that follows seems to be correct, with DEV it compiles and
>executes successfully, but I have a few questions about it (see the comment)
>
>test.c
>-------
>/*********************************************************************
>
>Please answer these questions:
>(suppose that argc is 1, so that the if will be executed)
>
>- am I allowed to free argv (before the statement argv = new_argv)?
>
>- at the end of the program. will anybody free argv (and the strings
> pointed by its elements) for me?
>
>- if nobody frees argv, will there be a "memory leak"?
>
>- can a "memory leak" cause trouble?
>
>*********************************************************************/
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>#define ARGV0 "new_program_name"
>#define ARGV1 "new_first_argument"
>
>#define NEWARRAY(ptr, len, T) \
> { \
> ptr = (T *)calloc(len, sizeof(T)); \
> if (!ptr) \
> { \
> fputs("out of memory\n", stderr); \
> exit(EXIT_FAILURE); \
> } \
> }
>
>#define NEWSTRING(ptr, len) \
> NEWARRAY(ptr, len + 1, char)
>
>char *
>copy(const char *from); /* make a copy of FROM */
>
>/********************************************************************/
>int
>main(int argc, char **argv)
>{
> if (argc == 1) /* change the argument list */
> {
> char **new_argv;
>
> argc = 2;
> NEWARRAY(new_argv, argc, char *); /* get memory for arg list */
>
> new_argv[0] = copy(ARGV0);
> new_argv[1] = copy(ARGV1);
>
> free(argv); /* trouble ? */
>
> argv = new_argv; /* who will free new_argv? */
> }
>
> puts("Here is the argument list:");
> while (argc--)
> puts(*argv++);
>
> system("pause");
> return EXIT_SUCCESS;
>}
>
>/*******************************************************************/
>
>char *
>copy(const char *from)
>{
> int len = strlen(from);
> char *s;
>
> NEWSTRING(s, len);
> return strcpy(s, from);
>}
>-------
>
>One last thing: is there any tool that catches memory leaks ?
>
>Thank you for your time.
>
>--
>gisan
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|