Heap Corruption Detected -- Malloc issues

This is a discussion on Heap Corruption Detected -- Malloc issues within the C Programming forums, part of the General Programming Boards category; Hi I wrote a program that takes a string of words and reverses the order of the words: String : ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    Heap Corruption Detected -- Malloc issues

    Hi I wrote a program that takes a string of words and reverses the order of the words: String : two words ----> words two

    The program seems to do what I intended but crashes right after with a HEAP CORRUPTION DETECTED error.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void reverseWord(char *string, int size);
    
    int main(void){
    	int size;
    	char *string = "Two words";
    
    	size = (int)strlen(string);
    
    	reverseWord(string, size);
    
    	return 0;
    }
    void reverseWord(char *string, int size){
    	
    	char *buffer;
    	int currentPos, endWord, startWord, bufferWrite = 0;
    
    	buffer = (char *) malloc(size);
    
    	currentPos = size - 1;
    
    	while(currentPos >= 0){
    		if(string[currentPos] == ' '){
    			buffer[bufferWrite++] = string[currentPos];
    			currentPos--;
    		}
    		else if(string[currentPos] != ' '){
    			endWord = currentPos;
    			while(currentPos >= 0 && string[currentPos] != ' '){
    				currentPos--;
    			}
    			startWord = currentPos + 1;
    			while( startWord <= endWord ){
    				buffer[bufferWrite++] = string[startWord++];
    			}
    		}
    	}
    	buffer[bufferWrite] = '\0';
    	printf("%s\n", buffer);
    
    	free(buffer);	
    }
    Im certain it has something to do with malloc but I am not sure.
    What can I do to fix this? Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    You need strlen(s)+1 chars to store all the characters AND the \0 which follows.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by Salem View Post
    You need strlen(s)+1 chars to store all the characters AND the \0 which follows.
    woot! thanks Salem. Works fine without crashes now.

    I have a question though. When I do
    Code:
    	buffer = (char *) malloc(size+1);
    
    	printf("%d\n", strlen(buffer));
    for the string "two words", it returns 14. Shouldn't it return 10?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you bother to put anything in buffer before you call strlen? If there doesn't happen to be a string terminator in buffer by pure chance, then strlen will keep reading until it finds one. (The fact that it only goes four extra bytes makes you lucky indeed.)

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by tabstop View Post
    Did you bother to put anything in buffer before you call strlen? If there doesn't happen to be a string terminator in buffer by pure chance, then strlen will keep reading until it finds one. (The fact that it only goes four extra bytes makes you lucky indeed.)
    ah, I understand now. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-23-2010, 02:37 PM
  2. Heap corruption Detected Error
    By mrsirpoopsalot in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2009, 03:59 PM
  3. glibc detected malloc(): memory corruption
    By totalnewbie in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 05:21 AM
  4. Heap corruption detected. What does it mean?
    By franziss in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2008, 02:50 AM
  5. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM

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