create variable size arrays

This is a discussion on create variable size arrays within the C Programming forums, part of the General Programming Boards category; I got this code: Code: len = trap_FS_FOpenFile( filename, &f, FS_READ ); if( len <= 0 ) { return va( ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    40

    create variable size arrays

    I got this code:
    Code:
    	len = trap_FS_FOpenFile( filename, &f, FS_READ );
    	if( len <= 0 ) {
    		return va( S_COLOR_RED "file not found: %s\n", filename );
    	}
    	char filetext[len];
    len is an integer, and it's value after trap_Fs_fopenfile, is the size of the file he is opening, so before actually reading the file, i want to create a character array that will be big enough to get the entire file.
    so I'm thinking: char filetext[len];
    But I'm getting errors:
    error C2065: 'filetext' : undeclared identifier => at a line a bit futher where I pass filetext as a parameter
    error C2143: syntax error : missing ';' before 'type' => at the line char filetext[len]; (and no ; is missing for the record)
    Anyone know how I can get this to work?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    First, call malloc to dynamically allocate a chunk of memory:
    Code:
    char * filetext = malloc(len);
    if( filetext != NULL )
    {
        /* Everything is good */
    }
    else
    {
        /* Call to malloc failed */
    }
    When you're done with the array, then free it:
    Code:
    free(filetext);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    va( S_COLOR_RED "file not found: &#37;s\n", filename );
    This doesn't look valid. I don't know how is S_COLOR_RED defined (nor function va) but except if it's as (char *) (which, between you and me, would be totally useless/stupid), well the argument isn't valid, there's missing a ',' between S_COLOR_RED and the string.

    Also, you can't declared variable like this (filetext), you can do it only after an opening curling brace, else it's not valid C.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    40
    okay, but in the end the function returns filetext, so I can not use free(filetext); before the end of the function, and obviously not after the end of the function either, will it be automatically cleared if the function ends, or should I still free it manually?

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    40
    the va thing works perfectly
    anyway hk_mp5kpdw's code did not do the job, I'm still getting the same errors.

    I could just do: char filetext[100000];
    and then it works, but if the file were to be bigger it wouldn't so that's why I'd like to make an array that always will have enough room for the file.

    edit: i got it working with this:
    Code:
    	static char *filetext;
    
    	len = trap_FS_FOpenFile( filename, &f, FS_READ );
    	if( len <= 0 ) {
    		return va( S_COLOR_RED "file not found: &#37;s\n", filename );
    	}
    	
    	filetext = malloc(len);
    it appears i had to declare it before using any function, but I could only know the size after I exectued the first function, so like this it works. I'm running some testcase to check if it's stable.
    Last edited by s-men; 08-27-2007 at 06:57 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    Quote Originally Posted by s-men View Post
    it appears i had to declare it before using any function, but I could only know the size after I exectued the first function, so like this it works. I'm running some testcase to check if it's stable.
    I believe that it is a rule of C that all variable declarations must occur before any other statements. C++ is different and you can have a bunch of statements and then in the middle of your function you can declare another new variable. So yes, in your case you would need to declare the variable first before you do things like calling the function to determine the size, you would then be able to call the malloc function like you using it above once you had the size.

    Quote Originally Posted by s-men
    okay, but in the end the function returns filetext, so I can not use free(filetext); before the end of the function, and obviously not after the end of the function either, will it be automatically cleared if the function ends, or should I still free it manually?
    You are talking about scope. Everything depends on where you declare your variables. Let's say you declare the filetext variable in a function and this function ends. Memory allocated by malloc does not disappear until you explicitly free it with a call to the free function (or the program ends and the operating system recovers it). The filetext variable itself however does get popped off the stack and if we do not preserve that variable's value somehow, then we lose our handle to the memory that was allocated. We can do this in several ways.

    One way would be to have the function accept an argument that gets set to the value of the call to malloc. It is then the calling functions job to declare the pointer variable and pass it to this function that does perform the call to malloc and late free the memory:
    Code:
    void called_func(char ** filetext)
    {
        /* Determine the size needed */
    
        /* Allocate the memory */
        *filetext = malloc(size);
    }
    
    void calling_func()
    {
        char * filetext;
    
        /* Call the function to determine the size and allocate the memory. */
        called_func(&filetext);
    
        /* Do something with the memory pointed to by filetext */
    
        /* Free the memory */
        free(filetext);
    
    }
    You can also have the function return a pointer to the allocated memory location:
    Code:
    char * called_func()
    {
        /* Determine the size needed */
    
        /* Allocate the memory */
        return malloc(size);
    }
    
    void calling_func()
    {
        char * filetext;
    
        /* Call the function to determine the size and allocate the memory. */
        filetext = called_func();
    
        /* Do something with the memory pointed to by filetext */
    
        /* Free the memory */
        free(filetext);
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    40
    I declared the filetext variable outside any function. Now I got a function ReadFile which opens the file, allocates filetext, read file into filetext, closes the file and returns filetext.
    Now whenever I call ReadFile, I can just let it be followed by free(filetext);.
    That should work right? If the free(filetext) worked is hard to say now, but I think if this runs on a server for weeks and weeks and it keeps on not freeing the space, it'll cause problems then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 02:04 AM
  5. way around variable array size probs?
    By crag2804 in forum C Programming
    Replies: 3
    Last Post: 09-10-2002, 05:48 PM

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