i have decleard a structure and allocate some memory too . using a function i update datas . i got error segmentation fault when i acssing data.
This is my code
In headerfile :
typedef struct
{
int member;
char *name;
}place;
void update(place **,int);
void display(place **,int);
in function
static memallocate(place **ptr,int viname,int index)
{
ptr[index]=(place *)malloc(sizeof(place));
ptr[index]->name=(char *)malloc(viname*sizeof(char *));
}
void update(place **ptr,int index)
{
---read string value "na" find the strlen as "pp"---
memallocate(ptr,pp,index);
ptr[index]->name=na;
}
void display(place **ptr,int index)
{
int i;
for(i=0;i<index;i++)
{
printf("%s\n",ptr[i]->name);
printf("%s\n",ptr[i]->country);
}
}
in main file :
void main()
{
int index=0;
place *pla[5]={NULL};
while(index<2)
{
update(&pla[index],index);
index++;
}
display(pla,index);
}
my problem is i got segmentation fault when acessing function display and can't print datas ptr[0]->name,ptr[0]->country,ptr[1]->name,ptr[1]->country ..why this happen ? any memory fault . I got printing when i use printf after each updation .
I see two mayor issues here.
1st
Here
static void memallocate(place **ptr,int viname,int index)
{
ptr[index]=(place *)malloc(sizeof(place));
ptr[index]->name=(char *)malloc(viname*sizeof(char *));
}
you allocate too much memory. It shall be
static void memallocate(place ** ptr, int viname, int index)
{
ptr[index] = malloc(sizeof(place));
ptr[index]->name = malloc(viname * sizeof(char));
}
or even better:
static int memallocate(place ** ptr, size_t viname, size_t index)
{
int result = 0;
if (NULL == ptr)
{
result = -1;
errno = EINVAL;
}
else
{
ptr[index] = malloc(sizeof *ptr[index]);
if (NULL == ptr[index])
{
result = -1;
}
else
{
ptr[index]->name = malloc(viname * sizeof *(ptr[index]->name));
if (NULL == ptr[index]->name)
{
result = -1;
free(ptr[index]);
}
}
}
return result;
}
2nd
Then here (assuming na to be a char* properly initilaised to reference a C-"string")
void update(place **ptr,int index)
{
---read string value "na" find the strlen as "pp"---
memallocate(ptr,pp,index);
ptr[index]->name=na;
}
you overwrite what you just assigned to name. To copy a C-"string" use strcpy().
int update(place ** ptr, size_t index)
{
---read string value "na" find the strlen as "pp"---
int result = memallocate(ptr, pp, index)
if (-1 == result)
{
perror("memallocate() failed");
}
else
{
strcpy(ptr[index]->name, na);
}
return result;
}
Then call it like this:
int main(void)
{
size_t index = 0;
place * pla[5] = {NULL};
/* Loop over all array's elements. */
while (index < sizeof pla/sizeof *pla)
{
update(pla, index);
++index;
}
...
}
Notes:
Always check the outcome of relevant function calls (here malloc()) and design your functions to be able to pass failures up to the caller.
Do not cast the result of malloc(), calloc() and realloc() in C. It is not needed nor recommended.
Prefer using size_t over int for memory sizes and indexes. size_t does not waste a bit for negative numbers and it is guaranteed to be large enough to address any arrays' element or represent any memory size. sizeof as well as strlen() return size_t not int for example.
When you call your update(), you are passing a place ** of the current index as argument.
However, you nevertheless pass index too and later in your memallocate() allocate memory as if it was a pointer to the place *[].
So it should help to remove the parameter index from update() and memallocate() and change the memory allocation to something like:
*ptr = (place *)malloc(sizeof(place));
*ptr->name = (char *)malloc(viname*sizeof(char *));
Related
Hello i'implementing a smart vector in c, and i'm having problems with the reallocation of the buffer.
this is the struct that contains the array and its infos:
struct _vector
{
item* vec;
size_t elements;
size_t size;
};
item is just a typedef that in this case happens to be int.
I made several function to manage the array, but the one that should resize it, gives me problems.
(Vector is also a typedef for struct _vector* by the way)
This is the function:
void insertVector(const Vector vec,const int pos,const item a)
{
if(vec->elements==vec->size)
{
item* temp=realloc(vec->vec,(vec->size*2)*sizeof(item));
if(temp==NULL)
{
puts("Error: space unavailable");
return;
}
//vec->vec=realloc(vec->vec,(vec->size*2)*sizeof(item));
vec->vec=temp;
vec->size*=2;
}
int size=vec->elements;
if(pos>=0&&pos<=size)
{
for(int i=size;i>pos;i--)
{
vec->vec[i]=vec->vec[i-1];
}
vec->vec[pos]=a;
vec->elements+=1;
printf("size is %lu\nelements are %lu\n",vec->size,vec->elements);
}
}
I just shift the contents to make space for the new element, and it works fine, the problem is when the array is reallocated.
when the number of valid elements is equal to the actual size of the array,
i do a realloc to double the actual size.
As soon as that if activates though the realloc makes the program crash with this error:incorrect checksum for freed object.
The problem is in the if, because it only crashes when the size and elements are equal, if i comment out that section, everything works
I don't know what could it be.
EDIT:
The functions that i used to create and the initialise the instance i'm working with are:
Vector newVector(void)
{
Vector new=malloc(sizeof(*new));
new->vec=NULL;
new->elements=0;
new->size=0;
return new;
}
and
void initVector(const Vector vec,const size_t size)
{
vec->vec=calloc(size,sizeof(item));
vec->elements=size;
vec->size=size*2;
}
Based of your comment
I created a new vector setting to zero every field, then i used this function:
void initVector(const Vector vec,const size_t size)
{
vec->vec=calloc(size,sizeof(item));
vec->elements=size;
vec->size=size*2;
}
I think you are treating the size and the number of elements incorrectly. The
initVector function just allocates memory for the vec->vec array, so
vec->elements should be 0, not size. And vec->size should be size, not
size*2. So the correct function should be
// remove the const, you are modifying the data vec is pointing to
int initVector(Vector vec, size_t size)
{
if(vec == NULL)
return 0;
vec->vec = calloc(size, sizeof *vec->vec);
if(vec->vec == NULL)
return 0;
vec->elements = 0;
vec->size = size;
return 1;
}
Now the insertVector would only allocate new space, when all allocated spaces
are used.
And I suggest you use memmove to copy the memory:
// again, remove the const here
int insertVector(Vector vec, const size_t pos, const item a)
{
if(vec == NULL)
return 0;
if(vec->elements==vec->size)
{
item* temp=realloc(vec->vec,(vec->size*2)*sizeof *temp);
if(temp==NULL)
{
fprintf(stderr, "Error: space unavailable\n");
return 0;
}
vec->vec=temp;
vec->size*=2;
}
// I use vec->elements as upper limit,
// otherwise you could have "holes" in the array and
// you wouldn't realize it.
if(pos < 0 || pos > vec->elements)
{
fprintf(stderr, "invalid position\n");
return 0;
}
memmove(vec->vec + pos + 1, vec->vec + pos, (vec->elements - pos) * sizeof *vec->vec);
vec->vec[pos] = a;
vec->elements += 1;
printf("size is %lu\nelements are %lu\n",vec->size,vec->elements);
return 1;
}
In your initVector function, you set the size incorrectly, to two times what you allocated with calloc. This memory then gets overwritten as you are adding new elements and this is the reason the free fails when you finally invoke realloc. Change initVector to:
void initVector(const Vector vec,const size_t size)
{
vec->vec=calloc(size,sizeof(item));
vec->elements=size;
vec->size=size;
}
const static int VECTOR_BASIC_LENGTH = 20;
struct m_vector
{
void* my_vector;
size_t my_capacity;
size_t my_head;
};
typedef struct m_vector Vector;
Vector creat_Vector(size_t size,void *judge)
{
Vector _vector;
size = size?size:VECTOR_BASIC_LENGTH;
_vector.my_capacity = size;
_vector.my_head = 0;
//How I write the following two lines
_vector.my_vector = malloc(sizeof(*judge) * size);
return _vector;
}
The type of judge is uncertain,so I pass a void pointer as a parameters.I need the size of *judge to allocate memory to _vector.my_vector,for example if I use:
int *a;
creat_Vector(5,a);
I want the following line:
_vector.my_vector = malloc(sizeof(*judge)*size);
is equal to:
_vector.my_vector = malloc(sizeof(*a)*5);
How could I achieve this function.Using pure C
There is a forbidden thing done in your code.
You statically (at compile time) allocate/declare a local _vector of type Vector in your function creat_Vector. Then you return this object to the outside world. However, when you are exiting your function, all local data is dead. So, you should absolutely rethink this.
One suggestion would be:
int init_Vector(Vector* _vect, size_t size, unsigned int ptr_size)
{
size = size?size:VECTOR_BASIC_LENGTH;
_vect->my_capacity = size;
_vect->my_head = 0;
_vect->my_vector = malloc(size*ptr_size);
if (_vect->my_vector) {
return 0;
}
return 1;
}
Then:
Vector _vector;
char *a;
if (init_Vector(&_vector, 5, sizeof(char)) == 0) {
printf("Success!\n");
}
else {
printf("Failure!\n");
/* treat appropriately (return error code/exit) */
}
/* do whatever with a (if needed) and _vector*/
In my binary search tree I want to create a function that can get all words starting with a prefix and store all words in an array called results
this is my tree
struct BinarySearchTree_t
{
char *mot,*def;
struct BinarySearchTree_t *left;
struct BinarySearchTree_t *right;
};
typedef struct BinarySearchTree_t BinarySearchTree;
my function :
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, char*** results)
{
BinarySearchTree *tmp;
tmp=tree;
static int size=0;
if (!tmp)
return 0;
else if (strncmp(tmp->mot,prefix,strlen(prefix))==0)
{
(*results)= realloc(*results,(1+size)*sizeof(*(*results)));
(*(*results+size))= malloc(strlen(tmp->mot)*sizeof(char));
strcpy((*results)[size],tmp->mot);
size++;
return (1 + findWordsByPrefix(tmp->left,prefix, &results) + findWordsByPrefix(tmp->right,prefix, &results));
}
else
return (strncmp(tmp->mot,prefix,strlen(prefix))<0)?findWordsByPrefix(tmp->right,prefix, &results):findWordsByPrefix(tmp->left,prefix, &results) ;
}
This function should return a number of words starting with the given prefix.
my problem is that the program crash when it is run , and I don't how to resize my array results
so every time I found a word I should increase the size of the results array .
and I would know how exacly manipulate the pointer of pointer of pointer given in arg of this function (char ***results) : what exactly means?
If I simply compile your code, I get severe compiler warnings including:
1>binarysearchtree.c(98) : warning C4047: 'function' : 'char ***' differs in levels of indirection from 'char ****'
1>binarysearchtree.c(98) : warning C4024: 'findWordsByPrefix' : different types for formal and actual parameter 3
This alone will cause a crash -- you are calling your own function recursively with the wrong arguments.
Next, I believe you need to allocate one more than the length of the string, to hold a copy of a string:
malloc((strlen(tmp->mot) + 1 )*sizeof(char))
Next, you're passing around an array of strings of variable size -- and storing the size in a static variable. It's impossible to know if this will work, so don't do it.
Instead, if you want to use a dynamic array of strings, I suggest extracting out a struct to hold them, like so:
struct ResultTable_t
{
int size;
char **results;
};
typedef struct ResultTable_t ResultTable;
void InitializeResults(ResultTable *p_table)
{
p_table->size = 0;
p_table->results = NULL;
}
void AddResult(ResultTable *p_table, char *result)
{
if (result == NULL)
return;
p_table->size++;
p_table->results = realloc(p_table->results, p_table->size * sizeof(*p_table->results));
p_table->results[p_table->size-1] = malloc((strlen(result) + 1) * sizeof(**p_table->results));
strcpy(p_table->results[p_table->size-1], result);
}
void FreeResults(ResultTable *p_table)
{
if (p_table->results != NULL)
{
int i;
for (i = 0; i < p_table->size; i++)
{
free(p_table->results[i]);
}
free(p_table->results);
}
p_table->size = 0;
p_table->results = NULL;
}
(As an improvement, you might consider using geometric growth instead of linear growth for your table of results.)
Then your function becomes:
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, ResultTable *p_table)
{
if (!tree)
return 0;
else if (strncmp(tree->mot,prefix,strlen(prefix))==0)
{
AddResult(p_table, tree->mot);
return (1 + findWordsByPrefix(tree->left,prefix, p_table) + findWordsByPrefix(tree->right,prefix, p_table));
}
else if (strncmp(tree->mot,prefix,strlen(prefix))<0)
{
return findWordsByPrefix(tree->right,prefix, p_table);
}
else
{
return findWordsByPrefix(tree->left,prefix, p_table);
}
}
And you would use it like:
ResultTable results;
InitializeResults(&results);
// Get some prefix to search for.
char prefix = GetSomePrefix();
int size = findWordsByPrefix(tree, prefix, &results);
// Do something with the results
// Free all memory of the results
FreeResults(&results);
Update
If the ResultTable is distasteful for some reason, you can pass the dynamic array and array sizes in directly:
void AddResult(char ***p_results, int *p_size, char *word)
{
if (word == NULL)
return;
(*p_size)++;
(*p_results) = realloc(*p_results, ((*p_size)+1) * sizeof(**p_results));
(*p_results)[(*p_size)-1] = malloc((strlen(word) + 1) * sizeof(***p_results));
strcpy((*p_results)[(*p_size)-1], word);
}
void FreeResults(char ***p_results, int *p_size)
{
int i;
if (p_results == NULL || *p_results == NULL)
return;
for (i = 0; i < (*p_size); i++)
{
free ((*p_results)[i]);
}
free (*p_results);
*p_results = NULL;
*p_size = 0;
}
size_t findWordsByPrefix(BinarySearchTree* tree, char* prefix, char ***p_results, int *p_size)
{
if (!tree)
return 0;
else if (strncmp(tree->mot,prefix,strlen(prefix))==0)
{
AddResult(p_results, p_size, tree->mot);
return (1 + findWordsByPrefix(tree->left,prefix, p_results, p_size) + findWordsByPrefix(tree->right,prefix, p_results, p_size));
}
else if (strncmp(tree->mot,prefix,strlen(prefix))<0)
{
return findWordsByPrefix(tree->right,prefix, p_results, p_size);
}
else
{
return findWordsByPrefix(tree->left,prefix, p_results, p_size);
}
}
and use like:
char **results = NULL;
int tablesize = 0;
// Get some prefix to search for.
char prefix = GetSomePrefix();
int size = findWordsByPrefix(tree, prefix, &results, &tablesize);
// Do something with the results
// Free all memory of the results
FreeResults(&results, &tablesize);
This function appends a desired value to the end of the array. When I tested the code, I used the value 100776, but when I printed out the array, the value of the last element was 135009, which is completely different than what my desired value was. Does anybody know why that's the case?
Here is the struct for my append function:
typedef struct {
int* data;
unsigned int len;
} intarr_t;
And this is my actual append function:
intarr_result_t intarr_push( intarr_t* ia, int val )
{
unsigned int len = ia->len;
if (ia == NULL)
{
return INTARR_BADARRAY;
}
else
{
ia->data = realloc(ia->data, (sizeof(int)*len+1));
if (ia->data != 0)
{
ia->data[len+1]=val;
ia->len=len+1;
assert (ia->data);
return INTARR_OK;
}
else
{
return INTARR_BADALLOC;
}
}
return 0;
}
given you have:-
(sizeof(int)*len+1)
then this is off the end of your array :-
ia->data[len+1]=val;
for two reasons :-
you haven't allocated enough memory because the + 1 occurs after the multiplication.
if you allocate 1 then your first spot is data[0] not data[1].
so you should do :-
a->data = realloc(ia->data, (sizeof(int)*(len+1)));
and
ia->data[len]=val;
ia->len++;
and your last item is ia->data[ia->len-1];
typedef char* string;
int func1(string s);
char* func2(); // returns a new memory/
if(func1(func2()) == 4)
{
// code
}
Assuming func2() is only needed in the condition. Since i need to free newly allocated memory, how can i free it up within the same line(i.e. with the same condition or paranthesis) ? My motiviton for this is to keep the code clean.
EDIT 1.
Yes it is a c question. The use of "string" type was error on my part as i have always typedef it to char*. Sorry for the confusion.
To do this cleanly, make a new function that does the work in a clear manner:
static int func3()
{
char *s = func2();
int result = func1(s);
free(s);
return result;
}
…
if (func3() == 4)
…
(Presumably, there is some assurance that func2 successfully allocates memory. If not, you must test its return value.)
Free it in the same line with no new function definitions:
int result;
char *temp;
/* comma operator: evaluate these 4 expressions left-to-right,
and the value is the value of the last expression */
if(temp = func2(), result = (func1(temp) == 4), free(temp), result)
{
/* Do things */
}
Cleaner code:
int func3(void)
{
char *temp;
int result;
temp = func2();
result = func1(temp);
free(temp);
return result;
}
/* ... */
if(func3() == 4)
{
/* do things */
}
Here is a solution using a functional approach:
int apply_free(int (*f1)(char*), char * (*f2)()) {
char *s = f2();
if (s != NULL) {
int result = f1(s);
free(s);
return result;
}
else {
return -1; /* or any meaningful value if f2 returned a NULL pointer */
}
}
if (apply_free(func1, func2) == 4)
{
// code
}
This assumes your various cases will have the same type signature.