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.
Code:
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*/
}
}