|
-
June 30th, 2004, 07:14 AM
#1
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
-
June 30th, 2004, 07:26 AM
#2
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
-
June 30th, 2004, 07:48 AM
#3
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
-
June 30th, 2004, 09:57 AM
#4
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
**** **** **** **** **/**
-
June 30th, 2004, 12:32 PM
#5
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 ???
-
June 30th, 2004, 03:03 PM
#6
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;
}
**** **** **** **** **/**
-
July 5th, 2004, 11:04 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|