Dynamically Allocated Memory vs. Statically allocated Memory

Discussion in 'C++' started by csnerd@gmail.com, Dec 8, 2004.

  1. Guest

    I have a really simple question.
    What is the difference between allocating memory following way:

    #1
    int main(int argc,char* argv)
    {
    char str[strlen(argv[1])+1];
    return 0;
    }

    vs.

    #2
    int main(int argc,char* argv)
    {
    char* str=malloc(strlen(argv[1])+1);
    return 0;
    }

    Both code compiles fine on a Linux machine, but code #2 fails with
    visual C++ compiler.

    Is the code #1 not safe??? or there is not a huge difference between
    the code and it is just a compiler issue? ( I understand that in code
    #2 the memory is located on heap as oppose to the code #1)
    Any help, regarding this would be greatly appreciated!
     
    , Dec 8, 2004
    #1
    1. Advertising

  2. <> wrote...
    >I have a really simple question.
    > What is the difference between allocating memory following way:
    >
    > #1
    > int main(int argc,char* argv)
    > {
    > char str[strlen(argv[1])+1];
    > return 0;
    > }
    >
    > vs.
    >
    > #2
    > int main(int argc,char* argv)
    > {
    > char* str=malloc(strlen(argv[1])+1);
    > return 0;
    > }
    >
    > Both code compiles fine on a Linux machine, but code #2 fails with
    > visual C++ compiler.


    I actually expect the #1 to fail. It is not a valid C++ program. Array
    size has to be compile-time constant.

    > Is the code #1 not safe???


    Code #1 is not valid.

    > or there is not a huge difference between
    > the code and it is just a compiler issue? ( I understand that in code
    > #2 the memory is located on heap as oppose to the code #1)
    > Any help, regarding this would be greatly appreciated!


    See above.

    V
     
    Victor Bazarov, Dec 8, 2004
    #2
    1. Advertising

  3. red floyd Guest

    wrote:
    > I have a really simple question.
    > What is the difference between allocating memory following way:
    >
    > #1
    > int main(int argc,char* argv)
    > {
    > char str[strlen(argv[1])+1];
    > return 0;
    > }
    >
    > vs.
    >
    > #2
    > int main(int argc,char* argv)
    > {
    > char* str=malloc(strlen(argv[1])+1);
    > return 0;
    > }
    >


    Both pieces of code should fail. The footprint of main is

    int main(int argc, char *argv[])
    ^^

    You are declaring argv as a pointer, not as an array of pointers.
     
    red floyd, Dec 8, 2004
    #3
  4. wrote:
    > I have a really simple question.
    > What is the difference between allocating memory following way:
    >
    > #1
    > int main(int argc,char* argv)
    > {
    > char str[strlen(argv[1])+1];
    > return 0;
    > }
    >
    > vs.
    >
    > #2
    > int main(int argc,char* argv)
    > {
    > char* str=malloc(strlen(argv[1])+1);
    > return 0;
    > }


    Another alternative:
    int main(int argc, char * * argv)
    {
    char * str = new char [strlen(argv[1]) + 1];
    return 0;
    }





    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Dec 8, 2004
    #4
  5. Thomas Matthews wrote:
    > wrote:
    >
    >> I have a really simple question.
    >> What is the difference between allocating memory following way:
    >>
    >> #1
    >> int main(int argc,char* argv)
    >> {
    >> char str[strlen(argv[1])+1];
    >> return 0;
    >> }
    >>
    >> vs.
    >>
    >> #2
    >> int main(int argc,char* argv)
    >> {
    >> char* str=malloc(strlen(argv[1])+1);
    >> return 0;
    >> }

    >
    >
    > Another alternative:
    > int main(int argc, char * * argv)
    > {
    > char * str = new char [strlen(argv[1]) + 1];
    > return 0;
    > }


    Speaking of alternatives, I'd recommend

    #include <string>
    int main(int argc, char **argv)
    {
    if (argc > 1) {
    std::string str(argv[1]);
    // whatever else
    }
    return 0
    }

    otherwise if the program is run without additional arguments, argv[1]
    is NULL and strlen(NULL) has undefined behaviour.

    V
     
    Victor Bazarov, Dec 8, 2004
    #5
  6. Matt Wharton Guest

    <> wrote in message
    news:...
    >I have a really simple question.
    > What is the difference between allocating memory following way:
    >
    > #1
    > int main(int argc,char* argv)
    > {
    > char str[strlen(argv[1])+1];
    > return 0;
    > }
    >
    > vs.
    >
    > #2
    > int main(int argc,char* argv)
    > {
    > char* str=malloc(strlen(argv[1])+1);
    > return 0;
    > }
    >
    > Both code compiles fine on a Linux machine, but code #2 fails with
    > visual C++ compiler.
    >
    > Is the code #1 not safe??? or there is not a huge difference between
    > the code and it is just a compiler issue? ( I understand that in code
    > #2 the memory is located on heap as oppose to the code #1)
    > Any help, regarding this would be greatly appreciated!
    >


    As others have mentioned, #1 is actually not valid code (you need to have a
    constant size when statically declaring/allocating an array).

    When you say #2 'fails', do you mean fails to compile or fails at runtime?
    I assume the former. I tried compiling under VC6 and got a compile error
    about strlen being unable to convert from char to const char *. Remember
    that argv is actually a char**, not a char*. Also, malloc returns a void*,
    you need to cast that to a char* for your assignment to 'str' to work (i.e.
    compile).

    -Matt
     
    Matt Wharton, Dec 9, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. puzzlecracker
    Replies:
    9
    Views:
    1,665
    puzzlecracker
    Dec 30, 2005
  2. Thomas Paul Diffenbach

    statically allocated data structures source?

    Thomas Paul Diffenbach, Jul 11, 2003, in forum: C Programming
    Replies:
    4
    Views:
    447
    David Rubin
    Jul 12, 2003
  3. puzzlecracker
    Replies:
    4
    Views:
    436
    Luke Meyers
    Dec 25, 2005
  4. pratap
    Replies:
    20
    Views:
    774
    Flash Gordon
    Mar 7, 2007
  5. Yingjie Lan
    Replies:
    4
    Views:
    319
    John Nagle
    Jan 29, 2010
Loading...

Share This Page