dynamic memory allocation on demand

This is a discussion on dynamic memory allocation on demand within the C Programming forums, part of the General Programming Boards category; hi in my program's input is a string, but i actually do not know, what its size will be ( ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    dynamic memory allocation on demand

    hi

    in my program's input is a string, but i actually do not know, what its size will be (
    so i need somehow change the strings size on demand, i.e. my goal is let user type as many chars as he wants and save them all into 1 string

    cheers

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Sort of a side questions, architecturally speaking, under *nix or Windows, what could be the longest string passed as an argument? And of the "should not use" gets(), what about it?

    (Sorry to hijack, but perhaps (hopefully) still on topic.

    Todd

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You can use realloc() to continuously resize the buffer and allocate more memory for your char buffer.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    *oilrg:
    You could make a very large (512, 1024 maybe?) char array for the user's entry, then add the '\0', to elevate the char's to a single char string. The sizeof() function should be perfect to give you the actual size in bytes for your perfectly sized array.

    Be sure to delete the large initial array, when you no longer need it, before leaving the function that creates it.

    You'll find the tutorial section helpful. If you'll post up some code, and ask specific questions, then we can be more helpful, without needing to write a post which needs to be like a text book.

    *Todd:
    When you pass a string as an argument, and it's a large string, you would normally pass it as a pointer only, not the entire string. That way, size is not a problem.

    The gets() function passes whatever is entered into it, regardless of it having exceeded the maximum limit of any kind. That can lead to huge problems, which can effect the stability of the system. The fgets() is the safe way to get input from a user, because you have to specify beforehand, what the size of the input, (maximum), will be.

    Check the tutorial for all the details.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Todd Burch View Post
    Sort of a side questions, architecturally speaking, under *nix or Windows, what could be the longest string passed as an argument?
    I believe that depends on the function. You have to read the documentation to find out any restrictions, but on Windows a lot of the file related API functions have a limit of MAX_PATH.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    the code is smth like that:

    input is 0..9 & a..b (not checks others)
    counts the base of the 'number' (max 36 - 'z'), and writes down that 'number' is in decimal

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    int main()
    
    {
    	char s[256];
    	int base=0,basemax=0,i;
    	long decimalone=0;
    	scanf("&#37;s",&s);
    
    	for (i=0;i<strlen(s);i++)
    		{
    
    		if (s[i]>=97)
    			base=s[i]-86;
    		else
    			base=s[i]-47;
    
    		if (base>basemax)
    		basemax=base;
    
    		}
    				for (i=0;i<strlen(s);i++)
    { if (s[i]>=97)
    decimalone+=(s[i]-87)*pow(basemax,(strlen(s)-1-i));
    else decimalone+=(s[i]-48)*pow(basemax,(strlen(s)-1-i));
    }
    
    
    		printf("\n\n%d",basemax);
    
    		printf("\n\n%d",decimalone);
    		return 0;
    }
    Last edited by oilrg; 12-02-2007 at 02:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. dynamic memory allocation w/o new
    By bd43274 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 01:12 AM
  5. dynamic memory allocation - Please help
    By Space_Cowboy in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2002, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21