473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Large arrays: malloc or 'just declare'?

Hello,

Can someone please help.

I need to use a large array and I'm not sure when one should
use (A) and when one should use (B) below:-

#define MAX 10000000

(A) int x[MAX];

(B) int *x = ( int * ) malloc( MAX*sizeof( int ) );

This isn't a compiler specific problem, but just for some background, I was
using (A) for lower values of MAX but when I started increasing it, I hit
some compiler/linker problems with the borland compiler.

I then noticed that the size of the executable was similar regardless of
whether
I used (A) or (B) which might have been a mild advantage of (B).

Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?

I think the answer relates to stack versus heap (but I'm interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven't inspired me.

Thanks in advance
Hal.
Nov 14 '05 #1
19 3460
i) First of all, if you want to go for very large arrays,( as you had
rightly put it b4 ) , go for the heap and avoid taking up stack space
for it.

ii) Second, the life time of the variable. Most of the time, you would
want to move around the data pointed by the very large array and operate
on them. In that case, again it makes sense to have it on the heap and
pass around the address of the same.
Yeah . you can even define a local variable on the stack and move
around the address, but needless to say that is lost once the function
returns, whereas if it taken from heap, it is still there ( But then
there is always a lurking danger of a memory leak by separating the
malloc / free in diff. functions . )
My vote goes for heap !!

Hal Styli wrote:
Hello,

Can someone please help.

I need to use a large array and I'm not sure when one should
use (A) and when one should use (B) below:-

#define MAX 10000000

(A) int x[MAX];

(B) int *x = ( int * ) malloc( MAX*sizeof( int ) );

Thanks in advance
Hal.


--

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Nov 14 '05 #2
Hal Styli <no_spam@all> spoke thus:
#define MAX 10000000 (A) int x[MAX]; (B) int *x = ( int * ) malloc( MAX*sizeof( int ) ); ^^^^^^^^^
Don't cast the return value of malloc, ever. If you've forgotten to
include <stdlib.h>, you won't know it if you cast malloc's return
value. Highly detailed discussions on this topic can be found in this
group's archives.
This isn't a compiler specific problem, but just for some background, I was
using (A) for lower values of MAX but when I started increasing it, I hit
some compiler/linker problems with the borland compiler.
If it isn't compiler specific, why mention Borland's compiler? The
amount of automatic storage you can allocate is a function of your
implementation and how you use it.
I then noticed that the size of the executable was similar regardless of
whether
I used (A) or (B) which might have been a mild advantage of (B).
Mostly irrelevent, unless you have the (mis)fortune of developing on a
highly space-sensitive platform, such as an embedded device.
Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?
Use A when:

1) You know how much space you want at compile time,
2) You won't want more space at any future time, and
3) You don't run into problems such as the one you're having.

Otherwise, use B. (Right, clc?) Remember that you must free() memory
you allocate with malloc().
I think the answer relates to stack versus heap (but I'm interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven't inspired me.


On this newsgroup, "stack" and "heap" are dangerous terms, as
implementations need not use such structures to manage the memory they
allocate. As for references, the following contain a wealth of
valuable C information:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
On Tue, 20 Apr 2004 01:45:53 +0000, Christopher Benson-Manica wrote:
Hal Styli <no_spam@all> spoke thus:
#define MAX 10000000
(A) int x[MAX];

(B) int *x = ( int * ) malloc( MAX*sizeof( int ) );

^^^^^^^^^
Don't cast the return value of malloc, ever. If you've forgotten to
include <stdlib.h>, you won't know it if you cast malloc's return
value. Highly detailed discussions on this topic can be found in this
group's archives.
I then noticed that the size of the executable was similar regardless of
whether
I used (A) or (B) which might have been a mild advantage of (B).


Mostly irrelevent, unless you have the (mis)fortune of developing on a
highly space-sensitive platform, such as an embedded device.


In which case you should know how much memory your program can use to the
nearest byte or word.
Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?
Use A when:

1) You know how much space you want at compile time,
2) You won't want more space at any future time, and
3) You don't run into problems such as the one you're having.

Otherwise, use B. (Right, clc?) Remember that you must free() memory
you allocate with malloc().


And always check to see if your call to malloc() succeeded; that is,
always test the pointer you got back against NULL.

I like to use constructions like the below:

#include <stdlib.h>

/* ... */

int *ptr;

if ((ptr = malloc(sizeof(i nt) * LEN)) == NULL) {
/* Handle failure to allocate RAM */
}
I think the answer relates to stack versus heap (but I'm interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven't inspired me.


On this newsgroup, "stack" and "heap" are dangerous terms, as
implementations need not use such structures to manage the memory they
allocate. As for references, the following contain a wealth of
valuable C information:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html


Good pointers. ;)

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #4


August Derleth wrote:
And always check to see if your call to malloc() succeeded; that is,
always test the pointer you got back against NULL.

I like to use constructions like the below:

#include <stdlib.h>

/* ... */

int *ptr;

if ((ptr = malloc(sizeof(i nt) * LEN)) == NULL) {
/* Handle failure to allocate RAM */
}


My two cents - Before you start writing your C++ code, write your
wrappers for all the routine things that you would do - mem. allocation
(like the one above), sockets, files etc. It would really help a lot
later in terms of debugging the code later.

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Nov 14 '05 #5
On Mon, 19 Apr 2004 20:06:37 -0700, Rakesh wrote:

My two cents - Before you start writing your C++ code, write your
wrappers for all the routine things that you would do - mem. allocation
(like the one above), sockets, files etc. It would really help a lot
later in terms of debugging the code later.


Who says we're going to write C++?

(I probably missed something in the original post. Sorry.)

[OT]
Using malloc() in C++ isn't good style, as I understand it.
[/OT]

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #6
Sorry for the confusion and bringing in C++ here.

I wanted to mention abt wrappers ( and that has nothing to do with, if u
do in C++ or not ).

August Derleth wrote:
On Mon, 19 Apr 2004 20:06:37 -0700, Rakesh wrote:
My two cents - Before you start writing your C++ code, write your
wrappers for all the routine things that you would do - mem. allocation
(like the one above), sockets, files etc. It would really help a lot
later in terms of debugging the code later.

Who says we're going to write C++?

(I probably missed something in the original post. Sorry.)

[OT]
Using malloc() in C++ isn't good style, as I understand it.
[/OT]


--

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Nov 14 '05 #7
"Hal Styli" <no_spam@all> wrote in news:40******** **@127.0.0.1:
I need to use a large array and I'm not sure when one should
use (A) and when one should use (B) below:-

#define MAX 10000000

(A) int x[MAX];

(B) int *x = ( int * ) malloc( MAX*sizeof( int ) ); [...] Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?

I think the answer relates to stack versus heap (but I'm interested in
any other answers that come up). Can anyone point me to an online
reference on stack vs heap, my google searches haven't inspired me.


If "int x[MAX];" is global or static, it will typically not be put in
either place. Usually it'll get added to the .bss section, which is stored
in your executable as just a size, then the OS will allocate that much
memory somewhere for you when your program starts. Completely system
dependent, but pretty common.

Otherwise, the only serious reason I can think of to prefer (B) is: what
happens when you don't have that much memory available? With (A), your
program dies in some way that you probably have no control over. With (B),
you can exit gracefully and let the user know why. (or, algorithm
permitting, fall back to a smaller size)

-josh
Nov 14 '05 #8
August Derleth <se*@sig.now> wrote in message news:<pa******* *************** ******@sig.now> ...
On Tue, 20 Apr 2004 01:45:53 +0000, Christopher Benson-Manica wrote:
Hal Styli <no_spam@all> spoke thus:
[...]
Mostly irrelevent, unless you have the (mis)fortune of developing on a
highly space-sensitive platform, such as an embedded device.
In which case you should know how much memory your program can use to the
nearest byte or word.

.... and you don't need to alloc() memory, because you know it's there and
it's all yours. :-)
Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?


Use A when:

1) You know how much space you want at compile time,
2) You won't want more space at any future time, and
3) You don't run into problems such as the one you're having.
4) You realy need it.

Otherwise, use B. (Right, clc?) Remember that you must free() memory
you allocate with malloc().


And always check to see if your call to malloc() succeeded; that is,
always test the pointer you got back against NULL.
[...]


That makes (A) a favorite even if you don't know the exact amount but
an upper limit. (and have enough so you mustn't be stingy)
I think the answer relates to stack versus heap (but I'm interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven't inspired me.


On this newsgroup, "stack" and "heap" are dangerous terms, as
implementations need not use such structures to manage the memory they
allocate. [...]


.... but for most of them you can assum that local variables end up
on the stack and global (and static) variables end up on the heap.
So your problem (stack overflow ?) may vanish, if you declare your
big array global. (If it doesn't bother you that that is regarded
bad style.)

bye,
heyo
Nov 14 '05 #9
On Tue, 20 Apr 2004, Hal Styli wrote:
Hello,

Can someone please help.

I need to use a large array and I'm not sure when one should
use (A) and when one should use (B) below:-

#define MAX 10000000

(A) int x[MAX];

(B) int *x = ( int * ) malloc( MAX*sizeof( int ) );
Just a side note. If you failed to have #include <stdlib.h> the compiler
will not have a prototype for malloc. This will lead to undefined
behaviour. The behaviour might be things work fine. On the other hand, it
migth crash your computer or format your hard drive. If you had:

int *x = malloc(MAX*size of(int));

and failed to #include <stdlib.h>, you should get a warning or error
message.
This isn't a compiler specific problem, but just for some background, I was
using (A) for lower values of MAX but when I started increasing it, I hit
some compiler/linker problems with the borland compiler.

I then noticed that the size of the executable was similar regardless of
whether
I used (A) or (B) which might have been a mild advantage of (B).

Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?

I think the answer relates to stack versus heap (but I'm interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven't inspired me.
The idea of stack vs heap assumes your compiler is using a stack and a
heap. If you are looking for a generic C answer then forget that.

Method A will only work if you know the size of the array at compile time.
If the size of the array is dependent on the user then you must use method
B. Since you indicate that you can use either method the decision is not
being force one way or the other.

If I use method A, I'll know at compile or load time whether the program
will work. It will also work or not work. With method B, I have to wait
until run time to see if it will work. But I have the option to recover.
If the call to malloc fails I can have a fall back plan. I could switch to
secondary memory or process an array of half the size twice. It I'm just
going to exit then it is no better than method A.

On the other hand, I realize that different compilers and platforms behave
differently. I might try both methods and measure the performance of each.
For my particular platform with a specific compiler I might find one
method is notably better than the other. I'd put a comment reminding
myself and anyone else who might inherit my code.

If my testing found that method B was no better than method A, I'd go with
method A because it is less work for me.
Thanks in advance
Hal.


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #10

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

Similar topics

2
6447
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as "*?<>/\|\\" If one of the chars are found, I'd like to replace it with a "-" and then commit the change to the actual file system. I know that a scripting language may be better suited for this, but I'm experimenting with C and I'm trying to...
6
2060
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
8
2244
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along that a declaration like: int intArray means that there is an array of pointers to int with a size of 5, each of whose elements is an array of int with a size of 3. This definition seemed intuitive to me since a declaration like
15
6988
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with automatic allocation of memory. In C, arrays are groups of variables of the same type in adjacent memory. Allocation for dynamic arrays is handled by the programmer. This is an 8 mark question in an old exam paper, so I am assuming there are
11
4455
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
7
2165
by: mef526 | last post by:
I have had this problem for months now and it has been nagging me. I have a large project that has several C++ DLL's, one of them uses malloc / calloc / free for the other DLL's. I process very large datafiles (100MB to 300 MB) that have more than 524,000 lines with 5 columns of numbers in text. I allocate 8 arrays with 524,000 or more doubles , and 10 arrays of 32,768 doubles. Is there a min size for malloc / calloc required to...
39
19618
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
29
35433
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how multi-dimensional arrays work, b) how to call a function with a multi-dimensional array, c) how to return a multi-dimensional array from a function, or d) how to read and write arrays from a disc file. Note to C++ programmers: You should be using...
6
1402
by: raphfrk | last post by:
While I am here, I just thought I'd ask if there is a recommended way of handling 2d dynamic arrays. I implement them using the following kind of code. ------------------------------------------------ int fname( int sizex, int sizey, <other inputs) {
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.