views:

86

answers:

3

Edit: Forget it. it was another part of the code (actually the problem was a secondary element. another pointer but not allocated with malloc, just declared, so i think the memory was allocated in another place). the example now can compile.

thanks guys. im sorry for my typos but english isnt my native language(isnt a good escuse but i will try harder) .


hi. i want to pass the some elements (in a struct)to a function but i cannot read the elements in any way(seg faults)

#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>   
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "pcre.h"
#include <arpa/inet.h>
#define BUFFER 1512


typedef struct OCR {
    unsigned long int       ocr;
    struct OCR *         prev;
    struct OCR *         next;
} ip_ocr;


int sending (ip_ocr * tmp) {
    printf("%p\n",tmp); //this outpuut
    printf("%lu",tmp->ocr); // at this point i get a seg fault
    return 0;

}

int main () {
    ip_ocr * list;
    list=malloc(sizeof(ip_ocr));
    list->ocr=1;
    list->next=NULL;
    list->prev=NULL;

    sending(list);


 }    
+3  A: 

It worked on my machine after a few fixes.

Include system headers:

#include <stdio.h>
#include <stdlib.h>

Add semicolons after these lines:

printf("%lu",tmp->ocr);
return 0;

Added (unnecessary) typecast to value returned from malloc:

list=(ip_ocr*)malloc(sizeof(ip_ocr)); /* oops, not needed */
Blastfurnace
C does not require that type-cast.
Matthew Flaschen
Matthew is correct, the typecast of malloc's return value is not needed. I added it out of habit from using C++.
Blastfurnace
Not only that it is not needed, it obfuscates the error that the OP encountered, I guess. Since he didn't include the header, the return type got interpreted as an `int`. So the pointer that finally ended in `list` was completely off track. I think that every compiler nowadays gives a warning about that which must have been generously ignored by the OP.
Jens Gustedt
+1  A: 

I think that you must have ignored a warning that the return of malloc has been taken to be an int. Your list then goes completely wrong.

  • include the correct header files
  • always compile with -Wall or equivalent
  • improve your code until it doesn't spit out any warning at all
Jens Gustedt
We must conclude that the code was compiled as LP64 since,, had it been compiled as 32 bit code, accidentally treating a pointer as an int won't seg fault (in all real world C implementations I have ever used).
JeremyP
@JeremyP: yes, this was my guess, too. We also may conclude that we will see this kind of error more and more often.
Jens Gustedt
actually it was compiled in X86_64 and using bigfiles support and malloc wont give me any warnings (i always compile with -Wall)
Freaktor
A: 

Compiling with -Werror-implicit-function-declaration and -Werror=return-type prevents these kind of things happening in C.

Matt Joiner