473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamic allocation of memory to 2 dimensional Array in a Single malloc Call

21
To allocate a 2D array of integers, in such a way that the array elements can be accessed with a[i][j] syntax i.e., should not use pointers to access the values.

Note: The function should perform only a single malloc call.

The function declaration is:

int **alloc_array( int nrows, int ncols);
Apr 18 '11 #1
5 4316
neeru29
21
I had tried with the following statement to allocate memory to 2D array:

Expand|Select|Wrap|Line Numbers
  1. arr2D = (int **)malloc(row * sizeof(int *) + row * col * sizeof(int));
  2.  
  3. for(i = 0; i< row; i++)
  4. {
  5.  for(j=0; j< col; j++)
  6.  {
  7.   scanf("%d", &a[i][j]); // here the memory segmentation is displayed.
  8.  }
  9. }
  10.  
Please help me out
Apr 18 '11 #3
donbock
2,426 Expert 2GB
Are you sure you can't use two malloc calls to set this up?
Apr 18 '11 #4
neeru29
21
i got it by using multiple malloc calls.. i want it by single malloc call
Apr 18 '11 #5
weaknessforcats
9,208 Expert Mod 8TB
Remember, there is no such thing as a 2D array.

And an int** does not point to a 2D array.

If you have a 9 element array, the elements are in memory as

1 2 3 4 5 6 7 8 9

and you can't tell if this is a [9][1] or a [3][3].

So when you allocate you allocate the number of elements.

Let's say you need a [4][6] array of int. That's 24 ints. So allocate 24 ints.

malloc will return a pointer to the 24 ints.

Now a [4][6] array is an array of 4 elements where each element is an array of 6 int. Therefore typecast the return from malloc as a pointer to an array of 6 int:

Expand|Select|Wrap|Line Numbers
  1. int (*MyArray)[6] = (int(*)[6])malloc(24*sizeof(int));
You will need a separate variable to hold the 4 which is the number of elements.

Go back and read that article again. The final example is the one I am talking about here.
Apr 18 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lestat | last post by:
I have switched to a new compiler (gcc/g++ 3.2 on rh linux 8.0) and I cannot seem to dynamically allocate two dimentonal arrays (or even singel dimetional arrays!) of any user defined classes. ...
4
by: CodeMedic | last post by:
To anyone that is more intelligent than me or just knows better, I could use some help. I am attemping to write a simple program that will created a random maze that will only require for...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
2
by: frankh | last post by:
With Marshal you can copy data from a one-dimensional array to BitmapData.Scan0. What do I do if my array is two-dimensional? Or, alternatively, can I create somehow two variables byte arr1; ...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
13
by: jimjim | last post by:
Hello, I am coming from a C background and the below dynamic allocation of an array of pointers makes sense to me: #define SIZE 2 int **p; p = malloc ( SIZE * sizeof ( int * )); for(int j=0;...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
24
by: VijaKhara | last post by:
hi all, i am trying to create a dynamic 2D array with size N x 3 (N will be put in as a parameter) using the following code: int **xyz; int i,N; N=30000; xyz=malloc(3*sizeof(int*));
1
by: CzyToNieDariusz | last post by:
Hi! I was looking a lot about that, but i didn't find any answer so I decided to ask here. I have a function: void allocate(double **m,int n) { int i; ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.