?
Solved

multi-dimensional malloc/realloc

Posted on 1997-02-21
1
Medium Priority
?
767 Views
Last Modified: 2012-05-04
suppose I want an array like  array[?][8][32]
where I dont know how big the ? element needs to be
I find out somehow that ?=1000 so I have to malloc the array space.
Later I find out that 1000 was not big enough and it needs to be 5000 so I must realloc a larger chunck.

I dont know how to declare the array so that malloc works
nor do i know how to pass this to malloc, to say nothing of the realloc part of it.

Any suggestions would be helpfull
0
Comment
Question by:haggedk1
1 Comment
 

Accepted Solution

by:
noddy earned 100 total points
ID: 1249583
Declare a pointer to the array as:-

type*** pArray=0;

To allocate it do:-

pArray=malloc(sieof(type)*8*32*x);

where x is your '?'

Then to reallocate it if your x grows:-
pArray=realloc(pArray, sizeof(type)*8*32*newx);

this will preserve the contents of the array if the array grows but pArray might be a different value.

And then use:-

pArray[x][y][z]=...;

to address an item in the array.

NB: malloc and realloc may return 0 if the request cannot be met by the OS.

NB2: if you are using DOS or Win16, beware of the segment limitation of 64K

to use the array.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question