Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Malloc question

Posted on 1998-11-23
7
Medium Priority
?
310 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 while-loops in the C programming language.

604 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