Solved

Malloc question

Posted on 1998-11-23
7
276 Views
Last Modified: 2010-04-15
I have a 2 dimensional array: list[][]
I need to allocate a certain amount of space to it, determined by a third variable, space.

The size of the array is only determined at run-time.
How do I say allocate memory to make the array: list[space][13]
where the 13 is always constant.

I have tried defining the array as "char list[1][13]" at the start of the function and then calling realloc, but it doesn't work.

Any ideas?
0
Comment
Question by:idek1
  • 4
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1254583
What you need is a pointer to an array of thirteen elements. The number of these elements that there are, is immaterial. That is analogous to have a pointer to an int. The number of ints there are is irrelevant. So declare:

char (*list)[13];

Then when you have space you can:

list=(char (*)[13])malloc(space, sizeof(char [13]));

proceed to use list as you would any 2d array.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1254584
Is there something that you need clarified before you grade the answer?

0
 

Author Comment

by:idek1
ID: 1254585
Apologies for taking so long... my malloc (Borland C++ 3.1/Dos), only has one paramter, which is the size in bytes of whatthe required memory,  so i assume the lina changes to:
list=(char (*)[13])malloc(space);

Yes?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 16

Expert Comment

by:imladris
ID: 1254586
Almost. Sorry, I tend to use calloc. Anyway, given that list is supposed to wind up with max dimensions of space and 13 (i.e. list[space][13]) you would want to malloc as:

list=(char (*)[13])malloc(space*sizeof(char [13]));


0
 

Author Comment

by:idek1
ID: 1254587
Just a further query... Now that I have declared a number of these 2d arrays, say list1, list2, list3, list4 and list5... I want to group them in an additional array so that I can access the 2d array using an index, e.g. in a for loop I want to be able to access ListArray[2] which would be an array holding all the 2d arrays, so ListArray[2] refers to list3, etc.

Thanks,
(P.S. I also need to access elements within the 2d arrays using the ListArray, i.e. I need access to say list4[3] using ListArray.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1254588
So you need a pointer to a pointer to an array of thirteen characters:

char (**ListArray)[13];

You would then malloc for the number of 2d arrays there are (say lists):

ListArray=(char (**)[13])malloc(lists*sizeof(char (*)[13]));

Then you would have to assign each one:

ListArray[0]=list1;
ListArray[1]=list2;
ListArray[2]=list3;
etc. etc.

Perhaps it would be easier to just have ListArray? And do the list mallocing in a loop (instead of assigning list1 etc. to ListArray).

0
 

Author Comment

by:idek1
ID: 1254589
Thank you very much for your help.
I actually ended up using a structure which contained the list pointer and some other information, and I used an array of these structures to hold all the information.  
Nevertheless, your answers very very helpful.

Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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

12 Experts available now in Live!

Get 1:1 Help Now