Help with malloc()


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with malloc()
# 1  
Old 03-05-2013
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
# 2  
Old 03-05-2013
Quote:
Originally Posted by eracav
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...
This User Gave Thanks to shamrock For This Post:
# 3  
Old 03-05-2013
Oh. Thank you very much.
# 4  
Old 03-05-2013
Quote:
Originally Posted by shamrock
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?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 03-05-2013
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.
This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 03-05-2013
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 *).
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 03-06-2013
Thank you very much to all of you.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

malloc vs new speed

Which one is faster among malloc and new? My understanding is that since new also has to call constructors after allocating memory it must be slower than malloc. Am I correct? (1 Reply)
Discussion started by: rupeshkp728
1 Replies

2. Programming

help with malloc [solved]

Hi i found code in google how to malloc an 2D array and i tried that : #include<stdio.h> #include<stdlib.h> int **A; int **B; int main(int argc,char *argv) { printf("name of text : %s\n",argv); //read arrays int i,j; int l,m; int M,n; FILE *fp; fp=fopen(argv,"r"); ... (0 Replies)
Discussion started by: giampoul
0 Replies

3. UNIX for Dummies Questions & Answers

Kmalloc and malloc

Do kmalloc and malloc allocate from same heap ? (3 Replies)
Discussion started by: dragonpoint
3 Replies

4. Programming

malloc vs realloc

Why when using realloc, john is reversed 3 times but not the other 2 names ? But if I use malloc, then the 3 names are reversed correctly ? (but then there is a memory leak) How can I reverse all 3 names without a memory leak ? char *BUFFER = NULL; char *STRREVERSE(const char *STRING) {... (5 Replies)
Discussion started by: cyler
5 Replies

5. UNIX for Advanced & Expert Users

Malloc Implementation in C

Hey Guys Some of my friends have got together and we are trying to write a basic kernel similar to Linux. I am trying to implement the malloc function in C and I am using a doubly linked list as the primary data structure. I need to allocate memory for this link list (duh...) and I don't feel... (2 Replies)
Discussion started by: rbansal2
2 Replies

6. Programming

Malloc implementation in C

Hey Guys I am trying to implement the malloc function for my OS class and I am having a little trouble with it. I would be really grateful if I could get some hints on this problem. So I am using a doubly-linked list as my data structure and I have to allocate memory for it (duh...). The... (1 Reply)
Discussion started by: Gambit_b
1 Replies

7. Programming

malloc()

Some one please explain me what is Dynamic memory allocation and the use of malloc() function.How do we allocate memory dynamically and also the other way? (3 Replies)
Discussion started by: rash123
3 Replies

8. Programming

When to use Malloc?

Hi! I hope this is the correct forum to post the question even if I'm a newbie... I am a C-newbie (and really on the edge to be a C-addict ;) ) and have a question. When should I use malloc? To state it differently, when should I NOT use malloc? For instance, if I have an array of... (5 Replies)
Discussion started by: Tonje
5 Replies

9. Programming

malloc

hello sir since by mentioning a integer pointer and storing the integers by incrementing the pointer value then what is the purpose of malloc? u can decalre it as in t *p; several integers can be stored by incrementing the value of p, hence what is the diffrence between this... (2 Replies)
Discussion started by: rajashekaran
2 Replies

10. Programming

a problem about malloc()

1 . Thanks everyone who read the post. 2 . the programe is that : #include <stdio.h> #include <string.h> void do_it(char *p) { p = (char *) malloc(100); (void )strcpy(p,"1234"); } int main(void) { char *p; do_it(p); (void )printf("p = %s \n",p); (1 Reply)
Discussion started by: chenhao_no1
1 Replies
Login or Register to Ask a Question