Thread: Debugging help, pointer forwarding

  1. #1
    Registered User
    Join Date
    May 2012

    Debugging help, pointer forwarding

    There's the code under, but I'm getting an invalid read size of 4, and it seems because I forwarded the pointer a little far ahead... I think.
    It's just a little part of my network code, but it's pretty easy to understand what I'm doing under right?

    LSP_buffer is a char *, that's why I have to cast it.

    allocated memory for a struct LSP and how many counts edges there is around this router(nodes).

    Not so sure if I thought it out right, by adding sizeof(struct LSP) to pointer it would mean forwarding the pointer by so many bytes as the size of struct right?, why then would I get "Invalid write of size 4" ?

    thanks for replying.

       LSP_buffer = malloc((sizeof(struct LSP) + (sizeof(struct EDGE) * count))); 
        lsp = (struct LSP*) LSP_buffer;                                        /*cast it to struct LSP and write in the necessary info about this LSP*/
        lsp->source = htonl(own);
        lsp->seq = htonl(LSP_generation++);                                    /*set which generation this LSP is, and increment it*/
        lsp->nodes = htonl(count);
        node = (struct NODE *) (LSP_buffer + sizeof(struct LSP));                /*cast LSP_buffer to struct EDGE and move the pointer forward so we don't overwrite any data*/
        fprintf(stderr, "Own address: %d LSP generation is %d\n", own , LSP_generation);
        /*store info about the edges around this router*/
        for(i = 0; i < MAX_ADDRESSES; i++){     
          if(distance[own][i].cost > 0){
        node->address = htonl(i);   <---------------------------    
        node->cost = htonl(distance[own][i].cost);
        node = node + sizeof(struct NODE);                                /*forward pointer by sizeof struct NODE so we don't overwrite old NODE data*/

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    I see two things.

    1. On line 6 you cast it as a (struct NODE *), but maybe it should be (struct EDGE *).

    2. On line 16, you add in sizeof(struct NODE). But since node is of type (struct NODE *), simply adding 1 will move it forward by sizeof(struct NODE) bytes. Instead you are moving it forward by sizeof(struct NODE *) * sizeof(struct NODE *) bytes!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2012
    Your're right, struct EDGE has only an int and NODE to have two integers, an ID number and cost, so NODE was twice the size of EDGE, no wonder I got invalid write size by 4 byte (integer -> 4 byte).
    and because it's pointer, simply adding 1 will move it forward, gotta go back to the basic:P.

    Thanks for the fast reply! Appreciate it.

    What happens with the thread now when my problem is solved?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP data forwarding
    By Labor in forum Linux Programming
    Replies: 3
    Last Post: 05-24-2011, 01:58 PM
  2. Forwarding connection
    By splintter in forum C Programming
    Replies: 1
    Last Post: 08-28-2010, 12:17 PM
  3. Forwarding client data
    By zacs7 in forum Networking/Device Communication
    Replies: 4
    Last Post: 02-16-2010, 11:15 PM
  4. Port Forwarding
    By cerin in forum Tech Board
    Replies: 1
    Last Post: 04-05-2007, 03:41 PM
  5. Port Forwarding DI-624
    By Tonto in forum Tech Board
    Replies: 0
    Last Post: 08-27-2006, 10:48 PM

Tags for this Thread