Thread: type cast problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    type cast problem

    I wrote a little pointer program and I just have an error that I don't get.

    this line
    Code:
    numSize = ceil( (float) (max - min) / interval ) + 1;
    is supposed to make numSize 6 when:
    Code:
    int min = 0
    int max = 10
    int interval = 2
    But I get numSize = 1026

    seems to work fine like this:
    Code:
    numSize = ((max - min) / interval ) + 1;
    the reason I need the ceil in there is because if
    Code:
    int min = 0
    int max = 10
    int interval = 3
    I need 5 and not four.

    I am using visual C++ to compile and debug (I tried cygwin but vista does not ply nice).
    Is the problem specific to VC++?
    Last edited by tikelele; 10-31-2007 at 02:07 PM. Reason: spelling

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Are you sure you're printing the value of numSize properly, and not doing something like using the wrong printf() type specifier? Also, ceil() takes a double argument, so you might want to cast to double instead of float (not that it matters here).

    Edit: Another reason for using (double) instead of (float) is that the maximum integer that can be represented exactly by a float is fairly small, since a 32-bit IEEE 754 float only has 6 or 7 decimal digits of precision.
    Last edited by robatino; 10-31-2007 at 02:13 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    No here is what I am doing.

    Code:
    {
    	int max;
    	int min;
    	int interval;
    	int numSize;
    	int i;
    	int *num;
    	int *numSquare;
    
    	printf( "Enter a minimum value: ");
    	scanf( " %d", &min);
    	printf( "Enter a maximum value: ");
    	scanf( " %d", &max);
    	printf( "Enter an interval: ");
    	scanf( " %d", &interval);
    	
    	
    	numSize =  ceil( (float) (max - min) / interval )  + 1;
    	
    	num = malloc(numSize * 2);
    	numSquare = malloc(numSize * 2);
    
    	for (i = 0; i < numSize; i++)
    	{
    		num[i] = min + (i * interval);
    		numSquare[i] = num[i] * num[i];
    		printf( "  %3d squared  =  %5d\n", num[i], numSquare[i]);
    	}
    
    	free (num);
    	free (numSquare);
    	num = NULL;
    	numSquare = NULL;
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    1) What makes you think numSize is equal to 1026 with your specified input? The expression looks okay to me. I notice you don't have any code to print the value of numSize, though.

    2) Whatever you're doing in allocating space for num and numSquare is horribly wrong. For one thing, since they're arrays of ints, you should have a sizeof(int) factor inside malloc() (or sizeof(*num) and sizeof(*numSquare) if you want). What exactly are you trying to do?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Quote Originally Posted by robatino View Post
    1) What makes you think numSize is equal to 1026 with your specified input? The expression looks okay to me. I notice you don't have any code to print the value of numSize, though.
    The output is way too many lines. If I use min 0 max 10 and interval 2 i should get 6 lines. when i use the ceil(....) I get hundreds. Also when I step into the debugger numSize has a value of 1026 at that line. might just be VC++

    Quote Originally Posted by robatino View Post
    2) Whatever you're doing in allocating space for num and numSquare is horribly wrong. For one thing, since they're arrays of ints, you should have a sizeof(int) factor inside malloc() (or sizeof(*num) and sizeof(*numSquare) if you want). What exactly are you trying to do?
    I am asking for a minimum number, a maximum number, and an interval (all integers). I need to Allocate two arrays big enough to hold the values defined by those numbers, safely.

    Then i need to Initialize one array to have the values defined by those numbers (min, min+interval, min+2*interval, etc.) and the other array to have the square of those values.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Try just putting a printf() statement right after the assignment to numSize to see what it is. I'm pretty sure that right after you assign it, the value is what it should be. Also, if you want to allocate space for an array of N ints, the proper syntax is malloc(N*sizeof(int)) (you need the sizeof() there since malloc() takes the size in bytes and you need to tell it how many bytes are in each int, which is what sizeof(int) is).

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Thank you for the malloc() information.

    I added the printf to see what was being placed in numSize is after assignment and it is indeed 1026. I am going to reboot to see if that helps.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I doubt if rebooting will help. Could you post the _entire_ piece of code you are compiling? There are a number of things that could be causing the problem with numSize that could be from code you're not posting.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Code:
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int max;
    	int min;
    	int interval;
    	int numSize;
    	int i;
    	int *num;
    	int *numSquare;
    
    	printf( "Enter a minimum value: ");
    	scanf( " %d", &min);
    	printf( "Enter a maximum value: ");
    	scanf( " %d", &max);
    	printf( "Enter an interval: ");
    	scanf( " %d", &interval);
    	
    	//numSize = ((max - min) / interval ) + 1; //this works
    	numSize =  ceil( (float) (max - min) / interval )  + 1; //this doesnt
    	//printf( " %d", numSize); //use this to show the value of numSize
    	
    	num = malloc(numSize * sizeof(int));	
    	numSquare = malloc(numSize * sizeof(int));	
    
    	for (i = 0; i < numSize; i++)
    	{
    		num[i] = min + (i * interval);
    		numSquare[i] = num[i] * num[i];
    		printf( "  %3d squared  =  %5d\n", num[i], numSquare[i]);
    	}
    
    	free (num);
    	free (numSquare);
    	num = NULL;
    	numSquare = NULL;
    
    	return 0;
    }
    That's it unfortunately

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You really don't need all of that memory. You could get by with a for loop body like this:
    Code:
    		num = min + (i * interval);
    		printf( "  &#37;3d squared  =  %5d\n", num, num * num);
    Code:
    	//numSize = ((max - min) / interval ) + 1; //this works
    	numSize =  ceil( (float) (max - min) / interval )  + 1; //this doesnt
    Do you have the header file math.h included? If not, the compiler might not be aware of what ceil returns and assume it returns an int and mess up royally.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Quote Originally Posted by dwks View Post
    You really don't need all of that memory. You could get by with a for loop body like this:
    Code:
    		num = min + (i * interval);
    		printf( "  %3d squared  =  %5d\n", num, num * num);
    Thank you. I understand.

    Quote Originally Posted by dwks View Post
    Code:
    	//numSize = ((max - min) / interval ) + 1; //this works
    	numSize =  ceil( (float) (max - min) / interval )  + 1; //this doesnt
    Do you have the header file math.h included? If not, the compiler might not be aware of what ceil returns and assume it returns an int and mess up royally.
    That did it. I did not include it. Thank you greatly. rookie error (still wearing tightie whities - hope to be in boxers soon!) thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM