Thread: calloc

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    if i swich the two lines of the code
    Code:
    return 0;
    	free(makeAnArray);
    it seems to work. The question that I have is if I am using the right array makeAnArray or is it supposed to be anArray or array or even return?
    which one would free the correct array

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int foo() { return 5; }
    int main()
    {
        int myint = foo();
        printf("%i", myint);
        return 0;
    }
    Quote Originally Posted by goran00 View Post
    if i swich the two lines of the code
    Code:
    return 0;
    	free(makeAnArray);
    it seems to work. The question that I have is if I am using the right array makeAnArray or is it supposed to be anArray or array or even return?
    which one would free the correct array
    Think about what you're doing! C is linear!
    You end the function before the call to free is made!
    But once again, you are trying to free the address of a function! This is illegal and cannot be done, which we have told you several times before!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Returning from the function BEFORE you free the Array doesn't really make much sense. It might "work", but it doesn't do all that you want it to.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double* makeAnArray(double size);
    
    int main (void)
    {
    	double *anArray;
    	char throwaway;
    	int size = 0;
    	int k;
    	
    	printf("Please enter the size of the array: ");
    	fflush(stdout);
    	scanf("&#37;d", &size);
    	scanf("%c", &throwaway);
    
    	anArray = makeAnArray(size);
    
    	for (k = 0; k < size; k++)
    	{
    		printf("%f  ", anArray[k]);
    	}
    	printf("\n");
    	fflush(stdout);
    	
    	free(anArray);
    
    	getchar();
    	
    	return 0;
    }
    double* makeAnArray(double size)
    {
    	double* array;
    	
    	array = calloc(size,sizeof(double));
    	
    	return array;
    }
    And here's the output:
    Code:
    Please enter the size of the array: 10
    0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000
    I'm begging you, look at the red highlights HARD. These are the things you need to study.

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    what is foo?????

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A function. What does it look like?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thanks for help
    just a quick question
    fflush(stdout);
    what is this? what does it do?

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    what is it used for? when do you use it? what is the purpose of it???

  8. #23
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by goran00 View Post
    thanks for help
    just a quick question
    fflush(stdout);
    what is this? what does it do?
    It flushes the output buffer. My internal output console sometimes doesn't display my printfs immediately when I want them to. So I call fflush(stdout) to make sure they're output to the screen.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It flushes the output buffer. It forces all the data inside the output buffer to be directed to the screen.
    This may or may not be necessary, but it doesn't hurt.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Mar 2008
    Posts
    1
    你为什么用double类型的形参来接收int类型的实参的值呢?
    You why use double type virtual parameter take-over int type fact parameter?

    makeAnArray(double size)

  11. #26
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by Elysia View Post
    It flushes the output buffer. It forces all the data inside the output buffer to be directed to the screen.
    This may or may not be necessary, but it doesn't hurt.
    printf("\n"); always flushes the output buffer

  12. #27
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by KIBO View Post
    printf("\n"); always flushes the output buffer
    Sure does. But sometimes you don't want a new line.

  13. #28
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by KIBO View Post
    printf("\n"); always flushes the output buffer
    No. It only flushes the buffer if the stream is configured to line buffering mode. If it's fully buffered, there is no guarantee of a flush.

    Granted, when output is going to the screen, the stream is preconfigured into line buffer mode for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM