I need to make a program for bakery managment, I've done structures and three pointer arrays that must contain pointers for object of these structures. But i can't make function to add new bakery because it needs dynamic memory allocating. I've tried to do this but it throws Segmentation Fault on realloc. I would be grateful for any advice how to properly reallocate memory for these arrays to add element. Also feel free to make comments about other errors in the code, I'm just learning.
typedef struct BakeryType {
char *name;
} BakeType;
typedef struct Bakerys {
char *name;
BakeType *type;
char *photo;
float weight;
int portions;
float price;
char *description;
} Bakery;
Bakery *bakeryList[0];
BakeType *bakeTypeList[0];
void addBakery() {
Bakery new;
*bakeryList = realloc(*bakeryList, (sizeof(bakeryList)/ sizeof(Bakery))+ 1);//Segmentation Fault
bakeryList[sizeof(bakeryList)/sizeof(Bakery)]=&new;
}
bakeryList is a zero-element array of pointers to Bakery. It has room for zero pointers.
Yet later you set the first element of this array (*bakeryList which is the same as bakeryList[0]) to whatever comes back from realloc. So you're overwriting something, and it probably goes downhill from there.
I think you want bakeryList to just be a pointer to Bakery. That's how dynamically-allocated arrays work in C: you define a pointer to the first element and use pointer math (e.g., bakeryList[5] or *(bakeryList + 5)) to access other elements.
Another issue is your use of sizeof(bakeryList). sizeof is an operator that's evaluated by the compiler. It doesn't change at runtime. sizeof(bakeryList) / sizeof(Bakery) will evaluate to zero because you defined bakeryList as a zero-element array. You need another variable to keep track of how many elements are actually in the array at runtime.
Something like this would work:
int bakeryCount = 0;
Bakery *bakeryList = NULL;
void addBakery() {
// Add one to the array.
bakeryCount++;
bakeryList = realloc(bakeryList, bakeryCount * sizeof (Bakery));
// Create a pointer to the new element at the end of the array.
Bakery *newBakery = bakeryList + bakeryCount - 1;
// Set all the fields. Note that they will probably contain
// garbage so you should set them all.
newBakery->name = ...
}
Related
hey I am trying to create a program in which I am trying store elements from one array to another with the use of a pointer to pointer but the problem is that is caused undefined behavior I believe that the problem is that I do not pass the elements in members with a proper way
I know it is a vague way of doing this but It is in only for practising reasons
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student{
char *name;
int *number;
}T;
int main(void) {
char array[10][100]={"araaaa","bbgt","gffkghgh"};
T arr[10][100];
T *p;
T **p1;
p=&arr[0][0];
p1=&p;
int i=0;
for(i = 0 ; i < 3 ; i++)
{ p=arr[i];
strcpy((*p1)->name,array[i]);
}
/*******print_elements*************/
for(i = 0 ; i < 3 ; i++)
{ p=arr[i];
printf("\n the elements are %s",(*p1)-> name);
}
return 0;
}
When you do this:
strcpy ((*p1)->name, array[i]);
(*p1)->name is an uninitialised pointer. What happens, therefore, is in the lap of the gods.
The easiest fix is to modify your student structure such that name is a buffer, rather than a pointer. At the same time, change number to an int, rather than a pointer to an int::
typedef struct student{
char name [100];
int number;
} T;
If you want to keep name as a pointer then you have to allocate some memory before you store your string in it. This should work:
(*p1)->name = strdup (array[i]);
Don't forget to free the memory when done.
T is made of of two pointers, this first one points to a string of characters in memory.
arr is a 2D array that is allocated to store a total of 1000 T structures.
arr[i] would reference a 1D array of T structures within arr
*p1 would essentially be arr[i], since dereferencing p1 gives you p, which was just set to arr[i]. So, that is not a pointer to a T structure, but to an array of T structures. Forcing the cast will likely give you a reference to the first T structure in that row, however.
->name This value is never set. You allocated an array, but "name" is a pointer to memory, not an array of characters, so '->name' is undefined.
I think you need to change arr to be a single dimension array. You aren't using 90% of it.
And, you need to initialize every T struct in that array. You can use malloc or strdup, and then remember to free them all. Or, set the struct to use an array instead.
I have typdef of string in my C program, it looks like that:
#define WRD_LEN 100
typedef char cstring[WRD_LEN];
then at some point I declare dynamic array of this type:
int pcount = 1;
cstring *options = malloc(sizeof(cstring*)*pcount);
I add new strings to this array with use of realloc:
options = realloc(options, sizeof(cstring*)*pcount);
strcpy(options[pcount-1], //some string//);
pcount++;
and then show all entries to user, user choses one of them and that one is passed to another function:
highestof(mode, l, options[btn]);
mode is an integer, l is struct but those are irrelevant now. btn is number (int) of entry chosed by user.
Up to this point everything works just fine, problem shows up inside highestof function:
void highestof(const int mode, List l, const cstring cat) {
List *p = malloc(sizeof(List*));
here is definition of List:
struct W {
//some data
struct W *next;
};
struct List {
struct W *first;
};
So if highestof function is called with options[1], cat variable will get corrupted (it will became a set of few random symbols, like "#^?" or "^K^?") right after malloc is called i.e. before creating dynamic array of List I can use cat as much as I want, but after calling malloc it gets corrupted. Most strange thing about this is that it happens only if variable passed down to this function was in options array under index of 1 (options[btn] where btn = 1) For any other value of btn it works no problem.
I found a workaround for this, I can create a string (char s[100]) before calling malloc, copy cat value into it and use that variable instead, but it's really not resolving initial problem and it really bothers me.
sizeof(cstring*)*pcount is too small. The size calculation is amiss.
Avoid allocation errors. Use this idiom for code that is easier to write correctly, review and maintain.
Notice no type is used.
pointer = malloc(sizeof *pointer * n);
Then code becomes:
// options = malloc(sizeof(cstring*)*pcount);
options = malloc(sizeof *options * pcount);`
cstring* is just a pointer, usually four or eight bytes. sizeof (cstring*) is therefore a small number, usually four or eight.
You are not allocating enough memory for the data, just enough memory to hold pointers to the data.
first of all, I know that build array of elements without pointers it's bad coding but its part of the exercise which I can't change
edit :
typedef struct st
{
int id;
char *name;
int platforms;
} *Station;
i built an array of elements (structs) -
int num=X
Station statTable= malloc((num)*(sizeof(*statTable)));
and initialize it -
int i=0
while(i<num){
station=TS->stations+i*sizeof(*station);
station->name="blabla";
station->id=id;
i++
}
I check the array after initializing and its work, but after some iteration on the array -
int i=0;
while(i<num){
station=TS->stations+i*sizeof(*station);
printf("%d",station->id);
i++
}
when TS is big struct with pointer to station which is name is stations
I get a bad value ( print screen below)and it always in the second cell, someone has an idea why it happened?
Typedefing pointers isn't really good practice.
Bug in your code is in pointer arithmetics. More precisely here
station=TS->stations+i*sizeof(*station);
As we know adding 1 to pointer will move us in memory not one byte, but sizeof(typeItIsAppliedOn) bytes, in this case sizeof(Station) bytes.
So what you have written will move you outside of bounds pretty soon and as we know, accessing outside of bounds has undefined behavior.
This should fix it
station = TS->stations + i; // This will point to i-th station in memory
In general, you could use this semantics for better readbility
station = &TS->stations[i]; // Get an address of i-th element in array
I have the following structs as example:
#define MAX_PEOPLE 16
typedef struct {
int age;
}Person;
typedef struct {
Person *people;
int numPeople;
}Team;
I'm trying to allocate an array of persons in a function, passed by parameters. My Team is supposed to store an array of 16 pointers of Person. I can't figure out what I'm doing wrong.
void initiateTeam(Team * team){
team->numPeople = MAX_PEOPLE;
Person *p[MAX_PEOPLE];
for(int i=0; i<MAX_PEOPLE;i++){
p[i] = malloc(sizeof(Person);
}
team->people = &p[0];
}
I printed out the addresses of my team->people[i] and I'm getting random junk. Why is the assingment team->people = &p[0] wrong? Shouldn't it get the first address of my array then perform pointer arithmetic?
You are pointing team->people to a statically defined array of person pointers. Once the function ends, the stack pointer moves back to where main left off, erasing all previously local memory in the addPeople function. You need to malloc p, and return it from the function
Since in the comments you stated that you're trying to allocate an array of Person objects and not pointers, you should rather do:
void addPeople(Team * team){
team->numPeople = MAX_PEOPLE;
team->people = malloc(sizeof(Person) * MAX_PEOPLE);
}
mind that there's no * in sizeof since you don't want an array of pointers but of objects. You will later be able to access the single elements (i.e. each Person object) with
team->people[2].age = 25; // The third person in the array
Finally, remember to free your memory.
your variable p is allocated in the stack of the addPeople() function. The assignment team->people = &p[0] (which is equivalent to team->people = p) is valid but dangerous because that address will be invalid as soon as the function is finished.
Better create p with malloc(sizeof (Person *) * MAX_PEOPLE) instead of using the stack.
The problem is here:
Person *p[MAX_PEOPLE];
This allocates a local variable in the function to hold the array of people pointers.
You get the address of this local variable and send it back. As soon as you are not in the function the local data is freed. It was just allocated locally. It is no longer valid. I might work for a while or it might not depending on the the program does next.
You want this:
Person **p = (Person **)malloc(sizeof (Person *) * MAX_PEOPLE);
I think you are getting the thing with pointer and memory management in C a little bit wrong. Consider reading a little bit more about it, before you continue coding your application.
Beside, I think you do not need an array of pointers to persons, but an array of persons.
Your struct is correct, but I would implement your function like that:
void addPeople(Team * team){
team->numPeople = MAX_PEOPLE;
team->people = malloc(sizeof(Person) * MAX_PEOPLE);
}
And do not forget to free() your team->people.
But if MAX_PEOPLE is an pre processor define, then it is totally unnecessary to use memory from the heap. If you are not storing too many people in your struct, the stack can easily fulfill your requirements.
If I create a struct in C and want to add them to an array that is not set to a fixed size, how is the array created?
Can one create a tempStruct which is used on every iteration while getting user input and store this in an array, always using the same tempStruct struct in the loop?
How is an array created if the size is unknown as it depends on user input, and how are structs added to this array?
When the size is unknown at compile time, you'll need to allocate the memory on the heap, rather than in the data segment (where global variables are stored) or on the stack (where function parameters and local variables are stored). In C, you can do this by calling functions like malloc.
MyStructType *myArray = (MyStructType *)malloc(numElements * sizeof(MyStructType)
... do something ...
free(myArray)
If you're actully using C++, it's generally better to use new[] and delete[], e.g.
MyStructType *myArray = new MyStructType[numElements]
... do something ...
delete [] myArray
Note that new[] must be paired with delete[]. If you're allocating a single instance, use new and delete (without "[]"). delete[] and delete are not equivalent.
Also, if you're using C++, it's generally easier and safer to use an STL vector.
the C array must be with fixed size this is what we have learned years ago
but memory allocation functions may help you to find a solution
in c++ you can use the operator new
int num=0;
cout<<"please enter the number"<<endl;
cin>>num;
int *x=new int[num];
for(int i=0;i<num;i++)
{
cout<<"enter element "<<(i+1)<<endl;
cin>>x[i];
}
//do your work
and as
Mr Fooz
mentioned delete[] is used to free the memory allocated by new[]
and this is a general example
If you are using the older C89 standard compiler, you cannot use variable length arrays. If you use C99 then you can create variable length array. For clarification: variable-lenght doesnt mean that the array lenght can change during execution. It just means that you can set it during execution instead of fixing a value during compile time.
For eg:
CreateArray(const int numberOfElements)
{
struct MyStruct arrayOfStructs[numberOfElements];
}
This is valid in C99 but not in C89. Check your compiler documentaion.
Yes, you can use a tempStruct during input which you add later to the array.
If the size of the array is unknown, then you are in trouble. You must keep track of the array's size somewhere. Just have an integer variable that you change every time you change your array to keep track of your array's size.
If the size of the struct is not known at compile time it is even more complicated. You either just store Pointers in the array which point to your actual struct elements in memory, or you have to keep track of the sizes of every struct in the array. In the later case you would have to do addressing in the array completely manually calculating a lot. While this is very memory efficient, it is also error prone and extremely hard to debug.
OK. sample to create an array that hold your struct using pointers:
struct MyStruct
{
/* code */
}
main()
{
counter = 0;
struct MyStruct** array = malloc(counter);
// Create new Element
struct MyStruct myElement;
myElement.someData = someValue;
// Add element to array:
array = realloc(array, sizeof(struct MyStruct*) * (counter + 1);
array[counter] = &myElement;
counter++;
// Create yet another new Element
struct MyStruct myElement;
myElement.someData = someOtherValue;
array = realloc(array, sizeof(struct MyStruct*) * (counter + 1);
array[counter] = &myElement;
counter++;
// Now remove the last element
free(array[counter -1]); // may have something more complicated than that, depending on your struct
array = realloc(array, sizeof(struct MyStruct*) * (counter - 1);
counter--;
}
this code is not tested!