Help with malloc() | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Help with malloc()

UNIX for Dummies Questions & Answers


Tags
fgets, malloc

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-05-2013
eracav eracav is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 6 March 2013, 9:14 PM EST
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Help with malloc()

Good day! I'm a newbie in C. I'm trying to get an unlimited input from the user using malloc then printing the inputs after the user presses enter. My code works, but there's a warning that I don't know how to fix. Please help me. Thank you.

Here's my code:

Code:
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {

    char* pointer = malloc(sizeof(char*));
    fgets(pointer, malloc(sizeof(char*)), stdin);
    puts(pointer);


    return 0;

}

I get the warning: passing of argument 2 of 'fgets' makes integer from pointer without a cast

What does that mean? Thank you very much
Sponsored Links
    #2  
Old 03-05-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 18 September 2014, 2:18 PM EDT
Location: USA
Posts: 1,463
Thanks: 19
Thanked 141 Times in 133 Posts
Quote:
Originally Posted by eracav View Post
Good day! I'm a newbie in C. I'm trying to get an unlimited input from the user using malloc then printing the inputs after the user presses enter. My code works, but there's a warning that I don't know how to fix. Please help me. Thank you.

Here's my code:

Code:
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {

    char* pointer = malloc(sizeof(char*));
    fgets(pointer, malloc(sizeof(char*)), stdin);
    puts(pointer);


    return 0;

}

I get the warning: passing of argument 2 of 'fgets' makes integer from pointer without a cast

What does that mean? Thank you very much
Because the second argument of fgets is an integer while malloc returns pointer to void so take it out...
The Following User Says Thank You to shamrock For This Useful Post:
eracav (03-05-2013)
Sponsored Links
    #3  
Old 03-05-2013
eracav eracav is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 6 March 2013, 9:14 PM EST
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Oh. Thank you very much.
    #4  
Old 03-05-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 September 2014, 5:41 PM EDT
Location: San Jose, CA, USA
Posts: 4,659
Thanks: 179
Thanked 1,559 Times in 1,326 Posts
Quote:
Originally Posted by shamrock View Post
Because the second argument of fgets is an integer while malloc returns pointer to void so take it out...
The problem is much deeper than that.

The given program allocates enough space to hold a pointer to a character; not enough space to read a line of user input. And, I don't see how eracav can preallocate space for "unlimited input from the user". In addition to that, eracav isn't checking the return value of malloc() to determine if space was allocated nor of fgets() to determine if an error or EOF has been encountered before a <newline> was found.

To do what needs to be done, eracav will need much more complicated logic with a loop calling fgets() and realloc() to increase the input buffer size until a <newline> or EOF has been copied into the growing buffer.

Is this a homework assignment?
The Following User Says Thank You to Don Cragun For This Useful Post:
eracav (03-06-2013)
Sponsored Links
    #5  
Old 03-05-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 18 September 2014, 3:22 PM EDT
Location: NM
Posts: 10,211
Thanks: 278
Thanked 796 Times in 743 Posts

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {

    char* pointer = malloc(sizeof(char*) * 4096);
    if(pointer==NULL)
    {
        perror("Cannot allocate memory");
        exit(1);
    }
    fgets(pointer, 4096, stdin);
    puts(pointer);
    puts("\n");   // in case the user enters more than 4095 chars and you lose the trailing \n
    free(pointer);
    return 0;
}

You have to set an arbitrary limit, I chose 4096.
Best practice if you malloc:
1. check the return code from the malloc call, and handle any errors.
2. free any memory you do not need anymore.

And. You code did not work, it had undefined behavior. A successful compile in C means no warnings, no errors, no dereference of undefined or possibly NULL pointers.
The Following User Says Thank You to jim mcnamara For This Useful Post:
eracav (03-06-2013)
Sponsored Links
    #6  
Old 03-05-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 18 September 2014, 5:28 PM EDT
Location: Saskatchewan
Posts: 19,372
Thanks: 788
Thanked 3,266 Times in 3,061 Posts
char * is 'pointer to characters', you don't want to allocate pointers, you want to allocate characters. So, sizeof(char) would make more sense than sizeof(char *).
The Following User Says Thank You to Corona688 For This Useful Post:
eracav (03-06-2013)
Sponsored Links
    #7  
Old 03-06-2013
eracav eracav is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 6 March 2013, 9:14 PM EST
Posts: 4
Thanks: 5
Thanked 0 Times in 0 Posts
Thank you very much to all of you.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Malloc Implementation in C rbansal2 UNIX for Advanced & Expert Users 2 02-27-2010 08:05 PM
malloc() rash123 Programming 3 08-28-2008 11:35 AM
When to use Malloc? Tonje Programming 5 10-17-2005 10:14 AM
malloc function rajashekaran Programming 7 09-30-2002 01:27 PM
malloc rajashekaran Programming 2 09-30-2002 05:24 AM



All times are GMT -4. The time now is 05:44 PM.