Solved

multi-dimensional malloc/realloc

Posted on 1997-02-21
1
760 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Super Scope, DHCP 5 91
Resolve Dependency Issues 4 79
Display a Float Variable in C without using the function printf. 2 26
Please explain C language error codes ? 5 28
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

828 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