Help please: Want to write this piece of code without using malloc
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What is the lifetime of those allocations? For example, are they deleted before the end of the function in which they are created?
C99 allows you to declare local arrays (which are allocated on the stack) with sizes that are not know until run time (vs. ANSI C, which requires sizes of such arrays to be known at compile time).
is this your homework? ndiv is a constant, or where is it coming from?
Yes, it is. I've actually written the whole code already but I wanted to know if this particular part of the code could be written without using malloc. Yes ndiv is a constant.
What is the lifetime of those allocations? For example, are they deleted before the end of the function in which they are created?
C99 allows you to declare local arrays (which are allocated on the stack) with sizes that are not know until run time (vs. ANSI C, which requires sizes of such arrays to be known at compile time).
I'm using GNU. The lifetime of the allocations are deleted after the end of the function.
you seem to be making life very difficult for yourself.
maybe something like :
Code:
// define a struct to contain related variables:
struct thing {
double Ez, Hx, Hy, sigmaz, epsirz, amux;
};
// now you make a single array to keep track of
struct thing array[ndiv];
// and use like so:
array[t].sigmaz = 0.0;
array[t].Hx = 3.14;
// etc
Last edited by bigearsbilly; 11-09-2014 at 01:19 PM.
To add to the statements recommending pre-declaring your arrays; this is in my world a common practice, where we'll do a device supporting a certain amount of sessions or something similar and we initialize the system by either allocating all during initialization, or we declare fixed arrays as part of the code and yes we do place them in something like BSS.
The primary reason for doing this is since the product is supposed to support some maximum number of sessions, we do not take the chance that during peak operation, we could encounter an allocation failure. And you're kidding yourself if that's not the first thing QA does with your release.
Further, we also add headers to pre-allocated or statically mapped data so as to check the validity and integrity of the data. Sometimes simple things like start and end special markers or magic numbers. We may also place the address of the memory buffer somewhere within the buffer as a further verification step.
We re-use those buffers by way of shipping them from process to process. When the system breaks, and unfortunately it does, we have diagnostic utilities to validate the integrity of those buffers. If something breaks by accessing an out of range address, we can mostly tell what happened, because obviously a process shouldn't have an address of a buffer where that address is beyond the table's boundaries. Tracing back to the culprit who corrupted the address ... well that's always fun
I think I've only ever seen a bunch of nested allocations like that in academic exercises. My world has largely been routers, gateways, and such. Therefore they are designed to support what they advertise, hence our allocate or map in advance strategy. Thinking back, I did what you did and the first mentor I had was like "No, no, no, no, no ... c'mere kid, let's redo this here code ..." At least they were nice about it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.