Realloc no execute - c

First of all,the names of variables are in greek.
It's impossible to saw all the code,because is many files.
However a have a struct
typedef struct{
TTamias* Tamies;
}TPinakasTamiwn;
And TTamias is type
typedef struct{
int time_busy; /*xronos apasxolhshs tou tamia*/
int time_inactive; /*xronos pou o tamias einai adranhs*/
int arithos_pelaton; /*posous pelates eksipiretise o tamias*/
int enapomenon_xronos; /*enapomenon xronos eksipiretisi enos pelath*/
}TTamias;
With this function in main i create an array
void DimourgiaTamiwn(TPinakasTamiwn* tamias)
{
tamias->Tamies = (TTamias*)malloc(sizeof(TTamias) * TAMIES);
}
After some comparisons i want to raise the size of array with this function
int ProsthikiTamia(TPinakasTamiwn* tamias,int plithos_tamiwn)
{
TTamias* NeoiTamies;
int neo_plithos = plithos_tamiwn + 1;
NeoiTamies = (TTamias*)malloc(sizeof(TTamias) * neo_plithos);
for(int i = 0; i < plithos_tamiwn; i++)
NeoiTamies[i] = tamias->Tamies[i];
for(int i = neo_plithos - plithos_tamiwn; i < neo_plithos; i++)
TamiasDimiourgia(&NeoiTamies[i]);//fuction to initialize the data member of extra index
tamias->Tamies = (TTamias*)realloc(tamias->Tamies , neo_plithos);// <-----PROBLEM
for(int i = 0; i < neo_plithos; i++)
tamias->Tamies[i] = NeoiTamies[i];
free(NeoiTamies);
return neo_plithos;
}
The function return the new size that is raise than one.
I create a local array and copy to that the main array,
i want to reallocate the main array and copy again the local array to new main array.
Doesn't appear compile error,but in execution (also at debug) the program break at realloc.

When reallocating, you have forgotten to multiply the dimension by the unit size. The correct line shall be:
tamias->Tamies = (TTamias*)realloc(tamias->Tamies , sizeof(TTamias) * neo_plithos);

Related

How to cache part of the data in buffer/ array and have everything else stored in members of data structure in C

I have my pseudocode something like this in C. I have some part of data stored in data structure, but im struggling to have another set of data (based on an if condition) to store in a separate array which is not fixed size. Any suggestion is appreciated.
typedef struct struct1 {
uint32 member1
} PACKED struct1_t
typedef struct struct2 {
struct1_t *member2
} PACKED struct2_t
uint32 curnt_cnt = 0;
for (i=0; i<some_number; i++){
if (cond) {
k = m;
struct2_t->member2[curnt_cnt].member1 = k; #I have no prob writing here
}
else {
k = n;
array[curnt_cnt] = k; ==> Is this even correct implementation?
# I want to store/ book-keep the values of k in an array throughout every iteration of for loop without overwriting the previous value
# Size of the array will not exceed "some_number (mentioned in for loop)" at any time
}
curnt_cnt++;
}
You must create a pointer, since lists in C must have a specific size
int* arr;
arr = (int*)malloc(sizeof(int)*some_number);
and then in your code
else {
k = n;
array[curnt_cnt] = k;
}
will work.

Everytime I declare I function, my variable made by the function changes. C Language

so I am working on my Binary adding program, but now I am stuck. I want to declare 2 variables for the first binary and second one, so I use the getBinary function I created to declare these 2. However, after I entered value for firstBin, I got the value I want, but after I entered the value for secBin, the value of firstBin somehow changes and become the same as secondBin. I was hoping for the variable to be unchangable. Thanks for the help
#include <stdio.h>
int * getBinary(){
int i;
int j;
static int first8bits[8];
for (i = 0; i != 8; i++){
printf("Input 8-bits Binary:");
scanf("%d",&first8bits[i]);
}
for (j = 0; j != 8; j++)
printf("%d",first8bits[j]);
printf("\n");
return first8bits;
}
int main(){
int o;
printf("Input the first Set of Binary...");
const int * firstBin = getBinary();
printf("Input the second Set of Binary...");
int * secBin = getBinary();
for (o = 0; o != 8; o++)
printf("%d",firstBin[o]);
}
Because of the static keyword.
Don't use static. Try:
int* first8bits = malloc(8 * sizeof(int));
This way you will allocate new memory each time you call it. You can still access it with the same [ i ] subscript. Remember to free the memory again at the end of main!
free(firstBin);
free(secBin);

Creating a three dimensional struct array on the heap in c

