Trouble looping through arrays define in other source files - c

I have asked a question somewhat related to this this before asking why cant i return the array size from a malloc/calloc (i have received an answer to this).
My current question is i have 2 arrays defined and fill in two separate source files ship.c and rescue_assets.c. I am attempting to loop through them in a method inside a file called system_handler.c.
The trouble i am having is that this task requires that you DO NOT hardcore an array size into the code so i don't see how i can link the array size from each c file into this function in the 3rd c file.
Ultimately i would like:
assign_mayday_to_ships(int SIZE_OF_ARRAY_FROM_FILE_1, int SIZE_OF_ARRAY_FROM_FILE_2){
for(int i=0; i < SIZE_OF_ARRAYFROM_FILE_1; i++){
for(int j = 0; < SIZE_OF_ARRAYFROM_FILE_2; j++{
//do something
}
}
i could easily do this if they were in the same file, but i can't call that method from two different files, because it would obviously lack the parameters required.
Here is the code in question ( ive only added the required snippets, all headers are included and the system runs as intended bar getting the array sizes):
system_handler.c
void assign_mayday_to_ships() {
mayday_call* mday_ptr;
ship* ship_ptr;
rescue_asset* assets_ptr;
mday_ptr = read_mayday_file();
ship_ptr = read_ship_locations();
assets_ptr = read_recuse_assets();
int i;
int result;
/* loop through ship locations to find the ship that called the mayday
When found assign the mayday call to the ship for use when sending help*/
for (i = 0; i < arr_size; i++) {
result = strncmp(mday_ptr->ais, (ship_ptr + i)->ais, COMPARE_LIMIT);
if (result == 0) {
mday_ptr->ship = (ship_ptr + i);
}
}
calc_distance_to_mayday(mday_ptr, assets_ptr);
}
rescue_asset.c: assets is the array i want to get the size of.
rescue_asset* assets;
no_of_lines = count_lines(locof);
printf("number of lines = %d \n", no_of_lines);
assets = calloc(no_of_lines,sizeof (rescue_asset));
ship.c: ships is the array want to get the size of.
ship* ships;
/* -1 because first line of file is not a ship location*/
no_of_lines = (count_lines(locof) - 1);
ships = calloc(no_of_lines, sizeof (ship));
Would it be better to use actual arrays rather than calloc and such?
Thanks,
Chris.

You have to pass in the number of items you have allocated as an argument to the function. If you can't do that (like in you r case where those are allocated in called functions) you can return it by either having the size added as a pointer argument to the function which does the allocation (passing by reference), or by returning a structure containing the pointer and the size.
For the first, you can do something like
size_t asset_size;
asset *assets_ptr = read_recuse_assets(&asset_size);
Then in read_recuse_assets you set *asset_size to the correct size.
Of course, you can do the opposite with the pointer and size, and pass a pointer to assets_ptr as argument and returning the size.
More complete example:
asset *read_recuse_assets(size_t *asset_size)
{
...
*asset_size = no_of_lines;
return assets;
}
Call as outlined above.
For the second alternative, you can have a structure like this:
struct asset_data
{
size_t size;
asset *assets;
};
Then return an instance (not pointer) of this structure with the field filled in.

Related

How can I shorten an array?

I wanted to create a function that deletes from an array of segments the ones that are longer than a given number, by freeing the memory I don't need anymore. The problem is that the function I've created frees also all the memory allocated after the given point. How can I limit it, so that it frees just one pointer without compromising the others?
Here is the code I've written so far:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
typedef struct
{
double x1;
double y1;
double x2;
double y2;
} Segment;
double length(Segment* s)
{
return sqrt(pow(s->x1 - s->x2, 2) + pow(s->y1 - s->y2, 2));
}
// HERE IS THE PROBLEM!!
void delete_longer(Segment* as[], int n, double max_len)
{
for(int i = 0; i < n; i++)
{
if(length(as[i]) > max_len)
{
as[i] = NULL; // Those two lines should be swapped, but the problem remains
free(as[i]);
}
}
}
int main()
{
const int SIZE = 5;
Segment** arr = (Segment**)calloc(SIZE, sizeof(Segment*));
for(int i = 0; i < SIZE; i++)
{
arr[i] = (Segment*)malloc(sizeof(Segment));
}
srand(time(0));
for(int i = 0; i < SIZE; i++)
{
arr[i]->x1 = rand() % 100;
arr[i]->x2 = rand() % 100;
arr[i]->y1 = rand() % 100;
arr[i]->y2 = rand() % 100;
printf("Lungezza: %d\n", (int)length(arr[i]));
}
delete_longer(arr, SIZE, 80);
for(int i = 0; i < SIZE && arr[i]; i++)
{
printf("Lunghezza 2: %d\n", (int)length(arr[i]));
}
return 0;
}
First of all the free function should come after the instruction that sets the pointer to NULL, but that's not the main cause of the problem.
What causes the behaviour I described was the fact that the second for loop in the main stops after finding the first NULL pointer. Instead I should have written:
for(int i = 0; i < SIZE ; i++)
{
if(arr[i])
printf("Lunghezza 2: %d\n", (int)length(arr[i]));
}
You have two main problems:
In the delete function you write:
as[i] = NULL;
free(as[i]);
This is the wrong order. You must first free the memory and then set the element to null. But note that this is not the cause of your perceived problem, it only causes a memory leak (i.e. the memory of as[i] becomes inaccessible). You should write:
free(as[i]);
as[i] = NULL;
Your second problem is in your for loop, which now stops at the first null element. So not all the memory after it is deleted, you just don't print it. The loop should be for example:
for(int i = 0; i < SIZE; i++)
{
printf("Lunghezza 2: %d\n", arr[i]?(int)length(arr[i]):0);
}
Note: I agree with the discussion that free(NULL) may be implementation dependent in older implementations of the library function. In my personal opinion, never pass free a null pointer. I consider it bad practice.
There's no way to change the size of an array at runtime. The compiler assigns the memory statically, and even automatic arrays are fixed size (except if you use the last C standard, in which you can specify a different size at declaration time, but even in that case, the array size stands until the array gets out of scope). The reason is that, once allocated, the memory of an array gets surrounded of other declarations that, being fixed, make it difficult ot use the memory otherwise.
The other alternative is to allocate the array dynamically. You allocate a fixed number of cells, and store with the array, not only it's size, but also its capacity (the maximum amount of cell it is allow to grow) Think that erasing an element of an array requires moving all the elements behind to the front one place, and this is in general an expensive thing to do. If your array is filled with references to other objects, a common technique is to use NULL pointers on array cells that are unused, or to shift all the elements one place to the beginning.
Despite the technique you use, arrays are a very efficient way to access multiple objects of the same type, but they are difficult to shorten or enlengthen.
Finally, a common technique to handle arrays in a way you can consider them as variable length is to allocate a fixed amount of cells (initially) and if you need more memory to allocate double the space of the original (there are other approaches, like using a fibonacci sequence to grow the array) and use the size of the array and the actual capacity of it. Only in case your array is full, you call a function that will allocate a new array of larger size, adjust the capacity, copy the elements to the new copy, and deallocate the old array. This will work until you fill it again.
You don't post any code, so I shall do the same. If you have some issue with some precise code, don't hesitate to post it in your question, I'll try to provide you with a working solution.

Stored data is not same which I can access in file read loop

I have to read a txt with data in it. I can read it and store the data, but I don't know why, some stored data is not good after the read method.
Here is my output:
I write out these data with exactly the same code, except that the first is inside the loop and the second is outside of the loop.
I store these data in their own struct arrays. So as you can see, my problem is that I can't access my data outside that loop. What could be wrong?
Here is the full code: https://pastebin.com/wzEJqcZG
And the test data: https://pastebin.com/L7J133mz
This is inside the file read loop:
printf("%c %i - ", sorok[i].futarkod, sorok[i].datum);
for(j=0;j<sorok[i].rendelesCount;j++) {
printf("%i%c", sorok[i].rendelesek[j].db, sorok[i].rendelesek[j].fajta);
}
printf("\n");
And this is outside of the file read loop:
for(i=0;i<5;i++) {
printf("%c %i - ", sorok[i].futarkod, sorok[i].datum);
for(j=0;j<sorok[i].rendelesCount;j++) {
printf("%i%c ", sorok[i].rendelesek[j].db, sorok[i].rendelesek[j].fajta);
}
printf("\n");
}
In the output the first two columns are good, just the text after the dash is not.
test.c:65:14: warning: 'sor' may not be used as an array element due to flexible array member
[-Wflexible-array-extensions]
sor sorok[32];
^
rendeles rendelesek[]; is a flexible array member meaning since it's at the end of the struct you can, in theory, allocate as much memory for the array as you like. However this means the size of any given sor will vary.
Each element of an array in C must be of a fixed size, going from one element to another is simply start-of-array-memory + (i * sizeof(element)). Since sor can be of different sizes it can't be put into an array.
You could use an array of pointers to sor, or you can change sor to contain a pointer to rendeles **rendelesek;. Or both, getting used to working with pointers is good.
The real problem is sor.rendelesek is never allocated. Whichever you choose, you still have to allocate memory to sor.rendelesek else you're writing into someone else's memory. As a flexible array member, you have to use a pointer array and allocate sufficient memory as part of sor.
typedef struct {
char futarkod;
int datum;
int rendelesCount;
rendeles rendelesek[];
} sor;
sor *sorok[32];
for( size_t i = 0; i < 32; i++) {
sorok[i] = malloc(sizeof(sor) + (sizeof(rendeles) * 32));
}
Or you can use a rendelesek ** instead and allocate that directly. Combining both is probably the best option.
typedef struct {
char futarkod;
int datum;
int rendelesCount;
rendeles *rendelesek;
} sor;
sor *new_sor(const size_t num_rendeles) {
sor *new = malloc(sizeof(sor));
new->rendelesek = malloc(sizeof(rendeles) * num_rendeles);
return new;
}
int main()
{
sor *sorok[32];
for( size_t i = 0; i < 32; i++) {
sorok[i] = new_sor(32);
}
Reading inputs into statically allocated structures like this is risky and wasteful because you have to allocate what you think is the most possible elements. It's very easy to allocate way too much or not enough. Instead they should be dynamically allocated as needed, but that's another thing.

Creating a game board using struct

So I am making a game board for battle ship, but I am trying to use a struct to make it. Currently I am getting an error when trying to create the game board. I'm trying to make it this way so that when the user inputs coordinates for the ships, or when they are making a move I can easily change symbols and get to the correct coordinate on the array. This is what I am working with right now. I am using 3 file format, so I put all the parts down below.
I could create the board without using a struct, but I'm pretty sure I need to create it this way so that I am able to manipulate the board later.
If this helps this is my current error: Run-Time Check Failure #3 - The variable 'person' is being used without being initialized.
This is in the header file
typedef struct game_board
{
int board[10][10];
int row;
int col;
char symbol;
}Game_Board;
Game_Board initalize_game_board(Game_Board player);
The function
Game_Board initalize_game_board(Game_Board player)
{
int row_index = 0, col_index = 0;
printf(" 0 1 2 3 4 5 6 7 8 9\n");
for (row_index = 0; row_index < player.row; row_index++)
{
printf("%d ", row_index);
for (col_index = 0; col_index < player.col; col_index++)
{
player.board[row_index][col_index] = player.symbol;
printf("%c ", player.board[row_index][col_index]);
}
printf("\n");
}
}
In the main function
int main(void)
{
FILE *outfile = NULL;
outfile = fopen("battleship.log", "w");
Game_Board person, computer;
int who_goes_first = 0;
strcpy(person.symbol, '~');
person.row = 10;
person.col = 10;
strcpy(computer.symbol, '~');
computer.row = 10;
computer.col = 10;
welcome_screen(outfile);
printf("Player 1\n");
initalize_game_board(person);
You need return statements for both functions. Ending paren for main().
Highly recommend
computer.symbol = '~';
Instead of
strcpy(computer.symbol, '~');
Building on Ryan's answer, strcpy() assumes the source string has a null terminator, to know how many characters are to be copied. In your case, there is no null terminator in the source string. strcpy() will not stop copying till a null character is read from memory. Since you are using a single character, a simple assignment statement will do!
Also, in function initalize_game_board(), consider passing the pointer to the Game_Board structure, instead of the structure itself. When you do this, you can change the contents of the actual structure (struct 'person' in this case), by using the arrow (->) operator, within the function itself. Then you will not have to return the structure from this function. With your current design, you are creating a temporary copy of the structure, then populating it, and then (probably after modifications!) returning that structure, which will again be copied to the structure 'person'.

Initialising member elements of a dynamically allocated array of structs to zero

I have had a look around but have not been able to find an answer to this question already. I am trying to create a hash table of which each element is a struct. In each struct is a variable to let the program know if the cell has been occupied, for this to work I need to set all of them to zero. The thing is it worked fine but now and then (seemingly randomly) I'd get an access violation. I thought I fixed it but when I come to grow my array the error creeps up again, leading me to believe that I have made an error. My pointer knowledge is not that good at all, so any help would be appreciated. This is what the function looks like:
HashTableCell *initialiseTable(HashTableCell *hashTable, int *tableSizePtr)
{
int i = 0;
int totalSize = *tableSizePtr * sizeof(HashTableCell);
HashTableCell *tempStartingcell;
tempStartingcell = (HashTableCell*)malloc(sizeof(HashTableCell));
*tempStartingcell = *hashTable;
while (i <= *tableSizePtr)
{
/*we keep moving forward, need to use the first entry*/
*hashTable = *(tempStartingcell + (i * sizeof(HashTableCell)));
hashTable->isOccupied = 0;
i++;
}
free(tempStartingcell);
return hashTable;
}
And before I malloced some space for the table and passed it in another function like so:
HashTableCell *hashTable;
hashTable = (HashTableCell*)malloc((sizeof(HashTableCell)*tableSize));
hashTable = initialiseTable(hashTable, tableSizePtr);
The idea is to start at the beginning and move along the correct number of spaces along per iteration of the while loop. When I come to resize I merely make a new array with double the malloced space and pass it to the initialise function but this throws up an access violation error at seemingly random indexes.
I am using VS2015 if that helps anything.
Thank you for your help.
The problem is in this line:
*hashTable = *(tempStartingcell + (i * sizeof(HashTableCell)));
When you are adding an integer to a pointer, C and C++ already take into account the size of the array elements, so you should not multiply with sizeof(HashTableCell), but rather do:
*hashTable = *(tempStartingcell + i);
Otherwise, your extra multiplication will cause an access outside of the tempStartingCell array. It makes even more sense to write it like this:
*hashTable = tempStartingcell[i];
But there is more wrong with your code; if you just want to set isOccupied to zero for each element in hashTable, just do:
void initialiseTable(HashTableCell *hashTable, int tableSize)
{
for (int i = 0; i < tableSize; i++)
hashTable[i].isOccupied = 0;
}

C pass size of an array to another c file variable

**Issue overview **
The current issue i have is that i have an array which has the size of a variable which happens to be the amount of lines in a file. This is an integer which is calculated and returned, which becomes the arraysize (e.g. file text.txt has 12 lines so the array is of size 12).
I want to know how i can return this value to another c file for use in a function so i can loop through the complete array.
points to note
I am not allowed any globals at all in this assignment, no global arrays/variables at all.
The line counting functions works correctly so i will not be posting it here
The array is set up correctly and print the correct results
Most of the code from the functions has been removed to make it easier to read.
The correct #includes for each file are present, i just need an example of how to do it.
The code:
void read_from_file() {
/* reading and parsing removed */
no_of_lines = (count_lines(locof) - 1);
/* locof is a char array storing the file name */
ship ships[no_of_lines];
/* i want to return the value of no_of_lines *?
I want to return the value of no_of_lines
The c file where i need the value
/*This is where i need the variable */
void asign_mayday_to_ships() {
int* ship_arr_length = SIZE OF SHIP ARRAY NEEDED
mayday_call* mday_ptr;
ship* ship_ptr; /* this is a ship array */
mday_ptr = read_mayday_file();
ship_ptr = read_ship_locations();
int i;
for(i = 0; i < SIZE OF SHIP ARRAY; i++){
}
Just pass both pointer and size, it'a a C way.
void read_from_file() {
/* reading and parsing removed */
no_of_lines = (count_lines(locof) - 1);
/* locof is a char array storing the file name */
ship ships[no_of_lines];
some_fun_from_second_file(ships, no_of_lines);
Who calls the two fnctions? Can't you just return the nr of lines in an upper function then pass it in the second one? If not, then you must somehow store it in a variable(or a struct member) and then you can grab it later. This is a contextual solution, it might not work for you.
i had to first malloc my ships array, then set the size of the malloc depending on the amount of elements then i can return the size of the pointer:
ship* ships;
ships = malloc(sizeof (ship) * no_of_lines);
the in the function i was having trouble with:
mayday_call* mday_ptr;
ship* ship_ptr;
mday_ptr = read_mayday_file();
ship_ptr = read_ship_locations();
int arr_size = sizeof (ship_ptr) ;
int i;
for(i =0; i < arr_size; i++) {
//do something
}
`
sounds like your 'teacher' wants make you use a sentinel value. Ie put an object at the end of the array that cannot exist (a ship with name all spaces for example) then in the array processing you keep looping till you hit the magic value.
This is a bad design, but if you aren't allow globals and you aren't allow parameters I cant see what else to do

Resources