Thread: malloc problem

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    malloc problem

    This is supposed to be a basic programme to see how malloc works, it compiles and runs giving the correct output but then I get "DEBUG error, heap corruption detected" and I don't understand what I've done wrong.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char *p;
    
    	p = (char *)malloc(sizeof(char));
    
    	if (p==0)
    
    	{
    		printf("\nERROR. Not enough memory\n");
    		return 1;
    	}
    
    	else{
    
    	printf("\nEnter your six digit library card no. \n");
    	scanf("%s", p);
    	printf("value = %s\n", p);
    
    	free(p);
    
    	}
    
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You're only allocating enough memory for a single character. You need to malloc enough space to hold the number of characters you are expecting plus one for the null terminator character.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You only allocated 1 character with malloc() and then tried to stuff 6 characters into it with scanf().

    Also, there's no reason to typecast the return value of malloc ... p = malloc( ); is good enough.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're allocating a single byte (that's what sizeof(char) means). Thus the only string your pointer can point to is a single 0, or the empty string. But you're asking the user to enter sig digits. A string of 6 digits would require at least 7 bytes (6 for the digits, 1 for the null character). So you end up writing past the end of your buffer.

    You can do something like:
    Code:
    p = malloc(50 * sizeof *p);
    Now you have 50 bytes, which can still be overflowed, but at least you've got a fighting chance of working.

    The multiplication by sizeof *p is not necessary, since it's 1 by definition, but some consider it good style anyway, the idea being that you can change the type of “p” and still get the allocation right. I don't subscribe to this notion, because char* is almost always used for strings, which coerce you into using char*; even if you switch to wchar_t, you'd have to change all sorts of other things as well, not just the declaration of “p”.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    first mistake, sizeof(char) is 1

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    thanks all, I think I understand a bit better how it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc and array problem
    By yacek in forum C Programming
    Replies: 15
    Last Post: 12-09-2010, 12:04 AM
  2. Problem with malloc(), maybe extern??
    By Dimes in forum C Programming
    Replies: 10
    Last Post: 12-08-2010, 07:53 AM
  3. malloc problem
    By abc_killer in forum C Programming
    Replies: 9
    Last Post: 11-15-2010, 02:12 PM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM