LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2014, 01:03 AM   #1
Nenita
LQ Newbie
 
Registered: Nov 2014
Posts: 7

Rep: Reputation: Disabled
Post Help please: Want to write this piece of code without using malloc


Hi everyone, I'm writing a code but I would like to write a section of it without having to use malloc. Is there any other way to achieve this?

Code:
double **Ez, **Hx, **Hy, **sigmaz, **epsirz, **amux;
    Ez  =  (double**)malloc(sizeof(double*) * ndiv);
    Hx  =  (double**)malloc(sizeof(double*) * ndiv);
    Hy  =  (double**)malloc(sizeof(double*) * ndiv);
    sigmaz  =  (double**)malloc(sizeof(double*) * ndiv);
    epsirz  =  (double**)malloc(sizeof(double*) * ndiv);
    amux  =  (double**)malloc(sizeof(double*) * ndiv);
    
    for(i = 0; i<ndiv; i++){
      Ez[i]  =  (double*)malloc(sizeof(double)*ndiv);
      Hx[i]  =  (double*)malloc(sizeof(double)*ndiv);
      Hy[i]  =  (double*)malloc(sizeof(double)*ndiv);
      sigmaz[i]  =  (double*)malloc(sizeof(double)*ndiv);
      epsirz[i]  =  (double*)malloc(sizeof(double)*ndiv);
      amux[i]  =  (double*)malloc(sizeof(double)*ndiv);
    }
 
Old 11-09-2014, 05:03 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
is this your homework? ndiv is a constant, or where is it coming from?
 
Old 11-09-2014, 07:01 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
What dialect of C or C++ are you using?

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).
 
Old 11-09-2014, 08:47 AM   #4
Nenita
LQ Newbie
 
Registered: Nov 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 11-09-2014, 08:52 AM   #5
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
if niv is a constant than you know the size of you arrays and you can define it at compile time
double a[const][const];
 
Old 11-09-2014, 08:54 AM   #6
Nenita
LQ Newbie
 
Registered: Nov 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
What dialect of C or C++ are you using?

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.
 
Old 11-09-2014, 08:55 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Depending on where and how ndiv is defined, you might be able to do something as simple as:
Code:
double Ez[ndiv][ndiv]
and so on.

Be aware that sticking large amounts of data on the stack can lead to some nasty problems though.
 
Old 11-09-2014, 08:57 AM   #8
Nenita
LQ Newbie
 
Registered: Nov 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
What I'm did was write a code simulating Electromagnetic Wave Propagation using 2D FDTD.
 
Old 11-09-2014, 10:49 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It's not clear why you want to avoid malloc. Nonetheless, you can still use calloc or realloc.
 
Old 11-09-2014, 10:54 AM   #10
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
To avoid putting a large amount of data on the stack, you can also declare it as global which puts it uninitialized data space (BSS).
 
Old 11-09-2014, 01:16 PM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 11-14-2014, 12:24 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 11-14-2014, 01:46 PM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by rtmistler View Post
We may also place the address of the memory buffer somewhere within the buffer as a further verification step.
[hijack]
hmm interesting idea.
I might steal that one.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can we reduce the piece of code santosh0782 Linux - Newbie 5 08-20-2014 05:38 PM
Posting piece of code on the internet vinians Linux - Newbie 2 10-03-2009 07:53 PM
piece of code Volcano Programming 8 08-04-2009 09:28 AM
is there a piece of c code that can set the terminal window tinieprotonjam Programming 2 01-27-2007 07:20 AM
nice piece of code ....HOW DOES IT WORK? deadhead Programming 7 12-07-2003 12:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration