i'm trying to make a game in C, and my problem is this:
I need to have some kind of method in C, that does basicaly what this python code does:
array = list();
len(array) #returns 0
x = 1
array.append(x)
len(array) #returns 1
array.append(2)
len(array) #returns 2
This is just the basic method that I need to do, the test if it's less than 5 is just to be different sizes array, originally my problem is how to manipulate the gunshots in my game, creating them pressing space on the keyboard(appending the gunshot to an until now empty array), and then i have to do something like this, just in C:
for i in range(len(array)):
print array[i]
originally, that's when the loop is running and I have to determine if the gunshot is visible and move it or not, I don't know how to determine the lenght of the array in C, specially if the array has a variable size(can increase size in time)
In Python it would be easy to handle this, but I don't know how to do it in C, and since my game has to be in C 'cause it's for a college class..
I'd suggest a linked list instead of an array, because you'll eventually want to remove arbitrary elements. (Bullet 1 is still travelling across the screen, but bullet 3 hit a wall and bullet 6 hit an enemy.) You can do that directly in your bullet struct rather than as a separate data structure:
typedef struct bullet_t {
/* ...game logic... */
struct bullet_t* next;
} bullet;
C does not support variable sized arrays; if C++ is an option, go for std::vector. If not, you will have to implement that in C on your own. I would personally suggest doing something close to what std::vector does - each time you run out of memory, realloc twice as much.
There are different ways to do what you want to do, but I advice chained list.
Basically, the issue for converting your python code into C-code is the fact that you have to manually allocate your variables most of the time, and here you have. So you can do a chained list, where you allocate every new element with a malloc, and then you link it to the rest of the list with a pointer.
Then, to know the size, just follow the list with a counter.
Good luck. ;)
I would recommend a different ADT. For example a linked list, or a Set ADTs (if you google a bit, you'll probably find finished ones). However, if you absolutely need to use arrays, then you should look up dynamically allocated ones. Here is an implementation example. (Not tested)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *array;
int size;
int capacity;
} array_t;
#define ARRAY_INIT_CAPACITY 4
array_t *new_array(){
array_t *arr=malloc(sizeof(array_t));
arr->array=malloc(sizeof(int)*ARRAY_INIT_CAPACITY);
arr->size=0;
arr->capacity=ARRAY_INIT_CAPACITY;
return arr;
}
void increase_array(array_t *array){
int new_capacity=array->capacity*2;
int *new_location = realloc(array->array, new_capacity*sizeof(int));
if (!new_location) {
fprintf(stderr, "Out of memory");
exit(1);
}
array->capacity=new_capacity;
array->array=new_location;
}
void array_append(array_t *array, int item){
if (array->size >= array->capacity){
increase_array(array);
}
array->array[array->size]=item;
array->size+=1;
}
int array_size(array_t *array){
return array->size;
}
array_t *myArray=new_array();
I'd recommend learning about dynamic memory allocation and Arrays in C.
Hope it helps :)
Related
I think my english is just to bad to understand the other articles about this. But anyway:
I just thought i could write a program (in C), that can store a set of cards.
Not complicated, just store values and names of cards and print them out.
I'm a beginner in C, and because i'm in the section "Strings in Structures" in my Book, i wanted to try out structures on my own. This is my Code so far:
#include <stdio.h>
struct card
{
int value;
char name[];
};
int main(void)
{
const struct card heart[13] = { {2,"two"}, {3,"three"}, {4,"four"}, {5,"five"}, {6,"six"}, {7,"seven"}, {8,"eight"}, {9,"nine"}, {10,"ten"}, {11,"jack"}, {12,"queen"}, {13,"king"}, {14,"ace"} };
int i;
for (i = 0; i < 13; ++i)
{
printf("The card heart-%s has the value of %i", heart[i].name, heart[i].value);
}
return 0;
}
I just wanted to test if it works, so i just wrote the heart-cards in the code. If i want to compile this file, my compiler (gcc/mingw) hits me with 26 errors. It says:
"(near initialization of heart[0])"
"non static initialization of a flexible array member"
I don't really understand this. In the book, everything works as expected. I tried to rebuild the code in the book and changing the names, but it doesn't work. I think it's a problem with the strings, because if i use integers only, everything works.
In already read in another post, that every string should be allocated manually, and there was a code example, but i don't know what all the lines should mean, and i want understand what my code does, so i don't copy + paste.
Could you explain me why this doesn't work?
PS: I am writing currently in windows, so please don't use bash commands to explain or something like that.
I am also german and my english is not the "yellow of the egg", try to explain without using complex 'sentence builds' (i hope you know what i mean :D) and unusual words.
Thanks for all help!
You need to create some space for the name of each card. Easiest way to do this would be to change your struct card definition to something like:
struct card
{
int value;
char name[16]; // doesn't have to be 16, but make sure it's large enough to hold each card name plus a '\0' terminator
};
The prior answers suggest allocating a fixed length for your names. This has limitations and even dangers. It is always a good idea to avoid it all together.
e.g. You want to alter the name during the game, e.g. "Ace (Trump Card)" but that might be both too long even worse overwrite memory. (Many of the known vulnarabilities in code are caused by buffer overruns)
You are also building in a limitation; What if your game needs translating into another language?
By using pointers, you don't need to resort to either variable length structures or fixed string lengths.
You also add the ability to add API access functions that set data, allowing checks before it's written, preventing buffer overruns.
Instead of using character array (aka strings) you should use pointers in your structures. If you follow the link at the bottom I take this further and use pointers to the structures themselves.
As the pointer storage size never changes your names can be of any length and even altered later, perhaps as the game progresses.
Your card could look something like
typedef struct card
{
int value;
char * name;
}
Now the initial assignment can be done like this
card_t card_ace = {14, "Ace"};
And the values are not fixed (unless that is what you want, then you make them const).
card_ace.value = 200;
card_ace.name = "Trump card";
or an array of cards like this
card_t suit_hearts[] = {{2,"two"}, {3,"three"}, {4,"four"}, {5,"five"}, {6,"six"}, {7,"seven"}, {8,"eight"}, {9,"nine"}, {10,"ten"}, {11,"jack"}, {12,"queen"}, {13,"king"}, {14,"ace"}}
Even better make the whole thing using pointers
typedef card_t * cards_t;
cards_t mysuit = &(card_t){2,"two"}, &(card_t){3,"three"}, ...
Perhaps consider makeing the suit a structure.
typedef struct
{
char * name;
card_t ** cards;
} suit_t;
typedef card_t * cards_t[];
suit_t mysuit = {
.name = "Hearts",
.cards = (cards_t){&(card_t){2,"two"}, &(card_t){3,"three"},....}
}
* For a fully working example of the latter, demonstrating using arrays of pointers to sidestep the limitations of variable length members of fixed arrays, see this gist on github
I'm facing some trouble to find the best way to return an struct with an array or pointer to an array.
here is what i want to do:
i have a struct
typedef struct {
double *matrix;
int cols;
int rows;
int nelems;
} ResultMat;
and a function that parses a file. I need to call that function and have it return the struct
ResultMat read (string file, string tag) {
ResultMat mat;
.....
mat.cols = //some value from the file
mat.rows = //some value from the file
double array[rows][cols];
//now i fill the array
.......
mat.matrix = *array;
return mat;
}
within an array is filled with the values and i want to get back that whole struct with the
array/ pointer to the array stored in mat.matrix.
How to do that and is there maybe a better way? Im quite new to C and more familiar with OO programming, thats why I'm having trouble to find the best solution.
Hope anybody can give me some help! Thanks
I think that
double array[rows][cols];
will be a problem, as you create the array on the local function stack.
This will be erased once you leave the function.
You should also be aware of, that Variable-Length arrays are not ANSI-C conform and you should better not use it in my opinion.
You should work with pointers and dynamic memory allocation.
malloc would be the keyword here.
Hope this helps
The only other way I can think of, is to put all the output arguments as input pointers so that your prototype function would look like this:
void read (string file, string tag, double *matrix, int *cols, int *rows, int *nelms);
or you can keep the structure and go for something like this:
void read (string file, string tag, ResultMat *myStructure);
IMO, there is no "better way", these are all just different options, yours is another option, which I found myself using very often.
I am programming in C. I am using also a library to create tasks which can communicate among them via messages. The content of these messages must be the pointer to the data you want to exchange.
In my case I want to send an array so I am sending a pointer to the array. In the receiving task I can then access the different elements by increasing the pointer, but, is there a way to know how long the array was?
Thank you in advance.
No, there is no way to do this as arrays decay to pointers when passed to functions, thus all information regarding size is lost.
You can include another parameter to specify the array length
void func(int len, int arr[]);
You can mark the end of the array with a special value (perhaps like argv does)
You can put your array into a structure and pass that (and suffer the performance penalties) or pass the a pointer to the structure
struct args {
int arr[100];
};
If you decide to go the first route, you can use a nice feature of C99, even if it doesn't actually enforce that arr has at least len elements:
void func(int len, int arr[len]);
No. So consider something slightly different.
struct arrayinfo {
struct element *array;
int count;
};
Then you can pass a pointer to your struct arrayinfo type and your receiving task will know how many elements are in your array.
An alternative technique is to make your array and array of pointers. Make your array one element bigger than necessary and set the last element to NULL.
No, you can't. The compiler doesn't know what the pointer is pointing to. You either need to pass the length value along with the array or you can trick it by allocating extra memory in order to store the length value before the start of the array.
No, that is not possible unless you also send the size of the array or set a guardian value in the end of the array.
I suggest that you do not send the array directly but use a intermediary struct, like the following:
struct intermediary {
int *the_array; /* the pointer to the array */
size_t size; /* the array size */
};
In the message you send the struct, and not the array itself.
Couldn't you use some kind of sentry value at the end of the buffers where the messages are stored. That should be such a value that you won't find it anywhere in the message. Depending on the type of these message its possible that there is such a value: typically a negative number if your messages consists of positive integers, a "INF" or "BIG_INT" number, etc. Then, you must process your message just up to the sentry value. Something like this:
#include <stdlib.h>
#include <string.h>
#define N 100
#define ENDTAG -999999
void foo( int *arr ){
int i = 0;
while( arr[i] != ENDTAG ){
//do whatever
i++;
}
}
int main( void ){
int *arr = (int *) malloc( sizeof(int) * N );
//fill the array, no matter how.
// ...
//set the sentry value
arr[N-1] = ENDTAG;
foo( arr );
free( arr );
return 0;
}
It may sound to much simple, but I actually used something like that more than once. You must though be absolutely sure that you'll take some restricted types of messages.
I have a dynamic array of structures, so I thought I could store the information about the array in the first structure.
So one attribute will represent the amount of memory allocated for the array and another one representing number of the structures actually stored in the array.
The trouble is, that when I put it inside a function that fills it with these structures and tries to allocate more memory if needed, the original array gets somehow distorted.
Can someone explain why is this and how to get past it?
Here is my code
#define INIT 3
typedef struct point{
int x;
int y;
int c;
int d;
}Point;
Point empty(){
Point p;
p.x=1;
p.y=10;
p.c=100;
p.d=1000; //if you put different values it will act differently - weird
return p;
}
void printArray(Point * r){
int i;
int total = r[0].y+1;
for(i=0;i<total;i++){
printf("%2d | P [%2d,%2d][%4d,%4d]\n",i,r[i].x,r[i].y,r[i].c,r[i].d);
}
}
void reallocFunction(Point * r){
r=(Point *) realloc(r,r[0].x*2*sizeof(Point));
r[0].x*=2;
}
void enter(Point* r,int c){
int i;
for(i=1;i<c;i++){
r[r[0].y+1]=empty();
r[0].y++;
if( (r[0].y+2) >= r[0].x ){ /*when the amount of Points is near
*the end of allocated memory.
reallocate the array*/
reallocFunction(r);
}
}
}
int main(int argc, char** argv) {
Point * r=(Point *) malloc ( sizeof ( Point ) * INIT );
r[0]=empty();
r[0].x=INIT; /*so here I store for how many "Points" is there memory
//in r[0].y theres how many Points there are.*/
enter(r,5);
printArray(r);
return (0);
}
Your code does not look clean to me for other reasons, but...
void reallocFunction(Point * r){
r=(Point *) realloc(r,r[0].x*2*sizeof(Point));
r[0].x*=2;
r[0].y++;
}
The problem here is that r in this function is the parameter, hence any modifications to it are lost when the function returns. You need some way to change the caller's version of r. I suggest:
Point * // Note new return type...
reallocFunction(Point * r){
r=(Point *) realloc(r,r[0].x*2*sizeof(Point));
r[0].x*=2;
r[0].y++;
return r; // Note: now we return r back to the caller..
}
Then later:
r = reallocFunction(r);
Now... Another thing to consider is that realloc can fail. A common pattern for realloc that accounts for this is:
Point *reallocFunction(Point * r){
void *new_buffer = realloc(r, r[0].x*2*sizeof(Point));
if (!new_buffer)
{
// realloc failed, pass the error up to the caller..
return NULL;
}
r = new_buffer;
r[0].x*=2;
r[0].y++;
return r;
}
This ensures that you don't leak r when the memory allocation fails, and the caller then has to decide what happens when your function returns NULL...
But, some other things I'd point out about this code (I don't mean to sound like I'm nitpicking about things and trying to tear them apart; this is meant as constructive design feedback):
The names of variables and members don't make it very clear what you're doing.
You've got a lot of magic constants. There's no explanation for what they mean or why they exist.
reallocFunction doesn't seem to really make sense. Perhaps the name and interface can be clearer. When do you need to realloc? Why do you double the X member? Why do you increment Y? Can the caller make these decisions instead? I would make that clearer.
Similarly it's not clear what enter() is supposed to be doing. Maybe the names could be clearer.
It's a good thing to do your allocations and manipulation of member variables in a consistent place, so it's easy to spot (and later, potentially change) how you're supposed to create, destroy and manipulate one of these objects. Here it seems in particular like main() has a lot of knowledge of your structure's internals. That seems bad.
Use of the multiplication operator in parameters to realloc in the way that you do is sometimes a red flag... It's a corner case, but the multiplication can overflow and you can end up shrinking the buffer instead of growing it. This would make you crash and in writing production code it would be important to avoid this for security reasons.
You also do not seem to initialize r[0].y. As far as I understood, you should have a r[0].y=0 somewhere.
Anyway, you using the first element of the array to do something different is definitely a bad idea. It makes your code horribly complex to understand. Just create a new structure, holding the array size, the capacity, and the pointer.
I'm trying to create a function which takes an array as an argument, adds values to it (increasing its size if necessary) and returns the count of items.
So far I have:
int main(int argc, char** argv) {
int mSize = 10;
ent a[mSize];
int n;
n = addValues(a,mSize);
for(i=0;i<n;i++) {
//Print values from a
}
}
int addValues(ent *a, int mSize) {
int size = mSize;
i = 0;
while(....) { //Loop to add items to array
if(i>=size-1) {
size = size*2;
a = realloc(a, (size)*sizeof(ent));
}
//Add to array
i++;
}
return i;
}
This works if mSize is large enough to hold all the potential elements of the array, but if it needs resizing, I get a Segmentation Fault.
I have also tried:
int main(int argc, char** argv) {
...
ent *a;
...
}
int addValues(ent *a, int mSize) {
...
a = calloc(1, sizeof(ent);
//usual loop
...
}
To no avail.
I assume this is because when I call realloc, the copy of 'a' is pointed elsewhere - how is it possible to modify this so that 'a' always points to the same location?
Am I going about this correctly? Are there better ways to deal with dynamic structures in C? Should I be implementing a linked list to deal with these?
The main problem here is that you're trying to use realloc with a stack-allocated array. You have:
ent a[mSize];
That's automatic allocation on the stack. If you wanted to use realloc() on this later, you would create the array on the heap using malloc(), like this:
ent *a = (ent*)malloc(mSize * sizeof(ent));
So that the malloc library (and thus realloc(), etc.) knows about your array. From the looks of this, you may be confusing C99 variable-length arrays with true dynamic arrays, so be sure you understand the difference there before trying to fix this.
Really, though, if you are writing dynamic arrays in C, you should try to use OOP-ish design to encapsulate information about your arrays and hide it from the user. You want to consolidate information (e.g. pointer and size) about your array into a struct and operations (e.g. allocation, adding elements, removing elements, freeing, etc.) into special functions that work with your struct. So you might have:
typedef struct dynarray {
elt *data;
int size;
} dynarray;
And you might define some functions to work with dynarrays:
// malloc a dynarray and its data and returns a pointer to the dynarray
dynarray *dynarray_create();
// add an element to dynarray and adjust its size if necessary
void dynarray_add_elt(dynarray *arr, elt value);
// return a particular element in the dynarray
elt dynarray_get_elt(dynarray *arr, int index);
// free the dynarray and its data.
void dynarray_free(dynarray *arr);
This way the user doesn't have to remember exactly how to allocate things or what size the array is currently. Hope that gets you started.
Try reworking it so a pointer to a pointer to the array is passed in, i.e. ent **a. Then you will be able to update the caller on the new location of the array.
this is a nice reason to use OOP. yes, you can do OOP on C, and it even looks nice if done correctly.
in this simple case you don't need inheritance nor polymorphism, just the encapsulation and methods concepts:
define a structure with a length and a data pointer. maybe an element size.
write getter/setter functions that operate on pointers to that struct.
the 'grow' function modifies the data pointer within the struct, but any struct pointer stays valid.
If you changed the variable declaration in main to be
ent *a = NULL;
the code would work more like you envisioned by not freeing a stack-allocated array. Setting a to NULL works because realloc treats this as if the user called malloc(size). Keep in mind that with this change, the prototype to addValue needs to change to
int addValues(ent **a, int mSize)
and that the code needs to handle the case of realloc failing. For example
while(....) { //Loop to add items to array
tmp = realloc(*a, size*sizeof(ent));
if (tmp) {
*a = tmp;
} else {
// allocation failed. either free *a or keep *a and
// return an error
}
//Add to array
i++;
}
I would expect that most implementations of realloc will internally allocate twice as much memory if the current buffer needs resizing making the original code's
size = size * 2;
unnecessary.
You are passing the array pointer by value. What this means is:
int main(int argc, char** argv) {
...
ent *a; // This...
...
}
int addValues(ent *a, int mSize) {
...
a = calloc(1, sizeof(ent); // ...is not the same as this
//usual loop
...
}
so changing the value of a in the addValues function does not change the value of a in main. To change the value of a in main you need to pass a reference to it to addValues. At the moment, the value of a is being copied and passed to addValues. To pass a reference to a use:
int addValues (int **a, int mSize)
and call it like:
int main(int argc, char** argv) {
...
ent *a; // This...
...
addValues (&a, mSize);
}
In the addValues, access the elements of a like this:
(*a)[element]
and reallocate the array like this:
(*a) = calloc (...);
Xahtep explains how your caller can deal with the fact that realloc() might move the array to a new location. As long as you do this, you should be fine.
realloc() might get expensive if you start working with large arrays. That's when it's time to start thinking of using other data structures -- a linked list, a binary tree, etc.
As stated you should pass pointer to pointer to update the pointer value.
But I would suggest redesign and avoid this technique, in most cases it can and should be avoided. Without knowing what exactly you trying to achieve it's hard to suggest alternative design, but I'm 99% sure that it's doable other way. And as Javier sad - think object oriented and you will always get better code.
Are you really required to use C? This would be a great application of C++'s "std::vector", which is precisely a dynamically-sized array (easily resizeble with a single call you don't have to write and debug yourself).