Like Tree2Likes
  • 1 Post By AndiPersti
  • 1 Post By std10093

Malloc - question

This is a discussion on Malloc - question within the C Programming forums, part of the General Programming Boards category; PhoneBook_Pointers.c Phone_Pointers.c .h Hi, Enclosed my phonebook code. could you please explane the need of this line: Code: phonebook=(phone*)malloc(sizeof(phone)); This ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Question Malloc - question

    PhoneBook_Pointers.cPhone_Pointers.c.h

    Hi,
    Enclosed my phonebook code.
    could you please explane the need of this line:
    Code:
     phonebook=(phone*)malloc(sizeof(phone));
    This line is needed but I don't understand why ...

    Thanks in advanced

    Code:
    int main(void)
    {
        char userChoice;
    
        phone *phonebook;
        phone *phonebook_ptr[100];
        phonebook=(phone*)malloc(sizeof(phone));
        phonebook_ptr[counter]=(phone*)malloc(sizeof(phone));
    
    .....
    
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    333
    malloc() creates a persistent block of memory, that lasts until you release it with free().

    It's hard to show the use in a tiny fragment. Normally there's no point in mallocing a single object, then freeing it, in main().

    You need malloc() 1) when you are allocating a lot of objects, and you don't know the number at compile time. 2) when you are passing data between functions a lot and need to maintain a "state" across calls.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    So if I understand corectly ...

    Could you please approve ?

    phone *phonebook; phone *phonebook_ptr[100]; phonebook=(phone*)malloc(sizeof(phone)); // because - passing data between functions phonebook_ptr[counter]=(phone*)malloc(sizeof(phone)); // because - don't know the number at compile time

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Post your code in [code]/*your code*/[/code] .
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    After looking through your code I think your problem is that you don't understand the difference between pointers and arrays.

    If you want to have an array of "phone" elements you either need to use a static array like
    Code:
    phone phonebook[100];
    or a dynamic array like
    Code:
    phone *phonebook = malloc(sizeof(*phonebook) * nr_of_entries);
    Your function declarations and calls would look like:
    Code:
    void PrintEntries(phone pbook[], int size);  // plural because you print all entries not just one
    ...
    PrintEntries(phonebook, current_size);
    Using a second parameter for the actual size of the array gets rid of the global counter.

    Bye, Andreas
    stahta01 likes this.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Question don't understand the answers

    Tanks for the answers but I don't think I understend the answers for my question.

    enclosed ( again) all my codePhoneBook_Pointers.cPhone_Pointers.c.h

    Code:
    int main(void) {          phone *phonebook;     phone *phonebook_ptr[100];     phonebook=(phone*)malloc(sizeof(phone));  // WHY need malloc here ? I don't understand     phonebook_ptr[counter]=(phone*)malloc(sizeof(phone)); //since don't know the data size  .....  }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    For reference -> Malloc - warning - Dev Shed
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,623
    Salem, you truly are everywhere!
    Devoted my life to programming...

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by ilans11il View Post
    Tanks for the answers
    You are welcome.
    GReaper likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    Sorry ,
    but I don't understend the answer
    why do I need this line:

    phonebook=(phone*)malloc(sizeof(phone));

    thanks


  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Because phonebook is just a pointer. It points to nowhere.
    You want this pointer to point at some memory, where data are going to be stored and processed by the programmer. Malloc allocates that memory for you and it returns a pointer to that block of memory.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ilans11il View Post
    Sorry ,
    but I don't understend the answer
    why do I need this line:

    phonebook=(phone*)malloc(sizeof(phone));
    The question you are asking is totally irrelevant. Your code is broken due to other reasons. The line you are asking about just keeps the compiler quiet but is the wrong solution.

    You have to understand how arrays work and how to pass them to functions. Start with static (fixed sized) arrays. You can think about dynamic arrays later.

    You could start with Arrays in C. Then you could continue with Arrays as Parameters and the grand final would be A Tutorial on Pointers and Arrays in C.
    There is also the C-FAQ which has a whole section about Arrays and Pointers (I think questions 2, 3, 4, 8, 10, 12, 13 and 14 are interesting for you at the moment).

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    Thanksssssssssss
    for the explenation and the greate info - I'll use it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Malloc question,... again!
    By audinue in forum C Programming
    Replies: 8
    Last Post: 10-28-2008, 01:14 PM
  3. malloc question
    By adrive in forum C Programming
    Replies: 11
    Last Post: 07-24-2008, 09:27 AM
  4. malloc( ) question
    By XSquared in forum C Programming
    Replies: 1
    Last Post: 02-21-2004, 10:46 PM
  5. another malloc question
    By Ian in forum Linux Programming
    Replies: 4
    Last Post: 10-16-2001, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21