Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-23-2011, 04:16 PM   #1
Solid State Member
 
Join Date: Mar 2009
Posts: 6
Default Is malloc needed for dynamic array declaration using GCC?

I'm reading up on and learning about pointers in C and have read that, if you want to create an array that you'll only know the length of at runtime, the correct way to do this is using malloc:

Code:
int *iptr;
iptr = malloc(i * sizeof(int)); // i to be read in at runtime
so that you end up with a pointer to the start of the array of length i.



However, when mucking about and testing this out I found that the compiler (GCC) doesn't throw up any complaints when you use:

Code:
int i;

scanf("%d", &i);

int test[i];
Is this just a fluke of how GCC compiles it that it takes into account the dynamic memory allocation for you? Would other compilers throw up an error? Or is the second method actually allowed?
__________________

Matuku is offline   Reply With Quote
Old 03-29-2011, 08:22 PM   #2
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Is malloc needed for dynamic array declaration using GCC?

Needed, no. Preferred? Yes. By using malloc you can then later call "free" to clear the memory and open it for other uses making the system leaner overall.

The real difference here is what is returned by the system. By doing things like int test[i] you're just saying we have a bunch of items to put in a list and is not a true pointer to anything, just strait values. The malloc function returns the memory address of the location that will store element 0 (the first data point) of an array, string or whatever else it might be. By setting a pointer equal to the results of malloc, you're telling the system "Hey, start here when looking for data from this pointer".
__________________

__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Old 03-29-2011, 08:34 PM   #3
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Is malloc needed for dynamic array declaration using GCC?

This is one of the very reasons why I don't recommend starting with languages such as C - the manual memory management can get confusing rather quickly!

Quote:
Needed, no. Preferred? Yes. By using malloc you can then later call "free" to clear the memory and open it for other uses making the system leaner overall.
Actually, to guarantee the code works you do need malloc. Malloc allocates a chunk of memory and gives it to (in this case) the array, marking that memory as in use by the program. If you don't use malloc, you're using unallocated memory, which could potentially change, be yanked away or cause a segmentation fault at any point in the code without you doing a thing. In simple programs this is relatively easy to spot and fix, in larger ones it's hideously complicated and is the source of lots of hard to track down bugs in many programs.

As an aside, I'm sure a few years ago when a power line was pulled down by a storm, sparked and caught fire, the key to the safety system not cutting in was a misplaced malloc / free that fell foul to this very bug.

You can (and should) later call free to release the memory aquired by malloc, and this will prevent memory leaks. Calling malloc however is an absolute must - GCC won't throw up any warnings because in C reading and writing to random chunks of memory is a perfectly legal operation, just one that can cause a lot of bugs, pains and headaches later on if you don't know exactly what you're doing!

This is why generally speaking, people don't use C anymore unless there's a damn good reason
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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



All times are GMT -5. The time now is 06:12 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0