Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 03-05-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 15 April 2015, 10:26 AM EDT
Location: USA
Posts: 1,556
Thanks: 25
Thanked 150 Times in 142 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 Unix and Linux 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 Unix and Linux 03-05-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 May 2015, 3:20 AM EDT
Location: San Jose, CA, USA
Posts: 6,394
Thanks: 268
Thanked 2,142 Times in 1,832 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 Unix and Linux 03-05-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 28 May 2015, 4:54 PM EDT
Location: NM
Posts: 10,454
Thanks: 337
Thanked 862 Times in 800 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 Unix and Linux 03-05-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 29 May 2015, 11:54 AM EDT
Location: Saskatchewan
Posts: 20,688
Thanks: 898
Thanked 3,668 Times in 3,431 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 12:13 PM.