Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
In my attempt to understand malloc and structs I have come across an error I do not understand
#include <stdio.h>
#include <stdlib.h>
typedef struct match
{
int round;
} match;
void foo(match *matches) {
for(int i = 0; i < 10; i++) {
matches = (match *) realloc(matches, i + 1);
matches[i].round = i + 1;
}
}
int main()
{
match *matches;
matches = (match *) malloc(0);
foo(matches);
free(matches);
return(0);
}
So in my attempt to fill this array of matches dynamicaly it fails
Your foo function is very flawed. First, the parameter passes a copy of the matches pointer, so when you realloc, that updates the foo matches pointer, but not the matches pointer in main. This may cause problems with the free in main. You need to change the parameter to foo to be a double pointer: void foo(match **matches). Then to realloc, *matches = realloc(...
Next, the second parameter to realloc is a size. But i + 1 isn't going to be big enough for a full copy of the match struct. You probably meant to do something like sizeof(struct match) * (i + 1).
I addition to above answer. Good Explanation...
Please check the error from realloc as well before using the memory,
Modified the program
void foo(match **matches) {
for(int i = 0; i < 10; i++) {
*matches = realloc(*matches, (i+1) * sizeof(match));
...
}
}
int main()
{
...
foo(&matches);
...
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to print maximum value of array elements. Programm compiles fine, but when I input array values I get this message *** stack smashing detected ***: terminated. What I did wrong?
#include <stdio.h>
int get_max(int ar[5]) {
int i;
for (i=0;i<5;i++) {
scanf("%d", &ar[i]);
}
int max = ar[0];
for (i=0;i<5;i++) {
if (max < ar[i])
max = ar[i];
}
}
return max;
}
int main() {
int a;
int k;
k = get_max(&a);
printf("%d",k);
return 0;
}
You're allocating space for one int, passing the address of that to the function, and trying to treat it as an array of five ints.
If you want to pass an array, declare an array.
But really, there seems to be no point to declaring a in main() at all, and passing it to the function. Just declare the array locally in the function.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm having some trouble with this code snippet.
size_t* defines = malloc(sizeof *defines);
if (!defines)
exit(1);
size_t def_cap = 1;
size_t def_size = 0;
...
for(condition) {
...
if (def_size == def_cap) {
void* tmp = realloc(defines, def_cap*=2);
if(!tmp)
exit(1);
defines = tmp;
}
defines[def_size++] = foo;
}
I'm getting a "malloc.c:2842: mremap_chunk: Assertion `((size + offset) & (_rtld_global_ro._dl_pagesize - 1)) == 0' failed." error when I run. Valgrind tells me there's an invalid write of size 8 in the realloc call. What's going on? condition and foo are part of a mess of file parsing that doesn't use or modify any of the variables above.
realloc takes a number of bytes just like malloc, so you need to multiply the number of entries by sizeof(size_t) as before:
def_cap *= 2;
void* tmp = realloc(defines, def_cap * sizeof *defines);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a some problems writing a code in which I want to modify a file extension stored in a string.For example string bla/bla/file.icc i want to be changed to bla/bla/file.cmr. This string makes part from a structure. I have 2 issues. One is that strcpy gives this message "expected expression before td_ActDOR and second one is in for and give's this message subscribed value is neither array nor pointer.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_ActDOR
{
char pDOR_file[86];
}td_ActDOR;
int main(void)
{
char path[80]="blabla/blabla/aici.icc";
td_ActDOR *Obiect;
Obiect = (td_ActDOR *)malloc(sizeof (td_ActDOR));
strcpy(td_ActDOR->pDOR_file, "blabla/blabla/file.icc");
int path_lenght=strlen(td_ActDOR->pDOR_file);
int i;
char bla[4] = "rmc\0";
printf("Stringul before: %s\n",path);
for (i = 0; i < 3; i++)
{
Obiect->pDOR_file[path_lenght-(i+1)] = bla[i];
}
printf("Stringul after: %s\n",path);
return 0;
}
In your code, td_ActDOR is not a variable, (it's a type), Obiect is.
Change
strcpy(td_ActDOR->pDOR_file, "blabla/blabla/file.icc");
to
strcpy(Obiect->pDOR_file, "blabla/blabla/file.icc");
Same goes for strlen(td_ActDOR->pDOR_file);, too.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
here is my problem, I malloc my array like that:
e->map = malloc(sizeof(int *) * e->map_y);
i = -1;
while (++i < e->map_x)
e->map[i] = malloc(sizeof(int) * e->map_x);
where e->map_y = 14 and e->map_x = 10
The problem is I can't access (I have a segfault) elements after e->map[10][0] (included)
I tough about I invert x and y but it doesn't seem to be the case here.
I can post my entire code if necessary, thx
hi, I added the entire project on github for more details: https://github.com/42-hbock/fdf
this part of the code is in src/default_reading.c, the malloc is in the function char *default_reading(char *file, t_env *e) and I have the segmentation fault while accessing in void create_int_map(char *cmap, t_env *e)
Should be:
e->map = malloc(sizeof(int *) * e->map_y);
i = -1;
while (++i < e->map_y)
e->map[i] = malloc(sizeof(int) * e->map_x);
The change is having the while look at e->map_y instead of e->map_x. The rest of the code is the same.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
With my current implementation of code, I'm getting a segmentation fault that I think is caused by trying to store several char* in another char*. However, I don't know a way around this being fairly new to C. I'm in a GLUE Unix environment. The code compiles but does not fully execute.
Here's my method that I think is causing the issue.
int totalLogins = 0, selectedLogins = 0,dateIn[3], timeIn[3], dateOut[3], timeOut[3];
int latest = 0,earliest=30,loggedIn = 0, firstTime[7][3],firstDate[7][3],i;
char* user[12], firstUser[7][12];
bool first = true;
void checkAndSetEarliest(int day)
{
first = true;
for(i = 0; i < 3 && first; i++)
{
first = (timeIn[i]<firstTime[day][i]);
}
if(first)
{
for(i = 0; i < 3; i++)
{
firstTime[day][i] = timeIn[i];
firstDate[day][i] = dateIn[i];
}
printf("user = %s\n",user);
firstUser[day] = user;
}
}
timeIn[],firstTime[][],firstDate[][], and dateIn[] are all ints
firstUser[] and user[] are char*
I'm trying to edit the contents of firstUser with the value of user.
First, the way you are using user[] in printf, it is suppose to be a char array, not an array of pointer to char (what you have declared).
Secondly, firstuser is a 2d arrray and you are using it as a 1d array.
You really need to show the definition of the variables this function uses. Fair chance that last line should be changed to:
strcpy( FirstUser[day], user );