Solved

Malloc question

Posted on 1998-11-23
7
285 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 119
Acrinis True image 2 78
What is sub-make ? 2 60
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 39
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 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.

776 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