Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dynamically Allocated Memory vs. Statically allocated Memory

Reply
Thread Tools

Dynamically Allocated Memory vs. Statically allocated Memory

 
 
csnerd@gmail.com
Guest
Posts: n/a
 
      12-08-2004
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!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-08-2004
<(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      12-08-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      12-08-2004
(E-Mail Removed) 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-08-2004
Thomas Matthews wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
Matt Wharton
Guest
Posts: n/a
 
      12-09-2004
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mix statically typed with dynamically typed Yingjie Lan Python 4 01-29-2010 08:50 AM
what is fast dynamically linked executable or statically linked executable ?how to decide? pratap C Programming 20 03-07-2007 04:46 AM
is Java dynamically or statically typed language or both? puzzlecracker Java 9 12-30-2005 08:26 PM
is C++ dynamically or statically typed language or both? puzzlecracker C++ 4 12-25-2005 09:57 AM
statically allocated data structures source? Thomas Paul Diffenbach C Programming 4 07-12-2003 04:57 AM



Advertisments