CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2003
    Location
    India
    Posts
    24

    Doubt in malloc()

    Hi,

    I have a doubt while specifying the size in malloc().

    int main()
    {
    char *str;
    str = (char *)malloc(sizeof(char) * 256);
    scanf("%s", str);
    }

    the above program will work fine, only if the inputed strings length is less than 256. Is there anything available, which can calculate the size dynamically?

    so that irrespective of the length of the inputed string, my program should work fine.

    regards,
    Ashok

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    If programming in C++, you can use std::string instead
    of a char array. Stroustrup gives a solution using C-style
    coding at (it is somewhat messy) :

    http://www.research.att.com/~bs/new_learning.pdf

  3. #3
    Join Date
    Oct 2003
    Location
    India
    Posts
    24
    Thanks Philip,

    I have already worked with std::string, but just wanted to know how can I do it in C. and its clear now.

    Thank you very much for the quick response..

    regards,
    Ashok

  4. #4
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    I would say the topic should be doubt in input methods rather than malloc.

    Another way to deal with unexpected length of input is
    reading it using 'fgets' with a length limitation
    (it can be used for reading from 'stdin' too),
    using 'realloc' as necessary and/or using 'sscanf' to parse the buffer.

    Regards,
    Guy
    **** **** **** **** **/**

  5. #5
    Join Date
    Jun 2004
    Posts
    7
    Another way to deal with unexpected length of input is
    reading it using 'fgets' with a length limitation
    (it can be used for reading from 'stdin' too),
    using 'realloc' as necessary and/or using 'sscanf' to parse the buffer.
    Excuse me for the question, but could you give an example, please ???

  6. #6
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    Here is an example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define EXTENTSIZE 5
    
    int main()
    {
      int bufferSize = EXTENTSIZE;
      int numExtents = 0;
      char* buffer = (char*)malloc(bufferSize);
    
      //
      // fgets reads its second parameter's value -1 characters, or until it
      // reaches a newline, which come first.
      // since buffer is been expanded when fgets neither read a newline nor end-of-file, 
      // we must read each cycle of the loop -
      // into the starting location of the last extention which is:
      // buffer+(EXTENTSIZE-1)*numExtents
      //
    
      while(fgets(buffer+(EXTENTSIZE-1)*numExtents++,EXTENTSIZE,stdin)!=NULL)
      {
         // if fgets read a newline then replace it with null character
         if(buffer[strlen(buffer)-1]=='\n')
         {
            buffer[strlen(buffer)-1]='\0';
            break;
         }
    
         // expand buffer with EXTENTSIZE bytes:
         buffer=(char*)realloc(buffer,(bufferSize+= EXTENTSIZE));
      }
    
      puts(buffer);
      free(buffer);
      return 0;
    }
    **** **** **** **** **/**

  7. #7
    Join Date
    May 2004
    Posts
    340

    Re: Doubt in malloc()

    Originally posted by MAshokKumar
    Hi,

    I have a doubt while specifying the size in malloc().

    int main()
    {
    char *str;
    str = (char *)malloc(sizeof(char) * 256);
    scanf("%s", str);
    }

    the above program will work fine, only if the inputed strings length is less than 256. Is there anything available, which can calculate the size dynamically?

    so that irrespective of the length of the inputed string, my program should work fine.

    regards,
    Ashok

    u can get it in this way:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    char *str,
    i;
    scanf("%d",&i);
    printf("%d\n",i);
    str = (char *)malloc(sizeof(char) *i);
    scanf("%s", str);
    printf("%s\n",str);
    return 0;

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured