?
Solved

Malloc question

Posted on 1998-11-23
7
Medium Priority
?
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

800 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