Solved

multi-dimensional malloc/realloc

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to define a string array with big size in c language 10 129
How to get time_t value that is a week old 6 163
Detect CR LF to each line 12 152
Computer slow / BSOD 10 38
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…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

914 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

18 Experts available now in Live!

Get 1:1 Help Now