LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-14-2011, 06:17 PM   #1
Fritz_Doll
LQ Newbie
 
Registered: Dec 2010
Posts: 20

Rep: Reputation: Disabled
2D dynamic array, less code maybe?


Language: C
Is there a shorter way to do the following
m, n are generic values, where m is a constant and n is a variable.
type is any primitive.
Code:
type **k=calloc(n,sizeof(type*));
for(i=0;i<n;i++)
  k[i]=calloc(m,sizeof(type));
Or am I stuck with this, and should just write a function?
 
Old 10-14-2011, 07:15 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You can calloc an array of size n*m then access the (i,j) element with i*m+j.
Kevin Barry
 
Old 10-14-2011, 07:43 PM   #3
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
Do you want a 2D array of the type or of pointers to the type?

You can allocate the entire array at once with a single call:

ArrayName = calloc( m * n * sizeof( type ) );

malloc() may be faster than calloc() because it does not initialize the memory. If you are sure that you will not try to read from any element before you have initialized it, this could save time.

Please read the man pages or the descriptions of the functions to make sure that my recall of this information is not faulty.

If the array index size n will change after your first allocation then you can realloc() to change the array size. This preserves the contents of your array while giving you more or fewer elements.
 
Old 10-14-2011, 08:32 PM   #4
Fritz_Doll
LQ Newbie
 
Registered: Dec 2010
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks for the feed back.
Guess I forgot about treating 1D arrays as 2D.
I'll have to try to remember that.
 
  


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
C/C++ dynamic string array? Thesniperofdeath Programming 9 03-09-2011 03:06 AM
c++ dynamic array Fredstar Programming 16 03-19-2009 08:26 PM
Dynamic Array Sizes JMJ_coder Programming 8 03-05-2008 07:33 PM
list with dynamic array TurtleFace Programming 7 11-05-2006 06:58 PM
expanding dynamic array niteshadw Programming 6 06-25-2005 11:19 PM

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

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

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