Solved

multi-dimensional malloc/realloc

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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 conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now