Solved

multi-dimensional malloc/realloc

Posted on 1997-02-21
1
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
noddy earned 50 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

Independent Software Vendors: 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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

734 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