Thread: What does malloc() do?

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by moi
    did i miss something
    A few of your C lessons maybe ?

    Marking moi's answers:
    0. <stdlib.h>
    Correct

    1. Not that argument again . Of course, it's not nessecary, but some here prefer it, its a stylistic thing.
    Good enough. Like you, I don't want to see that argument again!

    2. At most 50, but possibly a few or maybe very many less.
    3. At most 499, but possibly a few or maybe very many less.
    Not accurate enough. Try again

    4. Which example code?
    Forget it, I was only highlighting the fact it was very misleading code, but the other questions kinda did that anyway, or at least they will when someone answers them properly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by moi
    did i miss something
    Yeah. You missed the word keyword "sizeof".

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Hammer
    A few of your C lessons maybe ?
    2. At most 50, but possibly a few or maybe very many less.
    3. At most 499, but possibly a few or maybe very many less.
    Not accurate enough. Try again

    baaah! my answer is right!!! not accurate enuf for you, meh.

    to be more precise, 100 / sizeof (int) in the first and 999 / sizeof (int) in the second. the smallest possible sizeof (int) yields the max possible number of ints you can hold, 50 in the first and 499 in the second. so saying "at most 50" and "at most 499" is correct.
    hello, internet!

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>100 / sizeof (int) in the first and 999 / sizeof (int) in the second.
    >>so saying "at most 50" and "at most 499" is correct.
    No, and no.

    Look again at the code:

    >>ptr = (int*)malloc (sizeof (100));
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #20
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Here's a hint moi, 100 is an int, so sizeof(100) is the same as sizeof(int).

  6. #21
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Hammer
    >>100 / sizeof (int) in the first and 999 / sizeof (int) in the second.
    >>so saying "at most 50" and "at most 499" is correct.
    No, and no.

    Look again at the code:

    >>ptr = (int*)malloc (sizeof (100));
    jesus christ i feel soo dumb right now! of course i know that, i've just never seen a mistake that stupid before. yes of course, because 100 is an intiger literal, sizeof (100) holds 1 int, as does sizeof (999).
    hello, internet!

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by moi
    jesus christ i feel soo dumb right now! of course i know that, i've just never seen a mistake that stupid before. yes of course, because 100 is an intiger literal, sizeof (100) holds 1 int, as does sizeof (999).
    Sorry you had to fall for that one moi
    Your final response leads me nicely onto the answer to question 4:
    >- What's wrong with this example code?
    And the answer is: It's very misleading and makes it all too easy for the programmer to miss a nasty bug. It is obviously not the way to teach a newbie to code.

    Here's a working sample of code, for those that are still interested:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        const char *Original = "This is a string";
        char *p;
        size_t len;
        
        len = strlen(Original) + 1;
        
        p = malloc(len * sizeof(*p));
        
        if (p != NULL)
        {
            strcpy(p, Original);
            printf("p: %s\n", p);
            free(p);
        }
        
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #23
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    Do i get it?

    Ok, i just tried to write a program using malloc() that takes input and makes for sizeof_array elements then inputs them then prints them. When i try to run it i get an error. Whats wrong with my program?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void DieWithError(char *ptr2error);
    int main(void) {
    
    
    	int sizeof_array,num,i;
    	int *ptr,*original_ptr;
    
    	printf("Please input sizeof_array:");
    	scanf("%d",&sizeof_array);
    
    	if ((ptr=(int*)malloc(sizeof_array*sizeof(int)))==NULL)
    	DieWithError("Error on malloc");
    
    	original_ptr=ptr;
    
    	for (i=0; i<sizeof_array; i++){
    		printf("Input number %d:",i);
    		scanf("%d",&num);
    		*ptr=num;
    		ptr++;
    	}
    
    	ptr=original_ptr;
    
    	for (i=0; i<sizeof_array; i++) {
    		printf("Element %d=%d\n",i,*ptr);
    		ptr++;
    	}
    
    	getchar();
    	return 0;
    }
    
    void DieWithError(char *ptr2error) {
    	printf("%s",ptr2error);
    	getchar();
    	exit(1);
    }

  9. #24
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>When i try to run it i get an error.
    And that error would be...?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #25
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by moi
    0. <stdlib.h>
    1. Not that argument again . Of course, it's not nessecary, but some here prefer it, its a stylistic thing.
    2. At most 50, but possibly a few or maybe very many less.
    3. At most 499, but possibly a few or maybe very many less.
    4. Which example code?
    oh no ... i'm sorry man ! i forget for the <stdlib.h>

    actually the sizeof malloc of value doesn't matter, if you replace it with 1, if the user enter the data more than size of 1, the size will be append (enlarge the size of the malloc automatically, there's no problem right here)

  11. #26
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Re: Do i get it?

    well.... manwhoonlyeats, your source code is in error, but i will try to correct it.

    Code:
    	if ((ptr=(int*)malloc(sizeof_array*sizeof(int)))==NULL)
    // look messy...
    	DieWithError("Error on malloc");
    try to fix it with ....
    Code:
    ..
    ptr=(int*)malloc(sizeof(int));
    if (!ptr)
    {
        printf ("error...");
        exit (100);
    }
    else ...... // the malloc process was done successfully
    ..
    another error....
    Code:
    	original_ptr=ptr; // no need for it
    
    	for (i=0; i<.....; i++){
    		printf("Input number %d:",i);
    		scanf("%d",&num);
    		*ptr=num;
    		ptr++;
    	} // no need for this, but the easy way...
    correction
    Code:
    for (count = 0 ; *(ptr+count) != NULL ; count++)
    {
    printf("Input number %d:",count+1);
    scanf("%d", &( *(ptr+count) ) );
    }
    i need some info, why use this ?
    Code:
    	DieWithError("Error on malloc");
    another error on the printf situation, have a look ..
    Code:
    for (count = 0 ; *(ptr+count) != NULL ; count++)
         printf("Element %d=%d\n",count ,*(ptr+count) );
    // end of printing element
    
    free (ptr); // free the ptr mem
    i might knew that my correction might be wrong, i didn't check in computer. however, their guy should help me to correct my error. good luck ...

    -- beely

  12. #27
    fou
    Guest
    Originally posted by beely
    actually the sizeof malloc of value doesn't matter, if you replace it with 1, if the user enter the data more than size of 1, the size will be append (enlarge the size of the malloc automatically, there's no problem right here)
    Is this some kind of tacky joke, or do you actually believe this?
    manwhoonlyeats - your code looks fine. Please post the error details.

  13. #28
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    actually the sizeof malloc of value doesn't matter, if you replace it with 1, if the user enter the data more than size of 1, the size will be append (enlarge the size of the malloc automatically, there's no problem right here)
    Yea, and when you write past the end of what you have you'll probably write over the housekeeping stuff that free needs so that when you try to free it your program will crash.
    Code:
    #include <stdlib.h>
    
    main(){
        int *p=malloc(1);
    
        *p=10;
    
        free(p);
    }

  14. #29
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by beely
    oh no ... i'm sorry man ! i forget for the <stdlib.h>

    actually the sizeof malloc of value doesn't matter, if you replace it with 1, if the user enter the data more than size of 1, the size will be append (enlarge the size of the malloc automatically, there's no problem right here)
    Do NOT post messages like that. If you believe its true read a good C book before posting again. If it's a joke, don't bother trying to be funny, 'cos you're confusing the newer people.

    [edit]And your corrections to manwhoonlyeats's code are also wrong. Read this thread and understand!

    @manwhoonlyeats: Post your errors.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #30
    Registered User
    Join Date
    Dec 2002
    Posts
    32
    My program would compile fine, but then it would run i would input the first element in the area and it would crash. When i clicked on debug it brought up the line: *ptr=num;
    but then it stopped giving me the error so i must have changed something without knowing . Oh well, it works fine now!
    I dont understand what beely is talking about. Thanks for all your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM