Results 1 to 8 of 8

Thread: ahhh...that dp++

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    ahhh...that dp++

    PHP Code:
    typedef struct 


    char s1[90]; 
    int I,j,k

    data_t
    data_t data, *dp
    strcpy(data.s1"hi there"); 
    data.i=1
    data.j=2
    data.k=3

    dp=malloc(sizeof(data_t)*100); 
    strcpy(dp->s1"bye know"); 
    dp->I=7
    dp->j=8
    dp->k=9

    dp++; //[COLOR=red]See below[/COLOR]
    memcpy(dp, &datasizeof(data_t)); 
    Does "dp++;" create another pointer to data_t structure, so then you have another empty pointer to data_t?
    Baaaaaaaaah

  2. #2
    Zaei
    Guest
    No, it increments the pointer by either the sizeof(dp), or by 1(dont remember). This will probably make the pointer invalid, so dont do it =).

    Z.

  3. #3
    Zaei
    Guest
    Thanks. A while back I was writing a class to total the amount of memory allocated during the program's execution, but couldnt call the constructor. I eventually just overloaded "new" to call my class's functions, and it works fine.

    Z.

  4. #4
    jim mcnamara
    Guest
    oh.

    I use atexit(release) - then maintain a vector of pointers to each mem block malloc made and free() them in release().

    Same idea.

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    I think something is wrong with his malloc(), should be,


    dp=(data_t * )malloc(sizeof(data_t)*100);

    before dp++; will work.

    Or else how C will know how much to increment. By 1 int? by 1 char or by one data_t unit?
    I'm a VB6 beginner.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    trans: it is valid C code (but not C++ since C++ wants explicit type casts when casting void pointers (void*) to other pointers). The compiler knows how much to increment because dp is:
    data_t *dp;
    C wouldn't care if he used
    dp = malloc(11200);
    (112 is the size of the structure and he wants 100 of them).
    Actually, since sizeof is resolved by the compiler, it is exactly the same, except that if he one day decides to add a member to the struct, he'd have recalculate everything.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    amac
    Guest
    It increments the pointer by the size of what it is pointing to.

    take a looksie...

    long * pLong = malloc ( sizeof ( long ) * 100 );
    char * pChar = (char *) pLong;

    pLong++; // will increment the pointer by sizeof ( long )
    pChar++; // will increment the pointer by sizeof ( char )


    With VC++ you can not do this with a pointer to void. Not sure if this is a common thing or if its just the compiler I am using. Gives an error.


    CornedBee: I'm not sure if this is what you just said or not... but I thought I would throw in my two-cents worth.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    yeah, it is, but I don't care
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width