Sponsored Content
Full Discussion: Help with malloc()
Top Forums UNIX for Dummies Questions & Answers Help with malloc() Post 302775751 by jim mcnamara on Tuesday 5th of March 2013 11:04:47 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. UNIX for Dummies Questions & Answers

Kmalloc and malloc

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

9. 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

10. 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
bsdmalloc(3MALLOC)														bsdmalloc(3MALLOC)

NAME
bsdmalloc - memory allocator SYNOPSIS
cc [ flag ... ] file ... -lbsdmalloc [ library ... ] char *malloc(size); unsigned size; int free( ptr); char *ptr; char *realloc( ptr, size); char *ptr; unsigned size; These routines provide a general-purpose memory allocation package. They maintain a table of free blocks for efficient allocation and coa- lescing of free storage. When there is no suitable space already free, the allocation routines call sbrk(2) to get more memory from the system. Each of the allocation routines returns a pointer to space suitably aligned for storage of any type of object. Each returns a null pointer if the request cannot be completed. The malloc() function returns a pointer to a block of at least size bytes, which is appropriately aligned. The free() function releases a previously allocated block. Its argument is a pointer to a block previously allocated by malloc() or real- loc(). The free() function does not set errno. The realloc() function changes the size of the block pointed to by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If the new size of the block requires movement of the block, the space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the block are unspecified. If ptr is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer, the space pointed to is freed. The malloc() and realloc() functions return a null pointer if there is not enough available memory. They return a non-null pointer if size is 0. These pointers should not be dereferenced. When realloc() returns NULL, the block pointed to by ptr is left intact. Always cast the value returned by malloc() and realloc(). If malloc() or realloc() returns unsuccessfully, errno will be set to indicate the following: ENOMEM size bytes of memory cannot be allocated because it exceeds the physical limits of the system. EAGAIN There is not enough memory available at this point in time to allocate size bytes of memory; but the application could try again later. Using realloc() with a block freed before the most recent call to malloc() or realloc() results in an error. Comparative features of the various allocation libraries can be found in the umem_alloc(3MALLOC) manual page. brk(2), malloc(3C), malloc(3MALLOC), mapmalloc(3MALLOC), umem_alloc(3MALLOC) WARNINGS
Use of libbsdmalloc renders an application non-SCD compliant. The libbsdmalloc routines are incompatible with the memory allocation routines in the standard C-library (libc): malloc(3C), alloca(3C), calloc(3C), free(3C), memalign(3C), realloc(3C), and valloc(3C). 21 Mar 2005 bsdmalloc(3MALLOC)
All times are GMT -4. The time now is 03:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy