Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Pointer arithmetic problem in c programming

  1. #1
    Join Date
    Oct 2013
    Beans
    9

    Pointer arithmetic problem in c programming

    First of all Happy New Year to all of you,

    I am working on assignments published by Harvard CS61 and i am facing an arithmetic problem in my pointer.

    ptr = malloc(x) // ie ptr = 0x804b008
    ptr += 3; // now ptr must be 0x804b011 but it is 0x804b014

    So please let me know the solution of this undetermined problem that why pointer arithmetic is wrong.

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,152
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Pointer arithmetic problem in c programming

    It depends on the type that ptr points to. If ptr is char * , you will get what you are expecting. If ptr is int * (and int is 4-bytes) then you will get what you are seeing.

  3. #3
    Join Date
    Oct 2013
    Beans
    9

    Re: Pointer arithmetic problem in c programming

    Thanz to replying my question but i want to say that it does not work
    i have tried pointer of type chat but still not doing arithmetic operation sucessfully

  4. #4
    Join Date
    Oct 2013
    Beans
    9

    Re: Pointer arithmetic problem in c programming

    // please look into the code below, these r code for memory allocator

    Code:
    unsigned long long update = 0;
    unsigned long long nactive = 0;
    unsigned long long active_size = 0;
    unsigned long long ntotal = 0;
    unsigned long long total_size = 0;
    unsigned long long nfail = 0;
    unsigned long long fail_size = 0;
    void *m61_malloc(size_t sz, const char *file, int line) {
        char *ptr = NULL;
        (void) file, (void) line;   // avoid uninitialized variable warnings
        if(ptr = malloc(sz))
        {
            ptr[0] = sz;
    //        printf("aaaaaaa%llutttttt%llusssssss%d", active_size,total_size,ptr[0]);
            ptr += 4;
            nactive++;
            active_size += sz;
            ntotal++;
            total_size += sz;
        }
        else
        {
            nfail++;
            fail_size += sz;
        }
        return ptr;
    }
    
    
    void m61_free(void *ptr, const char *file, int line) {
        (void) file, (void) line;   // avoid uninitialized variable warnings
        // Your code here.
        char *fptr = ptr;
        size_t sz;
        if(ptr)
        {
            fptr -= 4;
            sz = fptr[0];
    //            printf("aaaaaaa%llutttttt%llusssssss%d", active_size,total_size,sz);
            active_size -= sz;
            nactive--;
        }
        free(ptr);
    }
    Last edited by Iowan; January 5th, 2014 at 07:08 PM. Reason: added [code] tags

  5. #5
    Join Date
    Feb 2008
    Location
    In my skin.
    Beans
    Hidden!
    Distro
    Xubuntu

  6. #6

    Re: Pointer arithmetic problem in c programming

    The first thing that leaps out at me is that in m61_malloc, you obtain a pointer (ptr) with malloc(), then return (ptr + 4). But in m61_free, you call free(ptr), which is unallowable -- you can only pass to free() the exact pointer you got from malloc(). Perhaps you meant free(fptr)?

    I haven't tried to divine the meaning of everything in your code, but one other thing that might not do what you expect is the printf()s (the ones you have commented out) -- if you uncomment them, they will only print values in the range [-128,127]. Is that what you want?

    As for the pointer arithmetic, though, it all looks ok to me. Can you post a small compilable example that shows the incorrect behavior?

  7. #7
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,152
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Pointer arithmetic problem in c programming

    Quote Originally Posted by Ravi_Chander_Jha View Post
    Thanz to replying my question but i want to say that it does not work
    i have tried pointer of type chat but still not doing arithmetic operation sucessfully
    Really? Here's a demonstration that the information I gave you is correct.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char * p = malloc(10);
        int *  q = (int *) p;
    
        printf("p and q are the same %p,%p\n", p, q);
        p += 3;
        q += 3;
        printf("p and q are different %p,%p\n", p, q);
    
        return 0;
    }
    Output
    Code:
    p and q are the same 0x2170010,0x2170010
    p and q are different 0x2170013,0x217001c
    You can see that p has increased by an offset of 3 bytes, and q has increased by an offset of 12 bytes.

  8. #8
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,152
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Pointer arithmetic problem in c programming

    Quote Originally Posted by Ravi_Chander_Jha View Post
    // please look into the code below, these r code for memory allocator

    Code:
    unsigned long long update = 0;
    unsigned long long nactive = 0;
    unsigned long long active_size = 0;
    unsigned long long ntotal = 0;
    unsigned long long total_size = 0;
    unsigned long long nfail = 0;
    unsigned long long fail_size = 0;
    void *m61_malloc(size_t sz, const char *file, int line) {
        char *ptr = NULL;
        (void) file, (void) line;   // avoid uninitialized variable warnings
        if(ptr = malloc(sz)) //bug
        {
            ptr[0] = sz; //bug
    //        printf("aaaaaaa%llutttttt%llusssssss%d", active_size,total_size,ptr[0]);
            ptr += 4;  // non-portable. think in terms of sizeof(?)
            nactive++;
            active_size += sz;
            ntotal++;
            total_size += sz;
        }
        else
        {
            nfail++;
            fail_size += sz;
        }
        return ptr;
    }
    
    
    void m61_free(void *ptr, const char *file, int line) {
        (void) file, (void) line;   // avoid uninitialized variable warnings
        // Your code here.
        char *fptr = ptr;
        size_t sz;
        if(ptr)
        {
            fptr -= 4; // non-portable. think in terms of sizeof(?)
            sz = fptr[0];
    //            printf("aaaaaaa%llutttttt%llusssssss%d", active_size,total_size,sz);
            active_size -= sz;
            nactive--;
        }
        free(ptr); //bug
    }
    In addition to trent.josephsen's remarks, I've marked where I think there are faults in your code. I don't want to explain why at this point, as this is an assignment.
    Last edited by spjackson; January 5th, 2014 at 09:09 PM.

  9. #9
    Join Date
    Dec 2013
    Beans
    35

    Re: Pointer arithmetic problem in c programming

    Quote Originally Posted by Ravi_Chander_Jha View Post
    ptr = malloc(x) // ie ptr = 0x804b008
    ptr += 3; // now ptr must be 0x804b011 but it is 0x804b014
    Your first mistake:

    Since the address of ptr is expressed as a hexadecimal number

    0x804b008 + 3 does NOT equal 0x804b011

    0x804b008 + 3 equals 0x804b00b

    Hexadecimal arithmetic

  10. #10

    Re: Pointer arithmetic problem in c programming

    Quote Originally Posted by spjackson View Post
    Code:
        if(ptr = malloc(sz)) //bug
    I beg to differ -- I think the actual behavior is the intended behavior for this particular line. But I would rewrite it; it's far too easy to read wrong.

    (Unless you're thinking of a bug I overlooked.)

    Edit: I'm wrong, it does have a serious bug.
    Last edited by trent.josephsen; January 5th, 2014 at 11:04 PM.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •