malloc and pointers

This is a discussion on malloc and pointers within the C Programming forums, part of the General Programming Boards category; hi all Code: #include <stdio.h> #include <stdlib.h> main() { char *aa; if((aa=malloc(100 *sizeof(char)))==NULL) { printf("Alloctaing Error\n"); exit(-1); } printf("%d",sizeof(aa)); } ...

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    malloc and pointers

    hi all
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
          char *aa;
         
           if((aa=malloc(100 *sizeof(char)))==NULL)
              {
                            printf("Alloctaing Error\n");
                            exit(-1);
               }
          
          printf("%d",sizeof(aa));
    }
    it give me 4 as output !!!!!!!!!!!!!
    but

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
          char  aa[100];
               
          printf("%d",sizeof(aa));
    }
    gives me 100 !!!
    so where is the problem ?
    why in the first code when i used malloc it gives me 4 but in the second code give me the object aa size ??
    thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    In the first case aa is a char pointer. On 32bit machines pointers are 32bit = 4bytes, so that's why sizeof(aa) is returning 4.

    In the second case aa is a vector of 100 elements, so sizeof will return 100*sizeof(char) = 100*1.
    In C vector names are also pointers to the first element, but that doesn't mean that aa is actually a pointer.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    And by vector, XayC actually means array.
    If you understand what you're doing, you're not learning anything.

  4. #4
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    aha
    so in arrays it returns the size of elements * size of array
    but pointers return the size of first element pointed to by the pointer
    thanks

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    but pointers return the size of first element pointed to by the pointer
    No, using the sizeof operator on a pointer will return the size of the pointer, no matter what type it points to.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    "man sizeof"

    returns the size (in number of bytes) of the argument passed in.

    pointers = 4 bytes (on most desktop)

    char = 1 byte (again on most desktop)

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > char = 1 byte (again on most desktop)
    Always, actually. (The catch is that in C/C++, a byte is _at least_ 8 bits, not necessarily exactly 8.)

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    In C at least sizeof char is always 1:

    When applied to an operand that has type char , unsigned char , or signed char , (or a qualified version thereof) the result is 1.
    http://web.archive.org/web/200502070...t.html#3.3.3.4
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    thanks all
    " When applied to an operand that has type char , unsigned char , or signed char , (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array"
    thanks allot vart
    Last edited by St0rM-MaN; 06-20-2007 at 04:57 AM.

  10. #10
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Shouldn't
    Code:
    if((aa=malloc(100 *sizeof(char)))==NULL)
    be:
    Code:
    if((aa=malloc(100 *sizeof(char *)))==NULL)

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Absolutely not. A char * points to chars, not other char *'s.

    To make it even less problematic, you should write something like (100 * sizeof(*aa)) for the size. That way, if you have to change the type of variable aa, you don't have to worry about the memory allocation size.

  12. #12
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    sorry i was just going through a guide and came across this:
    Code:
    int main(void)
    {
        char **p;
        int i;
        p = malloc(sizeof(char*) * 10);  // allocate 10 char*s-worth of bytes
        for(i = 0; i < 10; i++) {
            *(p+i) = malloc(30); // 30 bytes for each pointer
            // alternatively we could have written, above:
            //   p[i] = malloc(30);
            // but we didn't.
            sprintf(*(p+i), "this is string #%d", i);
        }
        for(i = 0; i < 10; i++) {
            printf("%d: %s\n", i, p[i]); // p[i] same as *(p+i)
        }
        return 0;
    }
    Hence, why I suggested it.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    And it's correct there, because p is a pointer to a pointer in that case.
    If you understand what you're doing, you're not learning anything.

  14. #14
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    that's right you are using array of pointers
    that's new for me thanks for adding it

  15. #15
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    And it's correct there, because p is a pointer to a pointer in that case.
    Oh sorry, I completely misread the *aa. I thought s/he put **a. My mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers + Malloc = Painloc
    By Chalks in forum C Programming
    Replies: 9
    Last Post: 10-19-2008, 01:10 PM
  2. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  3. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 11:05 AM
  4. Problems with pointers and malloc()
    By Deirdre in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 04:20 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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