Thread: malloc problem

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    malloc problem

    Hi,

    Just for fun (I have been out of C for some weeks) I coded some programs. In one of them I used this:

    Code:
    int decs[512];
    I decided to replace it by:

    Code:
    int *decs = (int *) malloc(NUMBER_OF_INTS_NEEDED * sizeof(int));
    I use this in a program to convert roman numbers to decimal numbers. When I use the second line, XLV = 45 (that's ok) but MCI = 1076 and MM = 1983 ?? It's late here, so it could be a stupid mistake.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Ideswa View Post
    Hi,

    Just for fun (I have been out of C for some weeks) I coded some programs. In one of them I used this:

    Code:
    int decs[512];
    I decided to replace it by:

    Code:
    int *decs = (int *) malloc(NUMBER_OF_INTS_NEEDED * sizeof(int));
    It's highly unlikely that your problem is caused by this change. Is NUMBER_OF_INTS_NEEDED at least 512? It has to be going wrong elsewhere. You may have a latent bug that was exposed when you switched to using a different region of memory for the decs array.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe NUMBER_OF_INTS_NEEDED has the wrong value. If you get the correct values for MCI and MM when using an array (int decs[512]), then that could be the problem.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    NUMBER_OF_INTS_NEEDED is the number of roman numbers (MM = 2 ints needed) + 1 for a terminating zero to indicate the end of the array.

    When I set NUMBER_OF_INTS_NEEDED = 512, all numbers are correctly calculated.

    When I set NUMBER_OF_INTS_NEEDED = 4 (3 chars + 1 zero) the program produces invalid output using MCI as input and valid output using XLV as input. It's not a segmentation error. Even when I input MMMMMMM (NUMBER_OF_INTS_NEEDED = 4) it doesn't segfault.

    I use the int array like a reverted enum:

    Code:
    ptr = rn; // rn: ( char rn[512] = MCI; for example)
    	while(*ptr != '\0')
    	{
    		if(*ptr == 'M')
    			decs[l] = 1000;
    		else if(*ptr == 'D')
    			decs[l] = 500;
    		else if(*ptr == 'C')
    			decs[l] = 100;
    		else if(*ptr == 'L')
    			decs[l] = 50;
    		else if(*ptr == 'X')
    			decs[l] = 10;
    		else if(*ptr == 'V')
    			decs[l] = 5;
    		else if(*ptr == 'I')
    			decs[l] = 1;
    		++l;
    		*ptr++;
    	}
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Like brewbuck said, you probably have a bug elsewhere in your code. By the way:
    > *ptr++;

    ptr++;
    would work just fine here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  4. freeing problem with concurrent processes
    By alavardi in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 01:09 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM