C: Dynamically Allocated Struct Array Usage - c

Getting errors such as
stats.c:28:36: error: ‘factoryStats’ has no member named ‘candyConsumed’ factoryStatsArray[producer_number].candyConsumed++;
What I want to be able to achieve is to create an array of structs, then access it's members. Is this the wrong way to do it?
Tried using -> but that shouldn't and don't work since I'm storing structs, not pointers to structs.
#include "stats.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int factoryNumber = 0;
int candyProduced = 0;
int candyConsumed = 0;
double minDelay = 0;
double avgDelay = 0;
double maxDelay = 0;
} factoryStats;
factoryStats *factoryStatsArray;
int NUM_FACTORIES = 0;
void stats_init (int num_producers) {
factoryStatsArray = malloc(sizeof(factoryStats) * num_producers);
NUM_FACTORIES = num_producers;
}
void stats_cleanup (void) {
free(factoryStatsArray);
}
void stats_record_produced (int factory_number) {
factoryStatsArray[factory_number].candyProduced++;
}
void stats_record_consumed (int producer_number, double delay_in_ms) {
factoryStatsArray[producer_number].candyConsumed++;
if (factoryStatsArray[producer_number].minDelay == 0) {
factoryStatsArray[producer_number].minDelay = delay_in_ms;
} else {
if (factoryStatsArray[producer_number].minDelay > delay_in_ms) {
factoryStatsArray[producer_number].minDelay = delay_in_ms;
}
}
if (factoryStatsArray[producer_number].maxDelay == 0) {
factoryStatsArray[producer_number].maxDelay = delay_in_ms;
} else {
if (factoryStatsArray[producer_number].maxDelay < delay_in_ms) {
factoryStatsArray[producer_number].maxDelay = delay_in_ms;
}
}
factoryStatsArray[producer_number].avgDelay+= delay_in_ms;
}
void stats_display(void) {
printf("%8s%10s%10s10s10s10s\n", "Factory#", "#Made", "#Eaten", "Min Delay[ms]", "Avg Delay[ms]", "Max Delay[ms]");
for (int i = 0; i < NUM_FACTORIES; i++) {
printf("%8d%8d%8d%10.5f%10.5f%10.5f",
factoryStatsArray[i].factoryNumber, factoryStatsArray[i].candyProduced,
factoryStatsArray[i].candyConsumed, factoryStatsArray[i].minDelay,
factoryStatsArray[i].avgDelay/factoryStatsArray[i].candyConsumed,
factoryStatsArray[i].maxDelay);
}
}

structs cannot be initialized this way. Remove all those = 0 in typedef struct { ... } factoryStats;. Afterwards it compiles as in http://ideone.com/uMgDzE .

Related

Segfault in Merge - Sort in C

I am trying to sort an array of structures of size 5500 using merge sort.
However, I am getting a segmentation fault pretty quickly because I am not allowed to use VLA. so I have to create 2 extra arrays of size 5500 each time I call merge-sort recursively.
I would appreciate a fix for my problem. I will provide my code here:
void merge(Student rightArr[], Student leftArr[], Student mergedArr[], int sizeOfRight, int sizeOfLeft) {
int rightArrIndex = 0;
int leftArrIndex = 0;
int mergedArrIndex = 0;
while (leftArrIndex < sizeOfLeft && rightArrIndex < sizeOfRight) {
char *ptrLeft, *ptrRight;
long gradeLeft = strtol(leftArr[leftArrIndex].grade, &ptrLeft, BASE_COUNT);
long gradeRight = strtol(rightArr[rightArrIndex].grade, &ptrRight, BASE_COUNT);
if (gradeLeft > gradeRight) {
mergedArr[mergedArrIndex] = rightArr[rightArrIndex];
rightArrIndex++;
} else {
mergedArr[mergedArrIndex] = leftArr[leftArrIndex];
leftArrIndex++;
}
mergedArrIndex++;
}
if (leftArrIndex == sizeOfLeft) {
for (int i = mergedArrIndex; i < (sizeOfLeft + sizeOfRight); i++) {
mergedArr[i] = rightArr[rightArrIndex];
rightArr++;
}
} else {
for (int i = mergedArrIndex; i < (sizeOfLeft + sizeOfRight); i++) {
mergedArr[i] = leftArr[leftArrIndex];
leftArr++;
}
}
}
void mergeSort(Student studentsArray[], int amountOfStudents) {
if (amountOfStudents <= 1) {
return;
}
int leftSize = (amountOfStudents / 2);
int rightSize = (amountOfStudents - leftSize);
Student leftArr[5500], rightArr[5500];
for (int i = 0; i < leftSize; i++) {
leftArr[i] = studentsArray[i];
}
for (int i = 0; i < rightSize; i++) {
rightArr[i] = studentsArray[i + leftSize];
}
mergeSort(leftArr, leftSize);
mergeSort(rightArr, rightSize);
merge(rightArr, leftArr, studentsArray, rightSize, leftSize);
}
Ok, I think this should do what you want. It assumes that Student and BASE_COUNT have been defined:
#include <stdlib.h>
#include <stdio.h>
void merge(Student studentsArr[],
int leftSize, int rightSize,
Student scratchArr[])
{
Student *leftArr = studentsArr;
Student *rightArr = studentsArr + leftSize;
int leftIx = 0, rightIx = 0, mergeIx = 0, ix;
while (leftIx < leftSize && rightIx < rightSize) {
long gradeLeft = strtol(leftArr[leftIx].grade, NULL, BASE_COUNT);
long gradeRight = strtol(rightArr[rightIx].grade, NULL, BASE_COUNT);
if (gradeLeft <= gradeRight) {
scratchArr[mergeIx++] = leftArr[leftIx++];
}
else {
scratchArr[mergeIx++] = rightArr[rightIx++];
}
}
while (leftIx < leftSize) {
scratchArr[mergeIx++] = leftArr[leftIx++];
}
// Copy the merged values from scratchArr back to studentsArr.
// The remaining values from rightArr (if any) are already in
// their proper place at the end of studentsArr, so we stop
// copying when we reach that point.
for (ix = 0; ix < mergeIx; ix++) {
studentsArr[ix] = scratchArr[ix];
}
}
void mergeSortInternal(Student studentsArray[],
int amountOfStudents,
Student scratchArr[])
{
if (amountOfStudents <= 1) {
return;
}
int leftSize = amountOfStudents / 2;
int rightSize = amountOfStudents - leftSize;
mergeSortInternal(studentsArray, leftSize, scratchArr);
mergeSortInternal(studentsArray + leftSize, rightSize, scratchArr);
merge(studentsArray, leftSize, rightSize, scratchArr);
}
#define MAX_ARR_SIZE 5500
void mergeSort(Student studentsArray[], int amountOfStudents)
{
if (amountOfStudents <= 1) {
return;
}
if (amountOfStudents > MAX_ARR_SIZE) {
fprintf(stderr, "Array too large to sort.\n");
return;
}
Student scratchArr[MAX_ARR_SIZE];
mergeSortInternal(studentsArray, amountOfStudents, scratchArr);
}
The top-level sort function is mergeSort, defined as in the original post. It declares a single scratch array of size MAX_ARR_SIZE, defined as 5500. The top-level function is not itself recursive, so this scratch array is only allocated once.

Create variable amount of struct objects inside a struct (C)

I haven't written any C for more than a decade, but here I am...
I want to be able to create and access the following items of a data structure:
FilterCoefficients[0].TargetSampleNum = 0;
FilterCoefficients[0].SourceWeights[0].Weight = 0.812;
FilterCoefficients[0].SourceWeights[0].SourceSampleNum = 0;
FilterCoefficients[0].SourceWeights[1].Weight = 0.153;
FilterCoefficients[0].SourceWeights[1].SourceSampleNum = 1;
FilterCoefficients[1].TargetSampleNum = 1;
FilterCoefficients[1].SourceWeights[0].Weight = 0.352;
FilterCoefficients[1].SourceWeights[0].SourceSampleNum = 0;
FilterCoefficients[1].SourceWeights[1].Weight = 0.721;
FilterCoefficients[1].SourceWeights[1].SourceSampleNum = 1;
[...]
The indices have to be dynamically allocated (amount of needed space changes during runtime). I am attempting to create said data structure with the following:
typedef struct SampleWeight_t
{
unsigned long SourceSampleNum;
double Weight;
} SampleWeight;
typedef struct FilterCoefficients_t
{
unsigned long TargetSampleNum;
SampleWeight* SourceWeights;
} FilterCoefficients;
However, I am having difficulties creating the structure. I am getting Break Point exceptions when malloc-ing or free-ing the structure.
FilterCoefficients* FilterCoefficients;
SampleWeight* SampleWeight;
FilterCoefficients = malloc(sizeof(FilterCoefficients) * target_width);
if (FilterCoefficients == NULL) {
//errhandler
}
for (int i = 0; i < target_width; i++) {
FilterCoefficients[i].SourceWeights = malloc(sizeof(SampleWeight) * (int)ceil(scalingFactorWidth * window_width)); // **exception usually here**
if (FilterCoefficients[i].SourceWeights == NULL) {
//errhandler
}
FilterCoefficients[i].TargetSampleNum = i;
for (int j = i - filter_width; j < i + filter_width; j++) {
FilterCoefficients[i].SourceWeights[j + filter_width].Weight = bilinear_filter(0.5 + scalingFactorWidth2 * (j - 0.5));
if (j > 0) {
FilterCoefficients[i].SourceWeights[j + filter_width].SourceSampleNum = j;
}
else {
FilterCoefficients[i].SourceWeights[j + filter_width].SourceSampleNum = 0;
}
}
}
for (int i = 0; i < target_width; i++) {
free(FilterCoefficients[i].SourceWeights); // **exception usually here**
}
free(FilterCoefficients);
Any help which is gonna point me to a solution is appreciated.
This compiles and executes. I had to simplify.
#include <stdio.h>
#include <stdlib.h>
typedef struct SampleWeight_t
{
unsigned long SourceSampleNum;
double Weight;
} SampleWeight;
typedef struct FilterCoefficients_t
{
unsigned long TargetSampleNum;
SampleWeight* SourceWeights;
} FilterCoefficients;
int main ( void) {
FilterCoefficients* FilterCoeff;
int window_width = 35;
int target_width = 55;
FilterCoeff = malloc(sizeof(FilterCoefficients) * target_width);
if (FilterCoeff == NULL) {
//errhandler
}
for (int i = 0; i < target_width; i++) {
FilterCoeff[i].SourceWeights = malloc(sizeof(SampleWeight) * window_width);
if (FilterCoeff[i].SourceWeights == NULL) {
//errhandler
}
FilterCoeff[i].TargetSampleNum = i;
for (int j = 0; j < window_width; j++) {
FilterCoeff[i].SourceWeights[j].Weight = i * j;
if (j > 0) {
FilterCoeff[i].SourceWeights[j].SourceSampleNum = j;
}
else {
FilterCoeff[i].SourceWeights[j].SourceSampleNum = 0;
}
}
}
for (int i = 0; i < target_width; i++) {
free(FilterCoeff[i].SourceWeights); // **exception usually here**
}
free(FilterCoeff);
}

C structure printing wrong value even after initializing properly

In the program below, I am allocating memory for pointer to pointer properly and then allocating individual pointers and setting values properly, even though I am getting the garbage value to one of the structure member. I don't understand where exactly I am going wrong.
The sample output of below program is:
***CK: H2 nSupport: 0
CK: H2 nSupport: 1303643608
CK: FR2 nSupport: 0
CK: H2 nSupport: 1303643608
CK: FR2 nSupport: 0
***CK: SP2 nSupport: 0
I don't understand how I am getting the value 1303643608, though it is set properly at the beginning.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NOK 15
#define ML 100
typedef struct sample_test_for_ties_t
{
char sender[NOK];
char receiver[NOK];
char message[ML];
}sample_test_for_ties;
typedef struct CUOfS_t{
char cK[NOK];
char eK[NOK];
char nK[NOK];
char AL[ML];
int nSupport;
}CUOfS;
CUOfS **Btenders = NULL;
sample_test_for_ties test_ties[] = {
{"H2","ICY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"FR2","AIY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"SP2","LAY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"H30","ICY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"F30","AIY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"W30","LAY", "fmaabghijklmmcdenoopqrstuvwxyz"},
};
void InitBtenders(int numOfBCtenders)
{
int count =0;
if(!Btenders)
{
if(Btenders = (CUOfS **)malloc(sizeof (**Btenders) * numOfBCtenders))
{
while(count < numOfBCtenders)
{
Btenders[count] = NULL;
count++;
}
}
else
{
printf("Malloc failed\n");
}
}
}
void freeBtenders(int numOfBCtenders)
{
int count =0;
if(Btenders)
{
while(count<numOfBCtenders)
{
if(Btenders[count]) {
free(Btenders[count]);
Btenders[count] = NULL;
}
count++;
}
free(Btenders);
Btenders = NULL;
}
}
void UpdateBtendersInfo(char *aContenders)
{
static int counter =0;
if(Btenders)
{
if(Btenders[counter] == NULL) {
Btenders[counter] = (CUOfS *)malloc(sizeof (Btenders[counter]));
if(Btenders[counter])
{
strcpy(Btenders[counter]->cK,aContenders);
strcpy(Btenders[counter]->eK,"\0");
strcpy(Btenders[counter]->nK,"\0");
memset(Btenders[counter]->AL,0,sizeof(Btenders[counter]->AL));
Btenders[counter]->nSupport = 0;
counter++;
}
else
{
printf("Insufficient memory for Btender\n");
}
}
}
else
{
printf("Looks like memory not allocated for Btenders\n");
}
int count =0;
while(count <counter && Btenders[count])
{
printf("***CK: %s nSupport: %d\n",Btenders[count]->cK,Btenders[count]->nSupport);
count++;
}
printf("\n");
}
int main()
{
int numOfBCtenders = 3;
int noc =0;
InitBtenders(numOfBCtenders);
while(noc < numOfBCtenders)
{
UpdateBtendersInfo(test_ties[noc].sender);
noc++;
}
freeBtenders(numOfBCtenders);
return 0;
}
I expect
***CK: H2 nSupport: 0
But I am getting
***CK: H2 nSupport: 1303643608
The way you allocate memory for Btenders is incorrect.
Btenders = (CUOfS **)malloc(sizeof (**Btenders) * numOfBCtenders) // WRONG
sizeof (**Btenders) is the same as sizeof(CUOfS), but you need sizeof(CUOfS*) :
Btenders = (CUOfS **)malloc(sizeof (*Btenders) * numOfBCtenders) // FIXED
Similarly for :
Btenders[counter] = (CUOfS *)malloc(sizeof (Btenders[counter])); // WRONG
sizeof (Btenders[counter]) is the same as sizeof(CUOfS*), but you need sizeof(CUOfS) :
Btenders[counter] = (CUOfS *)malloc(sizeof (*(Btenders[counter]))); // FIXED

printf shows wrong output, strange question mark at the end of line [C]

This code trying to perform queue, but that's queue has two fields: number and word. My problem is that field "word" prints incorrectly(field "number" is fine)
Expected output:
22
abc
12
efg
654
xyz
Unfortunately output looks like this
https://ibb.co/gjF446F
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define MAX_capacity 1000
#define Max_len_napis 100
typedef struct{
int number;
char word[];
} data;
data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;
int front() {
return intArray[peak].number;
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX_capacity;
}
int size() {
return itemCount;
}
void insert(data x) {
if(!isFull()) {
if(rear == MAX_capacity-1) {
rear = -1;
}
int indeks = ++rear;
intArray[indeks].number = x.number;
strcpy (intArray[indeks].word, x.word);
itemCount++;
}
}
data remove() {
data dat = intArray[peak++];
if(peak == MAX_capacity) {
peak = 0;
}
itemCount--;
return dat;
}
void print(int N){
for(int i=0;i<N;i++){
data n = remove();
printf("%d\n",n.number);
printf("%s\n",n.word); // that's line doesn't work correctly
}
}
int main() {
data tab[3];
tab[0].number = 22;
strcpy (tab[0].word, "abc");
insert(tab[0]);
tab[1].number = 12;
strcpy (tab[1].word, "efg");
insert(tab[1]);
tab[2].number = 654;
strcpy (tab[2].word, "xyz");
insert(tab[2]);
int siz = size();
print(siz);
return 0;
}
I think that printf("%s\n",n.word) is not work correctly. But if I dont use struct, all works properly.
You need to allocate memory for word. For example like this:
typedef struct{
int number;
char word[100];
} data;
Better way is to allocate memory for word dynamically.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define MAX_capacity 1000
#define Max_len_napis 100
typedef struct{
int number;
char word[100];
} data;
data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;
int front() {
return intArray[peak].number;
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX_capacity;
}
int size() {
return itemCount;
}
void insert(data x) {
if(!isFull()) {
if(rear == MAX_capacity-1) {
rear = -1;
}
int indeks = ++rear;
intArray[indeks].number = x.number;
strcpy (intArray[indeks].word, x.word);
itemCount++;
}
}
data remove() {
data dat = intArray[peak++];
if(peak == MAX_capacity) {
peak = 0;
}
itemCount--;
return dat;
}
void print(int N){
for(int i=0;i<N;i++){
data n = remove();
printf("%d\n",n.number);
printf("%s\n",n.word); // that's line doesn't work correctly
}
}
int main() {
data tab[3];
tab[0].number = 22;
strcpy (tab[0].word, "abc");
insert(tab[0]);
tab[1].number = 12;
strcpy (tab[1].word, "efg");
insert(tab[1]);
tab[2].number = 654;
strcpy (tab[2].word, "xyz");
insert(tab[2]);
int siz = size();
print(siz);
return 0;
}

C Header files not working?

I can't get my runner.c to see my mergesort.h import, just the last thing I need to do before I go through and add in comments.
Not seeing what I am doing wrong here so I thought I'd get more eyes to look at it. I looked up questions on here and also pretty much copied the template my lab professor has but it still won't work.
Error
/tmp/ccf8TT1E.o: In function `main':
/home/carson/CProgs/lab5/runner.c:10: undefined reference to `mergeSort'
/home/carson/CProgs/lab5/runner.c:11: undefined reference to `print'
collect2: error: ld returned 1 exit status
runner.c
#include <stdio.h>
#include <stdlib.h>
#include "mergesort.h"
int main()
{
int length = 4;
int array[4] = {7,5,3,1};
mergeSort(array, length);
print(array,length);
}
mergesort.h
#ifndef MERGESORT_H_
#define MERGESORT_H_
void mergeSort(int* x, int length);
#endif
mergesort.c
#include <stdio.h>
#include <stdlib.h>
#include "mergesort.h"
void mergeSort(int* array, int length)
{
if(length == 2)
{
if(array[1] < array[0])
{
swap(&array[1],&array[0]);
}
return;
}
else if(length == 1)
{
return;
}
else
{
if(length%2 == 0)
{
int halfLength = length/2;
int bottomHalf[halfLength];
int topHalf[halfLength];
int i;
for(i = 0; i <= (halfLength); i++)
{
bottomHalf[i] = array[i];
topHalf[i] = array[i+(halfLength)];
}
mergeSort(bottomHalf, halfLength);
mergeSort(topHalf, halfLength);
int arrayLoc = 0;
int bottomHalfLoc = 0;
int topHalfLoc = 0;
while(bottomHalfLoc < halfLength && topHalfLoc < halfLength)
{
if(bottomHalf[bottomHalfLoc] > topHalf[topHalfLoc])
{
array[arrayLoc] = topHalf[topHalfLoc];
topHalfLoc++;
}
else
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
}
arrayLoc++;
}
if(bottomHalfLoc < arrayLoc)
{
while(bottomHalfLoc < halfLength)
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
arrayLoc++;
}
}
return;
}else
{
int halfLength = length/2;
int bottomHalf[halfLength];
int topHalf[halfLength+1];
int i;
for(i = 0; i <= (halfLength); i++)
{
bottomHalf[i] = array[i];
}
for(i = 0; i <=(halfLength+1); i++)
{
topHalf[i] = array[i+halfLength];
}
mergeSort(bottomHalf, halfLength);
mergeSort(topHalf, halfLength);
int arrayLoc = 0;
int bottomHalfLoc = 0;
int topHalfLoc = 0;
while(bottomHalfLoc < halfLength && topHalfLoc < halfLength)
{
if(bottomHalf[bottomHalfLoc] > topHalf[topHalfLoc])
{
array[arrayLoc] = topHalf[topHalfLoc];
topHalfLoc++;
}
else
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
}
arrayLoc++;
}
if(bottomHalfLoc < arrayLoc)
{
while(bottomHalfLoc < halfLength)
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
arrayLoc++;
}
}
return;
}
}
}
void swap(int *smallerValue, int *largerValue)
{
int temp = *smallerValue;
*smallerValue = *largerValue;
*largerValue = temp;
}
void print(int array[], int length)
{
printf("%d",array[0]);
int i;
for(i = 1; i < length; i++)
{
printf(",%d",array[i]);
}
printf("\n");
}
use gcc *.c -o runner.out and one more thing i dont see print function in mergesort.h file but u r calling it in the main function. include it or use extern print(array,length);

Resources