LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-02-2005, 08:41 PM   #1
zaichik
Member
 
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:
Code:
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 though...like this:
Code:
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:
Code:
+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.
TIA.
 
Old 09-02-2005, 09:20 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

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, 09:11 PM   #3
lowpro2k3
Member
 
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:

Code:
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, 10:16 AM   #4
aluser
Member
 
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)
 
  


Reply



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 12:12 PM
memory allocation gecoool Programming 2 10-24-2005 09:47 AM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:24 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:22 PM
memory allocation. raven Programming 5 09-08-2002 01:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 PM.

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