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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer and malloc 1

Status
Not open for further replies.

tmoses

MIS
Dec 4, 2001
98
US
The question i have has to do with my next project. Its not mission critical but it is something that i am not sure of in c.<br>
typedef char * array[100]<br>
typedef array * array_ptr<br>
<br>
void function(void)<br>
{<br>
array_ptr something;<br>
}<br>
<br>
something is a pointer to an array of pointers of type char. I know that the compiler will allocate? space for the first pointer but does it allocate space for the other 100 ? Would i have to go in and malloc space for each individual pointer? i wouldn't be putting any information in the pointers just copying existing addresses into them. its to becuase the sort our teacher wants us to use i based upon implimentation in an array. <br>
<br>
thanks for any help<br>
<br>
tom moses <br>
<br>

 
In my experience you do have to malloc each element. However by using the [] array declaration I think most C compilers allocate space for it automatically.
 
Hey thanks, i was able to get it work without mallocing each pointer and it has seemed to work well so far :)
 
You only need to allocate space when you want to store information. Since in your case you are setting the pointers to pre-existing (already allocated) addresses, then there is no need to allocate space. As a matter of fact, allocating space in this situation can lead to a memory leak, because you would have allocated space at the beginning and then overwritten the pointers, thus being unable to free the space you allocated at the beginning. In almost every other situation where you need to copy information into the space, you need to allocate it first. <br>
<br>
When you have the following declaration:<br>
<br>
int a[3];<br>
<br>
then the compiler will allocate space for you on the stack. Declaring something like:<br>
<br>
int *a[3];<br>
<br>
requires you to allocate space for the 3 pointers if you want to store information in them, otherwise you will get a segmentation fault when trying to run the program.<br>
<br>
int *a[3];<br>
<br>
a[0] = (int *)malloc(sizeof(int));<br>
a[1] = (int *)malloc(sizeof(int));<br>
a[2] = (int *)malloc(sizeof(int));<br>
<br>
<br>
Declaring the aobve declaration using the double ** notation,<br>
<br>
int **a<br>
<br>
requires you to allocate space for ALL the pointers, even the pointer to the array of int pointers.<br>
<br>
int **a;<br>
<br>
a = (int **)malloc(3 * sizeof(int *));<br>
a[0] = (int *)malloc(sizeof(int));<br>
a[1] = (int *)malloc(sizeof(int));<br>
a[2] = (int *)malloc(sizeof(int));<br>
<br>
<br>
hope this helps.<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top