473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Dynamically Allocated Memory vs. Statically allocated Memory

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!

Jul 22 '05 #1
5 2349
<cs****@gmail.com> 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
Jul 22 '05 #2
cs****@gmail.com 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.

Jul 22 '05 #3
cs****@gmail.com 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

Jul 22 '05 #4
Thomas Matthews wrote:
cs****@gmail.com 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
Jul 22 '05 #5
<cs****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro 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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: kk | last post by:
Hi all, i didn't get output in the following code while compiling and executing with g++ version 3.2.3 it doesn't allocate memory to pointer varaible (x) in class B. and it gives correct output...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
3
by: Justin To via AccessMonster.com | last post by:
I just distributed a FE mde file throught my department, and while stress testing on the performance of the new release, 3 users got the following error: The instruction at ... referenced memory...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
0
by: menkaur | last post by:
Is there eny way to get the memory size allocated with new (like GlobalSize for GlobalAlloc) ?
4
by: Julián Sanz García | last post by:
Hi!!, How can i use Ram memory or virtual memory to crear a file??? The idea is to create a file in a temporaly space, something like RAM memory or virtual memory. Thanks
3
by: Sakharam Phapale | last post by:
Hi All, How to get the no of memory bytes allocated to the Array of type String? Dim arrDetail(100,10) As String Thanks and Regards Sakharam Phapale
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
2
by: nguyenlh | last post by:
I'm studying about Memory managerment - stack memory and I can't understand the following problems : Who can help me >> - Role of Stack memory -How to store address in stack memory -Garbage...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.