473,385 Members | 2,005 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,385 software developers and data experts.

memory allocation for two dimensional array

I need to allocate memory dynamically for a two dimensional array. It's
basically a pointer to an array of 2 element arrays: I declared it as long
int ** arr. The number of two element arrays are unknown at compile time.

I am not quite sure if I allocated memory properly for this (I am sure I
didn't).

arr = malloc((sizeof(long int)*2)*number_of_elements)

when I debug it, I have access to arr[0][0], arr[1][0], arr[2][0], etc...
but not arr[0][1]. It tells me it can't access memory which leads me to
believe I don't quite have my memory allocation and/or declaration correct.

I have tried a few different ways, but I obviously haven't found the right
way to do it yet.

Any suggestions?

Thanks a bunch!

John
Nov 14 '05 #1
2 9369

"John Hanley" <jd******@telusplanet.net> wrote in message
news:10***************@proxy2.srv.ualberta.ca...
I need to allocate memory dynamically for a two dimensional array. It's
basically a pointer to an array of 2 element arrays: I declared it as long int ** arr. The number of two element arrays are unknown at compile time.

I am not quite sure if I allocated memory properly for this (I am sure I
didn't).

arr = malloc((sizeof(long int)*2)*number_of_elements)

Have you tried looking into C-FAQ section.
Allocating 2D arrays are nicely discussed there

- Ravi
Nov 14 '05 #2
John Hanley wrote on 02/08/04 :
I need to allocate memory dynamically for a two dimensional array.

It's a FAQ.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #3

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

Similar topics

1
by: sugaray | last post by:
I don't get why the correct way to allocate memory space for 3d-array should be like this in C++: long (*ptr); ptr=new long; and when deallocation delete ptr;
4
by: kak3012 | last post by:
Hi, After my question today, I am not sure if this is the right group to ask but I have a simple question I guess. I have multi dimensional array called cover what I do is this iT GIVES AN...
6
by: R.kumaran | last post by:
hello all I have a problem in allocating memory for a pointer to an array. this is my code void main() { int(*a); *a=(int *)malloc(2*2*sizeof(int)); ........
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
5
by: Yi Xing | last post by:
I tried the following code: ... i=i+1 ... a.append(float(i)) ... Traceback (most recent call last): File "<stdin>", line 3, in ? MemoryError to see the size of the array at the...
21
by: =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= | last post by:
Hello, I´m using a very large 2-dimensional double matrix (45.000x45.000) in my program. At the initialization of the matrix: double matrix = new double I am getting an out of memory...
81
by: Peter Olcott | last post by:
It looks like System::Collections::Generic.List throws and OUT_OF_MEMORY exception whenever memory allocated exceeds 256 MB. I have 1024 MB on my system so I am not even out of physical RAM, much...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
11
by: gianluca | last post by:
Hy list, I've to declare a 3D matrix in C . I tried with that code: double ***mat; mat=(double***)G_malloc(ndimension*(sizeof(double)); but the program send me a run time error.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.