Peter "Shaggy" Haywood wrote:[color=blue]
> Groovy hepcat
a.zeevi@solidgroup.com was jivin' on 27 Apr 2006
> 03:15:30 -0700 in comp.lang.c.
> free() multiple allocation error in C's a cool scene! Dig it!
>[color=green]
> >I have written a program in C on PC with Windows 2000 in a Visual C
> >environment.[/color]
>
> You mean with Visual C in a Windoze 2000 environment. Windoze is the
> environment. Visual C is what you're programming with.
>[color=green]
> >I have an error in freeing multiple allocation as follows:
> >1. I allocated an array of pointer.
> >2. I Read line by line from a text file.
> >3. I allocated memory for the read line.
> >4. I wrote the pointer from malloc() to the array of pointers.
> >5. The problem is in the free() function. When I tried to free in a
> >loop the
> > allocated lines, the Visual C throw me with an exception.
> >6. I wanted to know why?. I tried also to free in the opposite order I
> >have
> > allocated them, but this also doesn't work.
> >7. Does anyone has an idea?
> >8. Please send answer to
a.zeevi@solidgroup.com .[/color]
>
> No. Post here, read here. That's the custom.
>[color=green]
> >9. Thanks in advance.
> >Ariel Ze'evi.
> >
> >Parts of the code:[/color]
>
> Oh dear! Where to begin... The beginning looks like a likely place.
> I realise some of what I'm about to say has already been said. You can
> skip what you've already seen and just take the other comments, if you
> like.
>[color=green]
> >#include "stdafx.h"[/color]
>
> Non-standard, non-portable header. Judging by the quotes, it's a
> user defined header (or a third party header), but you have not shown
> us what is in it.
>[color=green]
> >#include <stdio.h>
> >#include <stdlib.h>
> >#include <string.h>
> >
> >#define byte unsigned char
> >#define word unsigned short[/color]
>
> What's the point of that? Pascal programmer, are we? This does
> nothing for the legibility of your code. It's just pointless clutter.
> You ought to start getting used to using C's own language, and not try
> to change it into some grotesque half-arsed imitation of another
> language.
>[color=green]
> >typedef struct {
> > word FlashAddress, index;
> >} IndexAddress;
> >
> >FILE *fp;
> >char **FileBuff;
> >IndexAddress *AddStruct;
> >word LinesNumber=0;[/color]
>
> None of these objects belong here. So-called "global variables"
> should be avoided wherever practical. They can lead to bad things. Put
> all of these object definitions inside main().
>[color=green]
> >int main(int argc, char* argv[])[/color]
>
> You don't seem to be using main()'s parameters, so leave them out.
> There are two legal formats for main(). They are the one you have here
> and this one:
>
> int main(void)[color=green]
> >{
> > char FileName[]={"c:\\ariel\\flash196\\modbus.hex"};
> > char line[MAX_LINE];[/color]
>
> MAX_LINE does not exist. Perchance, is it defined in the header you
> haven't shown us?
>[color=green]
> > long BufSize=0;
> > word i,LineLen=0;
> >
> > // Open for read (will fail if file "FileName" does not exist)[/color]
>
> Useless comments like this one should be avoided. Comments should
> make code clearer, explain some construct that is not obvious or easy
> to understand, describe the purpose of a small group of statements,
> etc. Comments that don't add to understanding just get in the way.
> They add to clutter, and are best left out.
> Also, C++ style comments (those beginning with //) are frowned upon
> in here. There are two main reasons. One reason is that they are not
> legal in C90, the standard to which most currently available compilers
> conform). Also, long comments tend to wrap when posted here. When C++
> style comments wrap, it makes it more difficult to copy, paste &
> compile your code.
>[color=green]
> > if( (fp = fopen( FileName, "r" )) == NULL )
> > printf( "The file %s was not opened\n",FileName );[/color]
>
> If the file fails to open, you output an error message. How
> thoughtful! But you still go on trying to read (below) from the file.
> This results in undefined behaviour. Extremely bad! Quit or otherwise
> handle the error so that you never attempt to read from an unopened
> file. (But by all means do still output an error message.)
> By the way, diagnostic output usually goes to stderr. That's what
> it's there for, after all.
>[color=green]
> > else
> > printf( "The file '%s was opened\n",FileName );
> >
> > // Finds Number of lines in the Hex File[/color]
>
> This is a slightly better comment than the one above. At least this
> one explains the intent of the next few lines of code. Good!
>[color=green]
> > while (!feof(fp))[/color]
>
> The FAQ list of this newsgroup explains why this is wrong. Go now -
> yes, right now - and read the FAQ (at this location:
>
http://www.eskimo.com/~scs/C-faq/top.html). Please don't come back
> until you have read it.
>[color=green]
> > {
> > fgets(line,MAX_LINE,fp);
> > LinesNumber++;
> > }
> > printf("\nLinesNumber=%d\n",LinesNumber);
> >
> > // Allocate an array of pointers to number of lines
> > if((FileBuff = (char **)malloc(LinesNumber * sizeof(char *)))==NULL)[/color]
>
> Don't cast the return value of malloc(). It is never required, if
> you have properly declared malloc() by including stdlib.h, and can
> hide a bug if you haven't. See questions 7.6 and 7.7 in the FAQ for
> clues to the proper way to use malloc().
>[color=green]
> > {
> > printf("malloc error!\n");
> > return -1;[/color]
>
> Non-portable, non-standard return value. You can only portably
> return 0, EXIT_SUCCESS or EXIT_FAILURE from main(), the latter two
> being macros defined in stdlib.h.
>[color=green]
> > }
> >
> > rewind(fp);[/color]
>
> It would be a good idea to make sure this call succeeded. Check the
> return value.
>[color=green]
> > // Read line by line from file, allocate a memory to it and copy line
> >to allocation
> > for(i=0; i<LinesNumber; i++)
> > {
> > fgets(line,MAX_LINE,fp);
> > if((FileBuff[i] = (char *)malloc(strlen(line) *
> >sizeof(char)))==NULL)[/color]
>
> See above. Also see question 7.8 of the FAQ.
> And here we see a possible cause of your problem. You are not
> allocating enough memory here. Remember, a string needs a terminating
> '\0'. You must make room for it. strlen() returns the number of
> characters in the string *not* including the '\0'. So you need to
> allocate strlen(line) + 1 bytes here.
>[color=green]
> > {
> > printf("malloc error!\n");
> > return -1;[/color]
>
> Don't mix tabs and spaces in your code. We don't all set tabstops
> the same as you. And so what looks right in your editor looks out of
> whack to many of us. Be consistent with your indentation. Either only
> use tabs or only use spaces, and use the same number of tabs and
> spaces for each successive level of indentation. Many people here,
> myself included, prefer spaces to tabs.
>[color=green]
> > }
> > strcpy(FileBuff[i],line);
> > }
> >
> >
> > // Allocate an array of pointers to number of lines
> > AddStruct = (IndexAddress*)malloc(LinesNumber *
> >sizeof(IndexAddress));
> > if(AddStruct ==NULL)
> > {
> > printf("malloc error!\n");
> > return -1;
> > }
> >
> > // Prepare for sorting
> > for(i=0; i<LinesNumber; i++)
> > {
> > AddStruct[i].FlashAddress=Address(i);
> > AddStruct[i].index=i;
> > }
> > PrintAddIndex();[/color]
>
> No such function or macro.
>[color=green]
> > QuickSort(AddStruct,0,LinesNumber);[/color]
>
> No such function or macro.
>[color=green]
> > PrintAddIndex();
> >
> >
> > for(i=0; i<LinesNumber; i++)
> > free(FileBuff[i]); <====== IN THIS POINT THE PROGRAM WAS
> >THROWN.[/color]
>
> Don't put non-C text in C code. It makes it a pain to copy, paste &
> compile.
>[color=green]
> > free(FileBuff);
> >
> > /* Close fp */
> > if( fclose( fp ) )
> > printf( "The file %s was not closed\n",FileName );
> >
> > return 0;
> >}[/color]
>
> All in all that was quite ugly code. Ugly code is illegible code.
> Illegible code is incomprehensible code. Incomprehensible code is
> unmaintainable code. This is something you need to work on. And you
> need to leave Pascalisms (such as byte and word) in Pascal Land.
>
> --
>
> Dig the even newer still, yet more improved, sig!
>
>
http://alphalink.com.au/~phaywood/
> "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
> I know it's not "technically correct" English; but since when was rock & roll "technically correct"?[/color]