Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pointer and malloc

pointer and malloc

pointer and malloc

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.
typedef char * array[100]
typedef array * array_ptr

void function(void)
array_ptr something;

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.

thanks for any help

tom moses

RE: pointer and malloc

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.

RE: pointer and malloc

Hey thanks, i was able to get it work without mallocing each pointer and it has seemed to work well so far :)

RE: pointer and malloc

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.

When you have the following declaration:

int a[3];

then the compiler will allocate space for you on the stack. Declaring something like:

int *a[3];

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.

int *a[3];

a[0] = (int *)malloc(sizeof(int));
a[1] = (int *)malloc(sizeof(int));
a[2] = (int *)malloc(sizeof(int));

Declaring the aobve declaration using the double ** notation,

int **a

requires you to allocate space for ALL the pointers, even the pointer to the array of int pointers.

int **a;

a = (int **)malloc(3 * sizeof(int *));
a[0] = (int *)malloc(sizeof(int));
a[1] = (int *)malloc(sizeof(int));
a[2] = (int *)malloc(sizeof(int));

hope this helps.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close