So im currently trying to get my program to read a user input ( a double), and store said input in the heap using pointers in C. I allocate 40 bytes to the heap, and the program runs fine up until the program prompts me to enter an input to be stored, At which point after providing the first input, I get the segmentation fault. I know it may come as an out of bounds error, or an error with improper dereferencing, but I can't quite find where I did that. Any help would be greatly appreciated.
Edit: It's become clear my question was a bit confusing, so some clarifications: The goal of the program is to take an unspecified number of grades, starting with an initial heap size 40 bytes (project requirement), and whenever the heap is full, it is copied into a new one of size 80 bytes, with the old 40 bytes being freed. This continues until all grades have been entered.
Placing count as a parameter to defAlloc was a mistake, not quite sure why I put that there to begin with but that his since been removed.
The allocation aspect of this has been fixed and now works properly, im mainly having an issue with accessing elements within the heap and altering them ( grade Scanner Method)
Sorry for the messy code or confusing wordings, im new to both pointer usage and arithmetic as well as posting on stack overflow. Thank you for the responses.
an example of input and output: Input: 81.0 43.0 25.0 73.5
Output: Allocated 40 Bytes to the heap at 0x8d8010 stored 81 in the heap at 0x8d8010
This Repeats for each value until full heap or reaching a sentinel value.
I don't want to give away a whole bunch of information regarding input/output and the project as this is a school assignment (For academic dishonesty purposes) but essentially the issue im having is coming up in the gradeScanner method where im trying to read input and assign it to ptr, ptr+1, etc. Whatever it may be something is causing a segmentation fault
void defAllocator(double **grades);
void gradeScanner(int gradeCount, int allocCount, double *grades, double myGrade);
int main() {
int allocCount;
int gradeCount;
double myGrade;
double *grades;
printf("Enter a list of grades below where each grade is seperated by a newline Character");
printf("After the last grade is entered, enter a negative value to end the list");
defAllocator(&grades);
if(grades == NULL) {
printf("null");
}
else
printf("hello");
gradeScanner(gradeCount, allocCount, grades, myGrade);
}
void defAllocator(double **grades) {
double *arr = (double*)malloc(40);
if(arr != NULL) {
printf("Allocated 40 bytes at the heap at %p\n", grades);
}
else
printf("failed to allocate array");
*grades = arr;
}
void gradeScanner(int gradeCount, int allocCount, double *grades, double myGrade) {
int i =0;
while(i != 5){
if(scanf("%f", myGrade) > 0 && gradeCount == 0) {
*grades = myGrade;
gradeCount++;
printf("%p\n", grades);
printf("%p\n", gradeCount);
i++;
}
else if(scanf("%f", myGrade) < 0) {
i = 5;
}
else if(scanf("%f", myGrade) > 0 && gradeCount > 0) {
*(grades + gradeCount) = myGrade;
}
}
}