I am trying to create a three dimensional struct array using malloc in c on MSVC. It compiles without error but when i debug it it gives an error after initializing some elements.
declaration:
typedef struct table_info
{
unsigned long size;
char code[33];
char path[300];
}table_info;
table is a global variable and is defined as:
struct table_info ***table=NULL;
malloc and initialize table:
char garb[33] = { '\0' };
char garb_path[300] = { '\0' };
table = (table_info***)malloc(ROWS* sizeof(**table));
for (int m = 0; m < ROWS; m++)
{
table[m] = (table_info**)malloc(COLS* sizeof(*table[m]));
for (int j = 0; j < COLS; ++j)
{
table[m][j] = (table_info*)malloc(DEPTH * sizeof(table[m][j]));
for (int k = 0; k < DEPTH; ++k)
{
table[m][j][k].size = 0;
strcpy_s(table[m][j][k].code, sizeof(table[m][j][k].code), garb);
memcpy(table[m][j][k].path, garb_path, sizeof(garb_path));
}
}
}
Am I initializing it correctly? or what should I correct to make it work?
The size passed to malloc is incorrect in the following line:
table[m][j] = (table_info*)malloc(DEPTH * sizeof(table[m][j]));
sizeof(table[m][j]) is just sizeof(**table), which is sizeof(table_info *). It should be sizeof(table_info), or alternatively sizeof(*table_info[m][j]) or sizeof(***table_info).
You also don't need to cast the result of malloc, which is generally frowned upon today (at least in C). See this post for more info.
So the following should work (in C):
table[m][j] = malloc(DEPTH * sizeof(*table[m][j]));

Struct array is printing garbage

I'm having an issue with printing out my struct array. It is initialized like so:
struct matrix tokens[nbrState][12];
I then try to print it out with this code:
printf("%d", tokens[0][0].state);
for(int q = 0; q < nbrState; q++){
for(int r = 0; r < 12; r++){
printf("%d", tokens[q][r].state);
}
}
How ever it just gives back
160833216083325909500442637211181530452359314445659095247095039827295039732859091035295039760059091066417471141950397584105931452485931525045870278695909110245869685280135590599950397784950397744593145248159314402459314316859095284905931452481590950044135934508013593144456590952470095039760058696836095039787258698266456147669503978565869965120593144552593143168419536358702168841950960596593143168950398056950398016593144552159315536059315450459095284905931445521015931545041048576587404166341899271605931553609503978729503978563593450804195363-1141298268758698266459314316800419611295039881600950398592587297673016950398176950397984091005908849605884315520000050-100950398592419
And I'm not sure why. I'm also filling the array with values using this for loop.
while ( fscanf ( fp, "%d/%d%c", &index, &separateInt, &separateChar) == 3) {
for(int i = 0; i < 12; i++){
tokens[index][i].state = separateInt;
}
}
You said
It is initialized like so
struct matrix tokens[nbrState][12];
But the above declaration does not initialize anything (unless your array is declared with static storage duration). A local array declared in this fashion will contain garbage at the beginning. That's apparently what you are printing.
If you want your array initialized, you have to initialize it yourself. For example, this declaration
struct matrix tokens[nbrState][12] = { 0 };
will initialize everything with zeros, assuming nbrState is a constant.
If nbrState is not a constant, then you will not be able to use a = { ... } initializer in the declaration. You will have to assign the initial values to your array elements manually, using a cycle or in some other way.

C-Passing an 3d array,allocation and population

let's say I have a functions below.
void function createBands(boolean option) {
int i, j;
int ***bands = (int ***)malloc((SIZE + 1) * sizeof(int **));
for (i = 0; i < SIZE; i++) {
bands[i] = (int **)malloc(HEIGHT * sizeof(int *));
for (j = 0; j < HEIGHT; j++)
bands[i][j] = (int *)malloc(WIDTH * sizeof(int));
}
iterator *it =
createIterator(params); // do not be confused it is a structure with
// methods andaeribute just like Iterator class in
// java . Methods are poniters to functions.
repare_array(bands[Size], it);
}
void prepare_array(int **band, iterator *it) { read_array(band, it); }
read_array(int **band, iterator *it) {
for (int i = 0; i < Height; i++)
band[i] = (int *)it->next();
}
// Now in Iterator.c I have the iterator structure with the methods etc I will
// write just some line form iterator.
void *next() {
byte *b =
a function that reads bytes form a file and returns byte * CORECTLY !!!;
return b == NULL ? NULL : bytetoT(b);
// this function make void form byte conversion but it doesnt work so I make
// only a cast in read_aray as you see. SUppose just return b wich is byte(i
// know in C isn't any byte but I redeclared all the types to be JAVA.)
}
the questions is where I should allocate the bands because in this situation the 1D vector return by function is ok because I see the values in the function scope. But when it is return to array[i] I got a unallocated 3dVector.
I need to recieve bands[size][i][j] with the data form b. In b the data is good then I ve gote bands null.
What I have do so far I make another allocation in prepare aray before the call to read_array where I allocate **band and then I have some results but I am not confident.
Sorry for the confusion! And every comment is good for me. Maybe what I have do is ok I do not know!.
I am not new to C I just do not work with pointers for a long time.
If it is a 2D pointer(**) you have to assign it with the address of 2D array and if it is 1D array you have to assign it with the address of 1D array.
For your read_array function
read_array(int**array...)
{
for(i=0;i<HEIGHT(the same as in allocation);i++)
`enter code here`array[i] = function();//function return an 1D array
}
Make sure that function() returns the address of the 1D array.

Resources