I am a beginner learning C. From what I've learned and read here for example, you use malloc() when you don't know the amount of memory required at compile time. I am writing code in which I know the length of the string I am declaring but I get an error if I don't use malloc() to allocate it memory and I don't understand why.
The code I am writing is for a Harvard CS50 problem set in which the goal is to recover deleted jpgs but I've stripped it down to show just this problem. My code shows two lines declaring char* filename with the one that causes the error commented out. Both options compile, the one without malloc() causes a UndefinedBehaviorSanitizer:DEADLYSIGNAL error at run-time.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// remember filenames
char *infile = argv[1];
//open input file
FILE *inptr = fopen(infile, "r");
//buffer
BYTE buffer[512];
//read first 512 byte block in to buffer
size_t r = fread(&buffer, 1, 512, inptr);
int n = 0;
//get name for new jpg file
//char *filename[8]; //<<<<<<<<<<<<<<<<<<<<<this causes error
char *filename = malloc(8);
sprintf(filename, "%03i.jpg", n);
}
Here's the error:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==5563==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7fb433fba0ac bp 0x000000000030 sp 0x7ffd5af8a290 T5563)
==5563==The signal is caused by a WRITE memory access.
==5563==Hint: address points to the zero page.
#0 0x7fb433fba0ab (/lib/x86_64-linux-gnu/libc.so.6+0x900ab)
#1 0x7fb433fb8493 (/lib/x86_64-linux-gnu/libc.so.6+0x8e493)
#2 0x7fb433faa37d (/lib/x86_64-linux-gnu/libc.so.6+0x8037d)
#3 0x7fb433f86f1f (/lib/x86_64-linux-gnu/libc.so.6+0x5cf1f)
#4 0x7fb433fab6d0 (/lib/x86_64-linux-gnu/libc.so.6+0x816d0)
#5 0x7fb433f8f093 (/lib/x86_64-linux-gnu/libc.so.6+0x65093)
#6 0x428049 (/home/ubuntu/pset3/recover/malloc+0x428049)
#7 0x7fb433f4bb96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#8 0x402ad9 (/home/ubuntu/pset3/recover/malloc+0x402ad9)
UndefinedBehaviorSanitizer can not provide additional info.
==5563==ABORTING