c - Pointer Content was deleted after function call, why? -
i've been trying figure out what's going on code, no luck. have defined pointer: char ** fileslist = readdir(); pointer pointing array of strings. able have filenames in current directory. that's not issue.
when pass fileslist[i], i=0,..,n : readimage(fileslist[i],...); content of fileslist erased.. don't know why.. help?!
my code:
#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <syslog.h> #include <sys/stat.h> #include <time.h> #include <string.h> #include <unistd.h> #include <limits.h> #include <dirent.h> #include <jpeglib.h> unsigned char *readimage(//struct jpeg_decompress_struct cinfo, char *inputpath, int *width, int *height, int *pixel_size) { char *inputfilename; struct jpeg_decompress_struct cinfo; unsigned char *buffer; int rc, i, j; struct stat file_info; unsigned long jpg_size; unsigned char *jpg_buffer; struct jpeg_error_mgr jerr; int row_stride; inputfilename = (char *)malloc(1 + strlen(inputpath)); strcpy(inputfilename, inputpath); rc = stat(inputfilename, &file_info); if (rc) { syslog(log_err, "failed stat source jpg"); exit(exit_failure); } jpg_size = file_info.st_size; jpg_buffer = (unsigned char*) malloc(jpg_size + 100); int fd = open(inputfilename, o_rdonly); = 0; while (i < jpg_size) { rc = read(fd, jpg_buffer + i, jpg_size - i); += rc; } close(fd); cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); // <-- @ point, inputpath's content erased ... jpeg_mem_src(&cinfo, jpg_buffer, jpg_size); rc = jpeg_read_header(&cinfo, true); if (rc != 1) { syslog(log_err, "file not seem normal jpeg"); exit(exit_failure); } jpeg_start_decompress(&cinfo); *width = cinfo.output_width; *height = cinfo.output_height; *pixel_size = cinfo.output_components; unsigned long bmp_size = cinfo.output_width * cinfo.output_height * cinfo.output_components; buffer = (unsigned char*)malloc(bmp_size); row_stride = cinfo.output_width * cinfo.output_components; while (cinfo.output_scanline < cinfo.output_height) { unsigned char *buffer_array[1]; buffer_array[0] = buffer + (cinfo.output_scanline) * row_stride; jpeg_read_scanlines(&cinfo, buffer_array, 1); } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); free(jpg_buffer); return buffer; } char **readdir(int *nof) { char **fileslist; int numfiles = 0; int size = 256; dir *d; struct dirent *dir; d = opendir("."); if (d) { fileslist = (char **)malloc(size); while ((dir = readdir(d)) != null) { if (strstr(dir->d_name, ".jpeg")) { printf("%s\n", dir->d_name); fileslist[numfiles] = (char *)malloc(256); fileslist[numfiles++] = dir->d_name; size += 256; fileslist = realloc(fileslist, size); } } closedir(d); } *nof = numfiles; return fileslist; } int main(int argc, char *argv[]) { unsigned char *buffer; char **fileslist; int numberoffiles; int width; int height; int pixel_size; int i; fileslist = readdir(&numberoffiles); (i = 0; < numberoffiles; ++i) { printf("file: %s\n", fileslist[i]); // <-- here prints nothing in second round buffer = readimage(fileslist[i], &width, &height, &pixel_size); } //free(temp); free(fileslist); return exit_success; }
check this:
jpeg_create_decompress(&cinfo); // <-- @ point, inputpath's content erased ... // after point, filenames erased
if want run code, download library jpeglib 8c then, export folder's path path & c_include_path then, compile with:
gcc file.c -ljpeg
the directory enumeration function incorrect:
- you allocate memory directory entry, instead of copying string that, overwrite pointer allocated
dir->d_name
, contents may clobbered furtherreaddir
calls. - your allocation scheme imprecise , fail long filenames.
here corrected version:
char **readdir(int *nof) { char **fileslist = null; int numfiles = 0; dir *d; struct dirent *dir; d = opendir("."); if (d) { while ((dir = readdir(d)) != null) { if (strstr(dir->d_name, ".jpeg")) { printf("%s\n", dir->d_name); fileslist = realloc(fileslist, (numfiles + 1) * sizeof(char*)); fileslist[numfiles++] = strdup(dir->d_name); } } closedir(d); } *nof = numfiles; return fileslist; }
note readdir()
should take path argument, , strstr
not precise way match fie extensions: list.jpeg.txt
mistakenly included in list.
Comments
Post a Comment