Thread: MergeSort with array of pointers

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lionheart View Post
    Thanks for the info, Mats, but this is really over my league. All I know is that someone asked me to cast just like that, and I'm doing it.
    I'm studying for my BSc now, and I know about the C++ way of using pointers (new, delete and etc.)... I'm only now learning the advanced programming of C, and I see how f***** up that is, and probably we will learn other issues in the future.

    Thanks for the reply...
    Whoever asked you to cast malloc was misdirecting you. Note that you NEED to cast malloc() if you are compiling C code in a C++ environment, but you also have to abide to a whole range of other rules then. Do not compile pure C code with a C++ compiler.

    Edit: It should be noted that for example Visual Studio C++, although it is called C++, can also compile C code without any problem at all - all you have to do is call the file something.c instead of something.cpp [or you can force the issue by using the correct /Tp or /Tc (from memory) flags in the compiler options].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Jun 2008
    Posts
    23
    Mats, can you take a look at the code on the first page and explain to me why shouldn't I free all of the pointers in the array of pointers that point to the original array?... (I can't believe the logic of that sentence, but it makes sense)...

    By saying so, I have the a1 array of pointers and arr with values that I want to sort, using the
    a1 array. So I need to free all the pointers in the a1 array, am I not?
    Last edited by lionheart; 08-01-2008 at 10:13 AM.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I'm looking at the code you want me to look at (not sure), you don't malloc memory for the a1 pointers; you merely make them point at memory that was allocated elsewhere. [That is, you set outA[i] = &arr[i] -- that's not a malloc, that's just an assignment.] Since it's not your memory, you don't get to free it.

  4. #19
    Registered User
    Join Date
    Jun 2008
    Posts
    23
    Oh, now I see. So if I free that arr, it's enough "freeing"... No one but you gave me the exact explanation.

    I mean:
    outA[i] = &arr[i]

    That's all I needed to know...

    Many thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM