malloc ,calloc code problem..

This is a discussion on malloc ,calloc code problem.. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> /* required for the malloc, calloc and free functions */ int main() { float *calloc1, ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    malloc ,calloc code problem..

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    /* required for the malloc, calloc and free functions */
    
    int main() {
      float *calloc1, *calloc2, *malloc1, *malloc2;
      int i;
    
      calloc1 = calloc(3, sizeof(float)); /* might need to cast */ 
      calloc2 = calloc(3, sizeof(float));
      malloc1 = malloc(3 * sizeof(float));
      malloc2 = malloc(3 * sizeof(float));
    
    if(calloc1!=NULL && calloc2!=NULL && malloc1!=NULL && malloc2!=NULL) {
    
        for(i=0 ; i<3 ; i++) {
          printf("calloc1[&#37;d] holds %05.5f, ", i, calloc1[i]);
          printf("malloc1[%d] holds %05.5f\n", i, malloc1[i]);
          printf("calloc2[%d] holds %05.5f, ", i, *(calloc2+i));
          printf("malloc2[%d] holds %05.5f\n", i, *(malloc2+i));
        }
    
        free(calloc1);
        free(calloc2);
        free(malloc1);
        free(malloc2);
    
        return 0;
      }
      else {
        printf("Not enough memory\n");
        return 1;
      }
    }
    
    Output:
    calloc1[0] holds 0.00000, malloc1[0] holds -431602080.00000
    calloc2[0] holds 0.00000, malloc2[0] holds -431602080.00000
    calloc1[1] holds 0.00000, malloc1[1] holds -431602080.00000
    calloc2[1] holds 0.00000, malloc2[1] holds -431602080.00000
    calloc1[2] holds 0.00000, malloc1[2] holds -431602080.00000
    calloc2[2] holds 0.00000, malloc2[2] holds -431602080.00000

    at first they build 4 pointers
    each one of them points to the start of a sector whose size is 3 float variables.

    then the print some how
    calloc1[i] should return only the address

    *(calloc2+i) should return the data inside the address

    but they get similar output

    i cant understand these printf lines

    ??

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    calloc1[i] and *(calloc1 + i) are essentially saying the same thing.

    Both return the contents of the i'th floating point value from the start of the memory specified by the pointer.
    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.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no

    calloc will return the first cell

    celloc[i] will not

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No. *(calloc1 + 1) is the same as calloc1[i]. Please do not argue with senior members who are not incorrect.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    there is no array called calloc1[i]

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    /* required for the malloc, calloc and free functions */
    
    int main() {
      float *calloc1, *calloc2, *malloc1, *malloc2;
      int i;
    
      calloc1 = calloc(3, sizeof(float)); /* might need to cast */ 
      calloc2 = calloc(3, sizeof(float));
      malloc1 = malloc(3 * sizeof(float));
      malloc2 = malloc(3 * sizeof(float));
    
    if(calloc1!=NULL && calloc2!=NULL && malloc1!=NULL && malloc2!=NULL) {
    
        for(i=0 ; i<3 ; i++) {
          printf("calloc1[&#37;d] holds %05.5f, ", i, calloc1[i]);
          printf("malloc1[%d] holds %05.5f\n", i, malloc1[i]);
          printf("calloc2[%d] holds %05.5f, ", i, *(calloc2+i));
          printf("malloc2[%d] holds %05.5f\n", i, *(malloc2+i));
        }
    
        free(calloc1);
        free(calloc2);
        free(malloc1);
        free(malloc2);
    
        return 0;
      }
      else {
        printf("Not enough memory\n");
        return 1;
      }
    }

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Please do not argue with two senior members.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    More to the point, still, is do not argue with (senior) members who are not incorrect.

    And again, [] dereferences for free and without charge. Think about all the arrays you've ever used: you didn't want score[5] to be a pointer to a score, you wanted it to be a score itself.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    This may surprise you but:
    a[b] is exactly the same as *(a + b), which is the same as *(b + a), which is the same as b[a].

    So in your case, the below four expressions are identical:
    Code:
    *(malloc2+i)
    *(i+malloc2)
    malloc2[i]
    i[malloc2]
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    calloc1[i] wasnt declared as an array

    i see here only calloc1 which points at the
    start of a sector whose size is 3 float variables.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by transgalactic2 View Post
    calloc1[i] wasnt declared as an array

    i see here only calloc1 which points at the
    start of a sector whose size is 3 float variables.
    It does not have array type, true. What that has to do with [i] at the end of it, we're not sure. You can stick [i] at the end of any pointer.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok but
    *(malloc1+i) will return the content of address "i"

    but
    malloc1[i] will return the address "i" itself

    they are not the same

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > i see here only calloc1 which points at the
    > start of a sector whose size is 3 float variables.
    OK, so how do YOU propose to get to the 2nd and 3rd ones then (or even the first)?
    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.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by transgalactic2 View Post
    ok but
    *(malloc1+i) will return the content of address "i"

    but
    malloc1[i] will return the address "i" itself

    they are not the same
    And again, in caps and bold so maybe you'll read it this time, USING BRACKETS AND AN INDEX WILL DEREFERENCE (GIVE THE VALUE POINTED TO), NOT THE ADDRESS ITSELF.

    *(malloc1+i) and malloc1[i] are DEFINED TO BE EXACTLY THE SAME THING.

    Better?

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    I'm beginning to smell troll here....

    *(malloc1+i) will return the content of address "i"
    but
    malloc1[i] will return the address "i" itself

    WRONG - they're the same damn thing.

    And neither of them have anything to do with the address of i

    malloc1 is the address
    malloc1[0] is the contents of the first address
    malloc1[1] is the contents of the second address
    *(malloc1+1) is the same as malloc1[1] only written differently
    malloc1[i] is the contents of the i'th address.
    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.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my morse code program
    By justin87 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2007, 05:23 PM
  2. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM

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