Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a dynamic array in C 1

Status
Not open for further replies.

Andy23P

Programmer
Dec 2, 1999
1
0
0
CA
Visit site
Is it possible to create or simulate a dynamic array in Ansi C? <br>

 
Yes, you can use the functions calloc, malloc and realloc to dynamically allocate memory from the heap at runtime. <br>
<br>
For example, you can read the size of the array from the user and use this to dynamically allocate space for the array.<br>
<br>
int size;<br>
char *buf;<br>
<br>
scanf(&quot;%d&quot;, &size);<br>
<br>
buf = (char *)malloc(sizeof(char) * size);<br>
<br>
if ( buf != NULL ) {<br>
...<br>
...<br>
...<br>
}<br>
<br>
...<br>
...<br>
...<br>
<br>
free(buf);<br>
<br>
malloc and calloc allocate a chunk of memory from the heap at runtime. when you are done using this chunk you should always free it as shown above. There is another function which you will find useful and that is realloc. Realloc takes a pointer to a chunk of heap memory and changes the size. So you can allocate more space to an already allocated array. The same holds true here, free it once you are done using it otherwise you will have a memory leak in your program.<br>
<br>
<br>
<br>

 
Yes , at run time you will have to calculate the length of the memory to be allocated.For eg. <br>
If at run time you come to know that you need to have a character array of 20 bytes. Do :<br>
char *ptr;<br>
ptr = (char*) malloc(sizeof(char) * 20);<br>
<br>
Similarly you can create a 2-d , 3-dimensional or multi-dimensional array dinamically.<br>
<br>
Does it answer your question ?<br>
<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A><br>

 
I am currently on a college course where this stuff would be useful, would the following code work if I was using a struct datatype?

struct a
{ ...
...
} test[];

test[1] = malloc(sizeof(struct a));
.....
.....
free(test[1]);

I am planning to use this in my coursework but I haven't had chance to test it, so some advice would be cool.

Thanks.

Dan Cambridge
 
Hope this helps.
This is how I would do it:

#include <stdio.h>

typedef struct tagMyStruct
{
int number;
}MyStruct;

main()
{
MyStruct *test;

test = (MyStruct *) malloc(sizeof(MyStruct));

...

free(test);

return 0;
}

 
You don't require to define the array(struct array in this case) as static and then malloc it .Either you first declare it as dynamic and then allocate memory from heap dynamically or declare it to be static and then use it without allocating memory using malloc .
hope this helps .
Arun
 
By itself, arrays in C are not dynamic and never will be. O yes there are those local variables and thus local arrays that are allocated dynamically, whenever a routine starts, but you should recognize that, IN THE MIDDLE OF THE ROUTINE, you cannot just suddenly change the size of an array. You really DO need to simulate it. There is no way in ANSI C's grammar for you to declare a truly dynamic variable or array. C++ is another story...

Fortunately we can easily simulate a dynamic array in C. For one thing, C makes little distinction between arrays and pointers. Since we are using a pointer to store the address of the piece of memory we are using as a dynamic array, we can use it as if it were the array itself. In the above example, you can refer to test[0].number, test[1].number, test[n].number and the compiler will generate correct code. Care must be taken of course, because the compiler cannot know the limits of the array.

&quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Thanks for your advice folks,

I have gone so far as to create a bi-directional linked list based on what you have told me.

I have another post with regards to formatting and input masks, does anyone have any ideas?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top