Thread: my malloc is stuck on "8" for some reason... why?

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    9

    my malloc is stuck on "8" for some reason... why?

    Hello,

    I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.

    If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.

    Why? I want it to output 1000. If I change that to 500, I want it to output 500.

    int *a;
    a = malloc(1000 * sizeof(int));

    int j = sizeof(&a);
    printf("%d", j);


    Thanks. I want to build my skills where I can allocate, inspect and change memory sizes.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Keep track of the size yourself, e.g.,
    Code:
    size_t size = 1000;
    int *a = malloc(size * sizeof(*a));
    
    printf("%u\n", (unsigned int)size);
    Your use of sizeof merely results in the size of the pointer in bytes, not the number of elements for which space was allocated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    9
    Thanks for your suggestion. However, so I know I am properly allocating memory, I want the computer to tell me the size by actually checking the size, not just recalling a pre-set variable. It's kind of like setting a thermostat in your house to 70 degrees Fahrenheit. I want the system to actually take the temperature of the room and read it back to me. Your approach is kind of like "just write the number 70 on a piece of paper, and then read your piece of paper later".

    But maybe your way the *only* realistic way to do what I'm talking about? I did some reading on the internet, and it saw things like this:
    "there is no way to do it. the only way is to create a variable and remember the variable"

    I find it strange that this will output 3:
    int a[] = {1, 2, 3};

    int j = sizeof(a) / sizeof(a[0]);
    printf("%d", j);


    But my earlier code likes to print out 8 no matter what. So I'll do it your way, because it seems like C has "no idea" how big the memory block is when you set up it's pointer.
    Last edited by c_seeker; 01-08-2014 at 03:12 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c_seeker
    However, so I know I am properly allocating memory, I want the computer to tell me the size by actually checking the size, not just recalling a pre-set variable.
    You can tell that you are properly allocating memory by checking that malloc did not return a null pointer. Okay, this is not guaranteed if the memory allocation strategy was overly optimistic, but generally this is supposed to be the case.

    Quote Originally Posted by c_seeker
    But I am looking to interrogate the system's memory stack/heap and have it report back to me that way.
    This cannot be done in standard C, so you would need to provide more details on say, your OS and compiler and wait for someone else with the necessary knowledge to explain what to do, if feasible. Also, because the memory allocated does not necessarily equal what you requested, you can forget about your requirement that "I want it to output 1000. If I change that to 500, I want it to output 500."

    EDIT:
    Quote Originally Posted by c_seeker
    I find it strange that this will output 3:
    Code:
    int a[] = {1, 2, 3};
    
    int j = sizeof(a) / sizeof(a[0]);
    printf("%d", j);
    Nothing strange about that: sizeof(a) results in the size of the array in bytes. sizeof(a[0]) results in the size of the first element in bytes. When you divide, you get the number of elements. Notice that you did not take the size of a pointer to the first element of the array.
    Last edited by laserlight; 01-08-2014 at 03:21 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    9
    Thank you so much for this clarification. It's very strange this C stuff! Here is my new code:
    Code:
        size_t i_want_this_much = 1000;
        int *a = malloc(i_want_this_much * sizeof(*a)); 
        
        if (a == NULL) {
            printf("you asked for too much memory. good bye\n");
            exit(EXIT_FAILURE);
        }
        
        printf("%u\n", (unsigned int)i_want_this_much);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-02-2013, 09:21 PM
  2. "Expressions must have (pointer-to-)" I'm stuck. :(
    By Shmamy in forum C++ Programming
    Replies: 13
    Last Post: 12-07-2011, 12:58 PM
  3. how can i pass by reference by "malloc 2d array"?
    By Mathsniper in forum C Programming
    Replies: 10
    Last Post: 05-22-2005, 02:23 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM