C: how to initialise a vector - c

I would like to initalise a vector of type
struct vector
{
size_t capacity;
size_t size;
int *data;
};
I did this but i'm not sure:
struct vector *vector_new();
{
struct vector *vect;
vect = malloc (1*sizeof(vect));
vect->capacity = 1;
vect->size = 0;
int *data;
data = malloc (1*sizeof(int));
}
I think I need to allocate 2 memory spaces, one that holds the 'struct vector' and one for the data. Should I use malloc() or calloc() ?
Moreover how could I throw an error if there is not enough memory ? Thanks

Your code should look somehow like this:
#define INITIAL_CAPACITY 10 // initial capacity, choose some appropriate value
// between 1 and N
struct vector *vector_new();
{
struct vector *vect = malloc (sizeof(struct vector));
vect->capacity = INITIAL_CAPACITY;
vect->size = 0;
vect->data = malloc(sizeof(int) * INITIAL_CAPACITY);
return vect;
}
This was the easy part, now you need to write the functions that allow to put some data into the vector, then you'll probably want to use the realloc function when the size becomes larger than the capacity.
No error checking is done in this code for brevity.
For error handling you could return NULL from the vector_new function.

Related

Freeing up struct created with malloc who's member also had a malloc

Let's say I have a struct for implementing vectors in C like this:
struct cvector {
unsigned int size; // indicates number of element in the vector
unsigned int capacity; // indicates length of the array
int* data; // array to store the actual data
};
typedef struct cvector* cvector;
Then I create this vector like this:
cvector cvector_create() {
cvector retval = (cvector)malloc(sizeof(struct cvector));
retval->capacity = 8;
retval->size = 0;
retval->data = (int*)malloc(retval->capacity * sizeof(int));
return retval;
}
I use malloc for both allocating memory for the struct and for allocating memory for the internal int array.
For freeing up my cvector I use this:
void cvector_free(cvector vector) {
free(vector);
}
My question is, do I need to free the internal int array as well separately like this: free(vector->data) or is freeing up only the struct is enough?
Yes, you need to free also vector->data, the rule is: one call to free per each call to malloc
if you are under C99, you can use flexible array members:
struct cvector {
unsigned int size; // indicates number of element in the vector
unsigned int capacity; // indicates length of the array
int data[]; // array to store the actual data
};
Notice that int data[]; must be the last member of the struct.
Then, you reserve space in this way:
cvector cvector_create() {
cvector retval = malloc(sizeof(struct cvector) + (sizeof(int) * 8));
retval->capacity = 8;
retval->size = 0;
return retval;
}
Now, calling free(vector) is enough since vector and vector->data are on the same block.

how can i solve this when i try to return a 2d struct from a function

this is how i declare this struct
typedef struct cache{
int vaild;
char* tag;
char* data;
}cache;
this is part of my main which called this function
struct cache **cacheA = createCache(Setnum,(int)pow(2,blocksize),cachesize);
struct cache **cacheB = createCache(Setnum,(int)pow(2,blocksize),cachesize);
and now this is my called function
struct cache ** createCache(int numset, int blocksize, int cachesize){
int numcache = (int)((cachesize/blocksize)*numset);
struct cache out[numset][numcache];
int i,j;
for (i=0; i < numset; i++){
for (j=0; j < numcache; j++){
out[i][j].tag = "0";
out[i][j].vaild = 0;
out[i][j].data ="0";
}
}
return out;
}
and when i try to compile this, it tells me that
return from incompatible pointer type
function returns address of local variable
(which points to the line "return out;")
I have no idea whats wrong with my code, i mean the type of the function return is the same as how i declear "out", so what causes this problem?
You create struct cache out[numset][numcache];
within the function prototyped as: struct cache ** createCache(...).
Then attempt to return out.
It is because struct cache [][] is typed differently than struct cache ** that you are getting the return errors.
Other comments:
1) If you truly do want a pointer to pointer to struct, then malloc or calloc will need to be used at some point to allocate memory.
2) the char * members of the struct also need to be assigned memory before assigning values. For illustration below, they are changed to char []
3) assigning values to strings does not work by using = assignment operator. Use a string function such as strcpy, sprintf, etc.
4) you've named the struct with the same symbol as that of the new type you have created, i.e. cache. In this application, the name cache is not necessary. Also, purely for style, I show the new type in CAPS. This is not necessary, but just a style I use to make the new type more recognizable in code.
In consideration of the comments above, the struct could be changed to the following:
typedef struct { /// don't need name here when it in this application
int vaild;
//char *tag;
char tag[20];//for illustration, to avoid additional dynamic allocation of memory
//char* data;
char data[80];
}CACHE;//capitalization is style only, not a necessity here.
Note, there is no name, but the new type CACHE was created. Now, you can create the function createCache:
CACHE ** createCache(int ncache, int nset)//note for simplicity of freeing this
//object later, simplify number of arguments
{
CACHE **out;
out = calloc(ncache, sizeof(CACHE *));//create array of pointers to CACHE
if(!out) return NULL;
int i;
for (i=0; i < nset; i++)
{
out[i] = calloc(nset, sizeof(CACHE));//create space for each instance
//of CACHE pointed to by array pointers
}
return out;
}
Anytime memory is created on the heap, it needs to be freed. This method will free the CACHE object memory:
void freeCashe(CACHE **a, int nset)
{
int i;
for(i=0; i<nset; i++)
{
if(a[i])free(a[i]);
}
if(a)free(a);
}
Calling these functions as shown below, will create an array of pointers, each pointing to an instance of CACHE where you can use them as intended, then free all of the memory when finished:
int main(void)
{
int cachesize = 20;
int blocksize = 20;
int numset = 10;
//move the calculation out of creation function
//to simplify freeing object later.
int numcache = (int)((cachesize/blocksize)*numset);
CACHE **a = createCache(numcache, numset);
/// use a, then free a
freeCashe(a, numset);
return 0;
}
Your function needs to allocate the memory on the heap rather than the stack. You will need to allocate space on the heap for your array of pointers, and for what they point too.
struct cache ** createCache(int numset, int blocksize, int cachesize){
cache ** out;
int numcache = (int)((cachesize/blocksize)*numset);
size_t headerSize = sizeof(*out)*numset;
size_t bodySize = sizeof(**out)*numcache;
out = malloc(headerSize + (bodySize*numset));
if (out == NULL) {
/* Should probably output some message about being
* insufficient memory here. */
return NULL;
}
int i,j;
for (i=0; i < numset; i++){
/* need to assign our point */
out[i] = (cache*)(((char*)out)+(headerSize+bodySize*i));
for (j=0; j < numcache; j++){
out[i][j].tag = "0";
out[i][j].vaild = 0;
out[i][j].data ="0";
}
}
return out;
}
/* importantly, you want a way to free your allocated memory */
void destroyCache(cache ** ptr) {
free(ptr);
}
PS: You don't have to typedef your struct if you reference it with the struct keyword.
You are wanting a pointer pointer type to be returned, but in order to do that you need to dynamically allocate it. Local stack allocations (i.e. struct cache[x][y]) won't work. You will either get an error or your program will crash when attempting to use the 2D array.
The solution is to either pre-allocate space and pass it in to the function or allocate in the function itself.
Allocation In Function Example:
struct cache ** createCache(int numset, int blocksize, int cachesize){
int numcache = (int)((cachesize/blocksize)*numset);
struct cache **out = malloc(sizeof(struct cache *) * numset); // This line changed.
int i,j;
for (i=0; i < numset; i++){
out[i] = malloc(sizeof(struct cache) * numcache); // This line added.
for (j=0; j < numcache; j++){
out[i][j].tag = malloc(sizeof(char)); // This line added.
out[i][j].data = malloc(sizeof(char)); // This line added.
strcpy(out[i][j].tag, "0");
out[i][j].vaild = 0;
strcpy(out[i][j].data, "0");
}
}
return out;
}

Allocating memory to my stack dynamically (only if needed)

I need to allocate memory to an array inside my struct, this array has no defined size at the beginning when i define the struct:
typedef struct stacks {
int size; // Stores the size of my -values- array
int sp; //points to the top of the stack, my stackpointer
int *values;
} STACKS;
So, to initialize my struct i wrote this function, that allocates (using calloc?) memory to my array, and i put inside SIZE variable, the new size of my array .
#define MAXIMUM 10
int initStacks(STACKS *s){
s->values = calloc(MAXIMUM,sizeof(int));
s->size = MAXIMUM;
s->sp = 0;
return 0;
}
Now, if i want to push something to the top of the stack (LIFO) i use this:
int pushs(STACKS *s, int x){
if (s->sp==s->size) {
realloc(s->values, MAXIMUM * sizeof(int));
s->size*=2;
}
s->values[s->sp]=x;
s->sp++;
}
Is this the correct way of doing this?
Is realloc working as it should in my function?
Thank you very much for your help!
EDIT:
would this make more sense? This way, i really don't need to declare the value of the array, being that defined with #define maximum 10
typedef struct stacks {
int size; // guarda o tamanho do array valores
int sp;
int *values;
} STACKS;
int initStacks(STACKS *s){
s->values = calloc(1,sizeof(int));
s->size = 1;
s->sp = 0;
return 0;
}
int isEmptys(STACKS *s){
return((s->sp)==0);
}
int pushs(STACKS *s, int x){
s->size++;
realloc(s->values, s->size * sizeof(int));
s->values[s->sp]=x;
s->sp++;
}
Assuming you have an original size factor (the name capacity would be as-appropriate, if not more so), your original code lacks several things:
Compares the size against a constant, rather than the current sp against the stack current size.
Does not save nor test the return result of realloc
Does not actually double the allocation (you're missing the 2x in the realloc expression.
Declares an int return result, but no such return exists.
Has no way of communicating back to the caller the push result (success or not). That missing return result would be ideal for this, btw.
Addressing all of these:
int pushs(STACKS *s, int x)
{
if (s->sp == s->size)
{
void *pv = realloc(s->values, 2 * s->size * sizeof *(s->values));
if (pv != NULL)
{
s->values = pv;
s->size *= 2;
}
else
{
fprintf(stderr, "Failed to resize stack\n");
return -1;
}
}
s->values[s->sp++] = x;
return 0;
}
Untested, but hopefully close enough.
Best of luck
Although not directly an answer to the actual question, but more to the general problem, I post this as it does not fit into a comment.
If you expect excessive push/pop operations and memory usage, the following might be an alternative:
typedef struct SubStack_s {
struct SubStack_s *prev;
int data[ENTRIES_PER_SEGMENT];
} SubStack;
typedef struct {
SubStack *tos; // init to NULL
size_t sp; // init to 0
} Stack;
The basic idea is to push elements onto each substack until full (as you already do). If the current one is full, you alloc a new one, chain them (new->prev = old) and continue with the new one (storing new to Stack.tos)
Pop works similar, free'ing each substack once it is not used anymore.
That concept is called "fragmented stack". It is much more efficient than the realloc-approach (it avoids copying) and does not fragment RAM as all block are of equal size. Oh, and it allows to have pointers into the stack, which the realloc-varaint does not, because the address of the stack can change.

Writing to Dynamic Memory

I have the following pointer to structure
struct ALIST
{
short sPeriod;
long lDate;
}*list_ptr;
list_ptr = malloc(sizeof(*list_ptr));
Now if I have a global variable sIndex which I initialize to zero, is it possible to do this?
(list_ptr + sIndex)->sPeriod = period_variable;
(list_ptr + sIndex)->lDate = date_variable;
sIndex++
Is there a more efficient method?
This looks like you want to allocate a dynamic array. Make a size variable and set it to your starting size for the array.
Something like:
size_t list_size = 10;
struct ALIST list_ptr = 0;
size_t i;
list_ptr = malloc(sizeof(*list_ptr) * list_size);
for(i=0; i<list_size; ++i) {
list_ptr[i].sPeriod = period;
list_ptr[i].lDate = date;
}
Now, if you don't know the size of the array then what you want ends up looking a lot like a C++ std::vector.
I'd build a C version that wraps the necessary information in a struct. Use realloc to resize it.
It might look like (NOTE THAT THIS IS COMPLETELY UNTESTED):
struct dynamic_ALIST {
struct ALIST *array;
size_t size;
size_t capacity;
};
void dynamic_ALIST_construct(struct dynamic_ALIST *x, size_t initial_size)
{
x->array = 0;
x->size = 0;
x->capacity = 0;
dynamic_ALIST_reserve(x, initial_size);
}
void dynamic_ALIST_reserve(struct dynamic_ALIST *x, size_t size)
{
struct ALIST *tmp = realloc(x->array, sizeof(*tmp) * size);
if(!tmp) abort();
x->array = tmp;
x->capacity = size;
}
struct ALIST* dynamic_ALIST_get(struct dynamic_ALIST *x, size_t offset)
{
if(offset < x->size) {
return x->array + offset;
}
if(offset < x->capacity) {
x->size = offset + 1;
return x->array + offset;
}
dynamic_ALIST_reserve(x, offset+1);
return dynamic_ALIST_get(x, offset);
}
Then you could use it like:
void f()
{
size_t item_index = 0;
struct dynamic_ALIST list;
FILE *fp = fopen("filename");
dynamic_ALIST_construct(list, 0);
while( read_item(fp, dynamic_ALIST_get(list,item_index)) ) {
item_index++;
}
fclose(fp);
}
You can make all kinds of changes to that. The get function might return an error instead of automatically creating new entries. You might make another function that increases the size. You might want to have a function that sets all the values to zero before returning new memory.
If you have a lot of different structs to wrap up you can put ALL of the above dynamic_ALIST struct, and construct, reserve, get functions into a macro. If you do it right then you just say:
NEW_DYNAMIC_STRUCT(ALIST);
And the preprocessor spits out a whole new copy with all the names changed.
I'll answer point by point:
Do those pointer manipulations only if you know what you are doing.
Assuming sIndex to be an int, with sIndex=0;, it is no problem but if you increment sIndex, you don't have that space allocated to use becuase you have malloc'd just one block.
You need to first do your allocation appropriately if you need to access multiple such blocks then:
list_ptr = malloc(sizeof(struct ALIST)*N); //replace N with the number of blocks you want

Printing a member of an array within a struct within a struct?

Here I have a struct:
typedef struct Memo
{
// dynamically allocated HugeInteger array to store our Fibonacci numbers
struct HugeInteger *F;
// the current length (i.e., capacity) of this array
int length;
} Memo;
and this is the struct HugeInteger* within the Memo struct:
typedef struct HugeInteger
{
// a dynamically allocated array to hold the digits of a huge integer
int *digits;
// the length of the array (i.e., number of digits in the huge integer)
int length;
} HugeInteger;
My question is how can I access a member of the digits array within the Hugeinteger struct within the Memo struct?
I have malloced all three like so throughout my code:
Memo *newMemo = malloc(sizeof(Memo));
newMemo->F = malloc(sizeof(HugeInteger) * INIT_MEMO_SIZE); //in this case 44
for (i = 0; i < INIT_MEMO_SIZE; i++)
{
newMemo->F[i].digits = malloc(sizeof(int*) * 1); //creating an array of size 1 to test
newMemo->F[i].digits = NULL;
newMemo->F[i].length = 0;
}
I have tried for example...
newMemo->F[i].digits[0] = 1;
...which results in a segmentation fault. How can I implement the above line of code correctly? I really feel like i'm missing something important here. Thanks.
There's a problem right here:
newMemo->F[i].digits = malloc(sizeof(int) * 1); //creating an array of size 1 to test
newMemo->F[i].digits = NULL;
(Besides the syntax error that I fixed which I assume was a copy/paste error) The second line above replaces the memory address you just allocated with NULL. So that when you do this:
newMemo->F[i].digits[0] = 1;
You're writing to a NULL address.
You want to leave out the NULL assignment.

Resources