1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

type char **

Discussion in 'C' started by wever, Sep 4, 2014.

  1. wever

    wever New Member

    Joined:
    Sep 4, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Can you explain me why this code has two pointers?

    Code:
    char ** binaries; // Why two pointers here? Why pointer to pointer?
    binaries = (char **)malloc( sizeof(char *)*nDevices );
    Complete code for this can be found on Paste.of code U5g2Z3Cj5ZuRBnJf2b5cqv
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,010
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Would you understand this code?
    Code:
    int *binaries;
    binaries=(int*)malloc(sizeof(int)*nDevices);
    
    TYPE *foo;
    foo=(TYPE*)malloc(sizeof(TYPE)*nDevices);
    
    The first two lines allocates an array of nDevices ints; malloc returns a pointer to that block of nDevices ints, and so the return value is cast to int* to stop the compiler complaining.

    The second two is the generic form; whatever TYPE is, we allocate an array of TYPEs which is nDevices long, and foo's type is TYPE*, so malloc's return value is cast to TYPE*.

    Simply replace TYPE with char* and there's the explanation. The variable "binaries" is a list of char* values, each of which is a pointer to char. That's why it's a char**.
     
    shabbir likes this.
  3. wever

    wever New Member

    Joined:
    Sep 4, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for response. So the malloc deallocates the memory, which was already allocated by int. In case of 32bit windows it could be 4 bytes for integer. NDevices is 18. So it could be 18*4 bytes. So the binaries is not int anymore, it is array. I think this is amazing but also it was so confusing because first we declare it as e.g. 4 bytes integer and then we make it 72 bytes array... However in the case of char * we don't know how many characters will be saved in the char array. There is a function used, not sent here, which fills the char* with various strings with various lengths. In my case the binaries buffer is filled only at the first element and the rest is empty. I think I understand now. Thanks
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,010
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    >>So the malloc deallocates the memory

    No, malloc allocates memory. It doesn't deallocate anything. You might be thinking of Java, where a memory block is deallocated by the garbage collector (eventually) when nothing is pointing to it any more. That doesn't happen in C, and if you lose a pointer to allocated memory then you create a memory leak.

    >>which was already allocated by int.

    No, int doesn't allocate memory (well, it does, but not in the same way as malloc). Anyway, there isn't an int declaration. Did you mean this line of code:
    Code:
    int *binaries;
    
    If so, then this doesn't allocate any memory for binaries. It creates a pointer on the stack (OK, so yes it does allocate memory, but that's the local variable which is an uninitialised pointer to memory which hasn't yet been created). In this case binaries is defined as a pointer to an int.

    Let me clarify this. I have a friend called Joe, whose address I am going to write on this little piece of paper. The paper is equivalent to the int* declaration above. Both contain SPACE for information. The piece of paper does not cause Joe's house to be built or a plot allocated.

    Actually, Joe's house hasn't been built yet, and he doesn't have a plot of land. Time passes, and in the meantime our program does this:
    Code:
    binaries=(int*) malloc(256);
    
    OK, now Joe has a plot of land and I can give you the location (46 Main Street, Someplace), but nothing has been built there yet. Technically he has an address, but just as *binaries hasn't been initialised yet, Joe's house also hasn't been built, so when you go there you won't see much but garbage. (Note that binaries, as opposed to *binaries, HAS been initialised, with the return value from malloc, and there are now 256 bytes assigned to binaries.)

    >>There is a function used, not sent here, which fills the char* with various strings with various lengths.

    Technically you can't fill a char* with anything other than a pointer to memory. "If you have a string", like that one, and that string is located at memory address 0xdeadbeef, then you can set the char* variable to contain 0xdeadbeef. It's fairly common shorthand to say that a char* "contains" a string, but it's misleading. It contains a pointer TO a string.
     
    shabbir likes this.
  5. wever

    wever New Member

    Joined:
    Sep 4, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    It was a type. I wanted to type that it reallocated the memory. I cannot edit the post, so sorry for that typo.
     
  6. wever

    wever New Member

    Joined:
    Sep 4, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for nice explanation. The example with address, plot and how is simply clever.
     
  7. wever

    wever New Member

    Joined:
    Sep 4, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for nice explanation. The example with address, plot and house is simply clever.
     
  8. gusaniinfotech

    gusaniinfotech New Member

    Joined:
    Sep 9, 2014
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Thank you xpi0t0s I do learn lots...thank you very much for posting
     

Share This Page