so i keep getting some seg fault issue due to the realloc in my program. I'm reallocing an array of layer_t structs, which is in a function to add an empty cake layer that's being called by another function layer_t add_fresh_layer (cake_t *cake) ( (...) cake->layers= realloc(cake->layers, sizeof(layer_t) * cake->layer_height * 2); (...) }   int add_filling_layer (cake_t *cake){ for (int i = 0; i < cake->cake_height; i++) { layer_t *layer = &cake->layers[i]; (...) for (j = 0; layer->ingred[j] != NULL; j++) { char *ingredients = layer->ingred[j];] (...) for (k = 0; k < bake->layer_count; k++) { (...) } if (...) { (...) layer_t *filling = add_fresh_layer(bake); (...) } } }} I know that the realloc is causing the address to change while the pointer for cake->layers[i] remains the same. And i know the fix here is super simple, but I can't for the life of me remember what it is

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
Question

so i keep getting some seg fault issue due to the realloc in my program. I'm reallocing an array of layer_t structs, which is in a function to add an empty cake layer that's being called by another function

layer_t add_fresh_layer (cake_t *cake) (
(...)
cake->layers= realloc(cake->layers, sizeof(layer_t) * cake->layer_height * 2);
(...)
}
 
int add_filling_layer (cake_t *cake){
for (int i = 0; i < cake->cake_height; i++) {
layer_t *layer = &cake->layers[i];
(...)
for (j = 0; layer->ingred[j] != NULL; j++) {
char *ingredients = layer->ingred[j];]
(...)
for (k = 0; k < bake->layer_count; k++) {
(...)
}
if (...) {
(...)
layer_t *filling = add_fresh_layer(bake);
(...)
}
}
}
}

I know that the realloc is causing the address to change while the pointer for cake->layers[i] remains the same. And i know the fix here is super simple, but I can't for the life of me remember what it is

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Hash Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning