free function error.

This is a discussion on free function error. within the C Programming forums, part of the General Programming Boards category; I try to use free() 4 times but i have some errors here....can anyone help? The first two are ok! ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    free function error.

    I try to use free() 4 times but i have some errors here....can anyone help? The first two are ok! i think the problem is on the last two...

    Code:
    	free (plaintext);
    		free (ch);
    		free (ciphertext);
    		free (newplaintext);
    Code:
    cs091770@cs091770-Aspire-6930G:~/Desktop$ ./a.out 
    Please type k (length): 33
    Please type the text for coding: as"this is the text to be coded"asg
    
    this is the text to be coded
    
    	Encoding in progress...
    
    t h i s   i 
    s   t h e   
    t e x t   t 
    o   b e   c 
    o d e d   
          
    
    Encoding completed:
    tstooh e ditxbeshted e  i tc
    
    	Decoding in progress...
    
    t s t o o  
    h   e   d  
    i t x b e  
    s h t e d  
      e       
    i   t c   
    
    Decoded completed:
    this is the text to be coded
    *** glibc detected *** ./a.out: free(): invalid next size (fast): 0x08f4c0b0 ***
    ======= Backtrace: =========
    /lib/libc.so.6(+0x6c501)[0xb7624501]
    /lib/libc.so.6(+0x6dd70)[0xb7625d70]
    /lib/libc.so.6(cfree+0x6d)[0xb7628e5d]
    ./a.out[0x8048a9c]
    /lib/libc.so.6(__libc_start_main+0xe7)[0xb75cece7]
    ./a.out[0x80484f1]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:17 3015133    /home/cs091770/Desktop/a.out
    08049000-0804a000 r--p 00000000 08:17 3015133    /home/cs091770/Desktop/a.out
    0804a000-0804b000 rw-p 00001000 08:17 3015133    /home/cs091770/Desktop/a.out
    08f4c000-08f6d000 rw-p 00000000 00:00 0          [heap]
    b7400000-b7421000 rw-p 00000000 00:00 0 
    b7421000-b7500000 ---p 00000000 00:00 0 
    b7587000-b75a1000 r-xp 00000000 08:17 7864399    /lib/libgcc_s.so.1
    b75a1000-b75a2000 r--p 00019000 08:17 7864399    /lib/libgcc_s.so.1
    b75a2000-b75a3000 rw-p 0001a000 08:17 7864399    /lib/libgcc_s.so.1
    b75b6000-b75b8000 rw-p 00000000 00:00 0 
    b75b8000-b770f000 r-xp 00000000 08:17 7866647    /lib/libc-2.12.1.so
    b770f000-b7710000 ---p 00157000 08:17 7866647    /lib/libc-2.12.1.so
    b7710000-b7712000 r--p 00157000 08:17 7866647    /lib/libc-2.12.1.so
    b7712000-b7713000 rw-p 00159000 08:17 7866647    /lib/libc-2.12.1.so
    b7713000-b7716000 rw-p 00000000 00:00 0 
    b7716000-b773a000 r-xp 00000000 08:17 7864575    /lib/libm-2.12.1.so
    b773a000-b773b000 r--p 00023000 08:17 7864575    /lib/libm-2.12.1.so
    b773b000-b773c000 rw-p 00024000 08:17 7864575    /lib/libm-2.12.1.so
    b774d000-b7751000 rw-p 00000000 00:00 0 
    b7751000-b7752000 r-xp 00000000 00:00 0          [vdso]
    b7752000-b776e000 r-xp 00000000 08:17 7864373    /lib/ld-2.12.1.so
    b776e000-b776f000 r--p 0001b000 08:17 7864373    /lib/ld-2.12.1.so
    b776f000-b7770000 rw-p 0001c000 08:17 7864373    /lib/ld-2.12.1.so
    bfe0d000-bfe2e000 rw-p 00000000 00:00 0          [stack]
    Aborted

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Where's the code!
    How do you allocate memory..?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    It is probable that the error lies in the code before the snippet that you posted, e.g., you incremented those pointers so they are different from what was returned by malloc, realloc or calloc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    mmm....
    first of all i can not post the code cause it is an exersice...

    1) i use only malloc.
    2) and i am sure that the allocation is right.

    laserlight, malloc returns char or not?

    this is an example of what i wrote:

    Code:
    char *A;
    A=(char *)malloc(n*sizeof(char));
    Last edited by brack; 10-28-2010 at 10:03 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Then we can't do anything to help you.
    Possible causes:
    You pass invalid pointer(not from malloc/calloc/realloc) to free.
    You overwrite the heap.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by brack
    malloc returns char or not?
    malloc returns a pointer to void.

    Quote Originally Posted by brack
    this is an example of what i wrote:

    char *A;
    A=(char *)malloc(n*sizeof(char));
    The problem probably lies between this and free. For example, this is a mistake:
    Code:
    size_t n = 100;
    char *p = malloc(n * sizeof(*p));
    ++p; /* assume that malloc did not return a null pointer */
    free(p); /* oops, p no longer points to what was returned by malloc! */
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    i think i got what you mean laserlight but i "play" with arrays...
    i can post this part...is it helpfull?

    Code:
    /* Allocating space for ch */
    		ch = (char **)malloc(size*sizeof(char *));
    			for(i=0; i<size; i++){
    				ch[i] = (char *)malloc(size*sizeof(char));
    			}
    			if(ch == NULL){
    				printf("Error in allocating ch' s space.\n");
    				exit(1);
    			}
    		
    		/* Allocating space for ciphertext */ 
    		ciphertext = (char *)malloc((size^2)*sizeof(char));
    			if(ciphertext == NULL){
    				printf("Error in allocating ciphertext space.\n");
    				exit(1);
    			}
    			
    		/* Allocating space for newplaintext */
    		newplaintext = (char *)malloc((size^2)*sizeof(char));
    			if(newplaintext == NULL){
    				printf("Error in allocating newplaintext space.\n");
    				exit(1);
    			}
    		
    	printf("\n\tEncoding in progress...\n");
    		
    		/* Stores the 1-dim plaintext in 2-dim ch */
    		store_text(plaintext, k, ch, size);
    		
    		/* Printing what 2-dim ch contains */
    		print_2darray(ch, size);
    		
    		/* Coding the text( 2-dim ch to 1-dim ciphertext ) */
    		get_text(ch, size, ciphertext); // ch is [size][size] where ciphertest is [sixe]^2
    		
    		/* Printing the coded 1-dim ciphertext */
    		printf("\n\nEncoding completed:");
    		print_text(ciphertext, size);
    	
    	printf ("\n\tDecoding in progress...\n");
    		
    		/* Store the coded text 1-dim ciphertext to 2-dim ch array */
    		store_text(ciphertext, size , ch, size);
    		
    		/* Prints the 2-dim ch array(coded text) */
    		print_2darray(ch, size);
    		
    		/* Decoding the 2-dim ch array and stores the values to 1-dim newplaintext(decoded) with n^2 size */
    		get_text(ch, size, newplaintext);
    
    		/*Prints the 1-dim newplaintext array(decoded) */
    		printf("\n\nDecoded completed:");
    		print_text(newplaintext, size);
    		
    		/* Free the allocatin spaces */	
    		free (plaintext);
    		free (ch);
    		free (ciphertext);
    		free (newplaintext);

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    This looks suspicious:
    Code:
    ciphertext = (char *)malloc((size^2)*sizeof(char));
    are you aware that ^ is the bitwise xor operator?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    probably not....can you explain it for me please? what you mean?

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    x^2 does not mean x power 2. Look up bitwise operator in C.
    You probably mean x * x.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    yes it is correct thanks guys....this solved the problem!
    So this operator is called bitwise (^)?

    Basically i have done some digital design with boole operators etc and i know almost what you mean...
    one last question, you advice me to use x*x when i want to power something ?
    Last edited by brack; 10-28-2010 at 10:36 AM.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by brack View Post
    yes it is correct thanks guys....this solved the problem!
    So this operator is called bitwise (^)?
    Not just bitwise operator, but rather (in particular) the bitwise xor (exclusive-or) operator as indicated by laserlight. Others can be found here.

    Quote Originally Posted by brack
    one last question, you advice me to use x*x when i want to power something ?
    In this particular example it's easy enough to duplicate "x to the power of 2" by simply using x * x in your code. There is also a function that can be used for this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by brack View Post
    yes it is correct thanks guys....this solved the problem!
    So this operator is called bitwise (^)?

    Basically i have done some digital design with boole operators etc and i know almost what you mean...
    one last question, you advice me to use x*x when i want to power something ?
    That operator is called bitwise XOR.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21