Dynamically Allocated Memory vs. Statically allocated Memory

C

csnerd

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!
 
V

Victor Bazarov

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
 
R

red floyd

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.
 
T

Thomas Matthews

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
 
V

Victor Bazarov

Thomas said:
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
 
M

Matt Wharton

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top