Thread: Declaring a variable size char

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    Declaring a variable size char

    Hello, I have a question maybe more conceptual than practical, about the char variables. Suppose the next scenario

    Code:
    //compiled as *c with MingW on DevCpp IDE
    #include <conio.h>
    #include <stdio.h>
    void test(char *txt,int i,int f)
    {
    int q;
    char bff[f-i+1];
    for(q=0;q<(f-i);q++)
        {
        bff[q]=txt[q+i];
        }
    bff[q]='\0';
    printf("%s\n",bff);
    }
    
    int main()
    {
    test("That's a test.",0,6);
    test("That's a test.",7,8);
    test("That's a test.",9,13);
    getch();
    return 0;
    }
    It works well, or for that sample situation it works well. My question is about the 2nd line on the 'test()' function: the char name[size], will do a 'malloc' each function call (or something internal like a mem allocation)? If yes, how can I know when there's enought memory (like the 'malloc' that returns null value)? Should I have to test if the buffer created is null? (I'm afraid that it will crash the program directly).

    Thank's in advance
    Niara

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It will not malloc(). It will just crash if you exceed stack size. There's no way you can catch it. Stack overflow usually means instant death for the application, on pretty much any modern system.

    Of course, you have to pass a very long string before that actually happens.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Niara
    Hello, I have a question maybe more conceptual than practical, about the char variables. Suppose the next scenario

    Code:
    //compiled as *c with MingW on DevCpp IDE
    #include <conio.h>
    #include <stdio.h>
    void test(char *txt,int i,int f)
    {
    int q;
    char bff[f-i+1];
    for(q=0;q<(f-i);q++)
        {
        bff[q]=txt[q+i];
        }
    bff[q]='\0';
    printf("%s\n",bff);
    }
    
    int main()
    {
    test("That's a test.",0,6);
    test("That's a test.",7,8);
    test("That's a test.",9,13);
    getch();
    return 0;
    }
    It works well, or for that sample situation it works well. My question is about the 2nd line on the 'test()' function: the char name[size], will do a 'malloc' each function call (or something internal like a mem allocation)? If yes, how can I know when there's enought memory (like the 'malloc' that returns null value)? Should I have to test if the buffer created is null? (I'm afraid that it will crash the program directly).

    Thank's in advance
    Niara

    it will malloc, in a sense that the OS will try and allocate memory block for your array... you can't check it by yourself... unless if you are talking about dynamic data structure

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Also, variable length arrays are a new feature in C99, but have been available as an extension for several compilers for some time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello, thanks all for the responses.

    Then is safer to use a malloc(variable_size) instead char [variable_size] because I will be able to check for the memory disponibility, but with little strings that won't be any problem, and that kind of memory allocation 'instruction' will be supported by most of the actual compilers. I allways use the 'malloc' function, but that was a 'Is possible to...?' question that comes to my mind from I don't know where

    Re - Thank's all for your time and help
    Niara

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by CornedBee
    It will not malloc(). It will just crash if you exceed stack size. There's no way you can catch it. Stack overflow usually means instant death for the application, on pretty much any modern system.

    Of course, you have to pass a very long string before that actually happens.
    Then you just don't exceed the buffer size
    If you don't need the support for very long strings, it is ok to just check for buffer overflow.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Niara
    Hello, thanks all for the responses.

    Then is safer to use a malloc(variable_size) instead char [variable_size] because I will be able to check for the memory disponibility, but with little strings that won't be any problem, and that kind of memory allocation 'instruction' will be supported by most of the actual compilers. I allways use the 'malloc' function, but that was a 'Is possible to...?' question that comes to my mind from I don't know where

    Re - Thank's all for your time and help
    Niara
    yes you can do that
    Code:
    char *s;
    
    if((s = (char *) malloc(size_you_need)) == NULL) {
       fprintf(STDERR, "not enough memory\n");
       exit(0);
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Gah - casting malloc in C
    There's a FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Salem
    Gah - casting malloc in C
    There's a FAQ
    lol... just what I expected

    ok, here's the calloc version
    Code:
    char *s;
    
    s = calloc(array_size, size_of_var_type); // in your case : calloc(10, 1)
    
    return 0;

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hey, thanks for the new aclarations.

    I forgot to explain something: I use malloc like
    Code:
    char *cbff=(char*)malloc(sizeof(char)*10);
    int ibff=(int*)malloc(sizeof(int)*10);
    As I read the calloc's faq, I think that's the same because calloc willl determine the size of the variable type that I want to use (maybe the types sizes are different depending the o.s.), so in that last code if I declare a 'cbbf' (a char buffer) with a size 10, so the malloc will get mem for 10 times the size of a char in the o.s. where is executed, no? the same with the 'int' type will get mem for an array of 10 times the size of an int in the o.s. Maybe I'm misunderstanding that about mem managing, althought I always find it interesting and important.

    Niara

    P.S. Have you seen that in the editor on the quick reply when you mouseover/mouseout on the text formatting buttons the working area is expanded? I'm using nsn7.0.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The only difference between calloc and malloc is that calloc 0-initializes the entire memory it allocates.

    sizeof(char) is guaranteed by the standard to be 1. That's the definition of what sizeof means.

    You do not need to cast the return value of malloc/calloc in C. In fact, it can hide errors. (I forgot the example.)

    You should always check the return value of malloc. It might return NULL at any time.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Thank's for the aclarations, I'll bear in mind.

    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  3. pass error before char
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-26-2006, 12:00 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM