Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-02-2005, 09:41 PM   #1
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Help - memory allocation in C

Hello all,

I am writing a program that requires a global array of pointers to char:
char *target_mac[ 5 ];
I'm a little confused about exactly how to allocate memory for this--I think I have over-thought it and psyched myself out.

Unless I am really messed up, I do not need to allocate memory to the array itself: Since it is an array of pointers, that is taken care of with the [5] in the declaration, i.e., I get five pointers to char with that. Right? And the allocation for each element does not matter at this point, because a pointer is just a memory location, and I've given myself five here.

Each element needs to have memory allocated to it this:
for( x = 0; x < 5; x++ ) {
    target_mac[ x ]  = ( char * ) malloc( MAX_SIZE_ETHADDR + 1 );
I assume the above allocation can be coded in main( ) and later free( )'d when I am done with it, but again, I think I have psyched myself out. Perhaps a little diagram of the flow of the array usage might help:
+main() // allocate the memory to the array elements, pass ipAddr
+--+getMac( ipAddr ) passes ipAddr to otherFunction( )
   +--+otherFunction( ipAddr ) // doesn't touch target_mac[ ]
+--+getMac( ) // now packs target_mac[ ] with info from otherFunction
+main() // uses info in target_mac array and then frees it
Hope that makes sense, and everyone can see what I'm asking here.
Old 09-02-2005, 10:20 PM   #2
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,289

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Right, you got it. -- memory for the array itself is allocated when you make your array declaration. That gives you five "boxes", each of which contains a pointer to char. As you say, it is your responsibility to make those pointers point to something useful before dereferencing them. If you do that, you can use them in other functions and then free them when your program is done.
Old 09-03-2005, 10:11 PM   #3
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
You seem to understand arrays of pointers. I'm not sure what your code does so your little diagram is a little confusing

But basically you can do a few things with that array of char pointers. First, you could pass the whole array to a function, along with the number of elements in the array. The function could then read/modify the data and return. Second, you could pass a single element of the array to a function to be read/modified. You should see a clear distinction between passing a single element and passing the entire array. Remember, both would be passed as different types as well:

void modify_array(char * arr[], int sz);  /* modify_array(target_mac, 5);  */
void modify_elmnt(char * elmt);           /* modify_elmnt(target_mac[0]);  */
Hope this helped clarify a little,

- lowpro2k3
Old 09-04-2005, 11:16 AM   #4
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
It's also common to have the last pointer in the array be NULL; this way the function knows when it's at the last pointer without you passing a count. (e.g. the exec* functions)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
memory allocation esael Linux - General 7 01-12-2008 01:12 PM
memory allocation gecoool Programming 2 10-24-2005 10:47 AM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 10:24 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 10:22 PM
memory allocation. raven Programming 5 09-08-2002 02:50 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration