Hope is the first step on the road to disappointment.
thanks, anduril. that seems to have fixed it (for the moment atleast lol) =D
i had it down in my notes that int=1 byte and char=4.
**EDIT
thanks also, quzah! that was def my problem
Last edited by TheReceptionist; 07-28-2011 at 04:16 PM.
getting a segmentation fault error that i havent been able to fix in 7 hours. ive looked stuff up and all it says is that it has to do with a reference to a memory location that it cant find/is non-existent, etc.
running gdb, i get that the problem occurs:
i know the problem occurs on the 4th loop but dont know why! X OCode:Program received signal SIGSEGV, Segmentation fault. 0x0000000000400e0d in main () at histogram3.c:45 45 printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]); (gdb) backtrace #0 0x0000000000400e0d in main () at histogram3.c:45 (gdb) frame 0 #0 0x0000000000400e0d in main () at histogram3.c:45 45 printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]); (gdb) print i $1 = 4 (gdb) print mem[i][0] Cannot access memory at address 0x7fff00e3898e
malloc doesnt come back null and it takes some values, so all i can think is that it has to do with my malloc.
Code:int main() { FILE *fp; int *p,width,height,size,i=0,*mem_loc; unsigned char pix[4],mem[height][4]; char *filename; filename="/cise/homes/melendez/Lenna.jpg"; size=loadJPG(filename,"/cise/homes/melendez/output.bin"); fp=fopen("/cise/homes/melendez/output.bin","rb"); if (fp==NULL) //pg547 example { printf("CANNOT OPEN FILE\n"); } else { fread(&width,sizeof(width),1,fp); //(char* str,int n,FILE* file) fread(&height,sizeof(height),1,fp); printf("DIMENSIONS(in pixels): %d x %d\n",width,height); mem_loc=malloc(sizeof(int)*mem[height][4]); if(mem_loc==NULL) printf("FAIL\n"); else { while(i<height) { fread(&pix,1,4,fp); printf("%d %d %d %d\n",pix[0],pix[1],pix[2],pix[3]); strcpy(mem[i],pix); printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]); i++; } } } //return mem_loc; }
Right now you are telling malloc to make enough memory (int *mem[height][4]) which is not how large your mem array is. The compiler evaluates this as to (int * <whatever is in the location of your array @[height][4]>) If you want it to allocate enough memory to hold your array you would need malloc(sizeof(int)*height*4).
ohhhh, i see what youre talking about! this is where we have to use the pointer as an array (dynamically allocated storage for arrays)?
If you're reading unsigned chars into pix/mem[][], why is mem_loc a pointer to an integer (or integer array)? Those types should be consistent.
Also, since you're reading an image that's of size width x height, I'd expect both of those terms to be needed to figure out how many pixels there are to be read in total. And since you need to store all of them, you'd probably need to include both terms in the malloc size calculation.
thanks, KCfromNC. i figured that out at 4am when i got tired of not knowing the pixel values. i opened adobe and literally looked what the first/last few pixels RGB values were and realized it was only alloc enough for 256.
Good deal. Now a couple more pointers.
You have an array which now has enough space to hold all of the pixel values. You can modify your code to read it in one fread() call if you like and have it end up directly in the array you malloc()ed. Should clean things up a bit.
That will also get rid of the strcpy() call, which is not at all what you want in this case. Even if you don't do the step I mention above, look into the difference between strcpy() and the more useful memcpy() function. More useful in this case, anyway.
Also, become familiar with the unix utility od. It stands for octal dump, but really it's a tool for dumping out binary files in a human readable format. It has lots of options, but one that may be useful for testing is "od -t x1 file.bin" to dump out each byte of file.bin as a hex value. You can use printf("%2.2x", array[i]) in your code to print the data being read there and compare it against what is really in the file. Should be helpful for debugging if you run in to problems.
ohhh... ok, i see what your saying about 1 fread for all of it. tweaked that part already and checked that the right information was being copied, etc. thanks!!
still having issues with returning *width,*height in the function. this is what i know
so, in "get_pixels" function, to have the argument 2 (int *width) contain the value of int w(which we get from the function) i need to first point *width to the address of w, assign the value of w to *width, THEN i can printf the value that w has?Code:int i,*p; p=&i; //p points to LOCATION of i *p=i; //*p now equals VALUE of i p=i; //not valid unless both are same types *p=2 //VALUE of 2 is stored at LOCATION of where p points, which is i (in this case)
i dont get why when i print *width in the MAIN func it doesnt match the *width i print in the get_pixel func
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include "/cise/homes/melendez/jpeg.h" unsigned char *get_pixels(char *filename, int *width, int *height) { FILE *fp; int i=0,size,w,h; unsigned char pix[4], *mem_loc; size=loadJPG(filename,"/cise/homes/melendez/output.bin"); fp=fopen("/cise/homes/melendez/output.bin","rb"); if (fp==NULL) //pg547 example { printf("CANNOT OPEN FILE\n"); } else { fread(&w,sizeof(w),1,fp); fread(&h,sizeof(h),1,fp); width=&w; height=&h; *width=w; *height=h; printf("\n%d %d\n",*width,*height); printf("DIMENSIONS: %d x %d\n",w,h); unsigned char mem[h*w][4]; mem_loc=malloc(sizeof(int)*h*w*4); printf("%d\n",*mem_loc); if(mem_loc==NULL) printf("FAIL\n"); else { while(i<(h*w)) { fread(&pix,1,4,fp); //printf("%d %d %d %d\n",pix[0],pix[1],pix[2],pix[3]); memcpy(mem[i],pix,sizeof(int)); //printf("%d %d %d %d\n\n",mem[i][0],mem[i][1],mem[i][2],mem[i][3]); //printf("%d\n",i); i++; } } printf("%d\n",i); } return mem_loc; } int main() { int *width,*height,w,h; char *filename; unsigned char *p; filename="/cise/homes/melendez/Lenna.jpg"; p=get_pixels(filename,width,height); printf("%d\n",*p); printf("%d\n",*width); }Code:thunder:15% ./load 256 256 DIMENSIONS: 256 x 256 0 65536 0 1
ok, NEVERMIND. i fixed it. someone else told me about passing ints to int *s; all you need is to add a &infront of the int being passed to an int*
one last question (FTW)
im having trouble with fwrite. im sure everything else works coz i printf-ed it every time, but i dont know why its not working. i know fwrite returns the number of successfully written ELEMENTS (if its in an array).
im trying to print the array[64] in groups of 4, so first line of output file would be array[0], array[1], array[2],array[3]. and the second line starts from array[4], etc....
Code:void write_histogram(int *histogram, int bins_per_color, char *filename) { FILE *fp; int i=0; fp=fopen(filename,"w"); if (fp==NULL) { printf("CANNOT OPEN FILE\n"); } else { //while(i<64) // { fwrite(histogram,sizeof(int),4,fp); // } } fclose(fp); }
What problem are you having? fwrite is really for binay data. If you tell it to print the int 123:...and you open the file in a text editor, you aren't going to see "123". "Lines" are really irrelevant when using fwrite. Sure I suppose you could work that into what you are doing somehow, but you don't generally do that.Code:fwrite( &myint, sizeof( myint ), 1, file );
If you want text, use fprintf.
Quzah.
Hope is the first step on the road to disappointment.