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);
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 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);
...
}
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 7 years ago.
Improve this question
I have a file which contains a set of numbers.
I'm trying to read those numbers into an array. I'm allocating memory for that array using a pointer and reading from the file into the location.
For some reason, the program does not read beyond 5 values from the file.
int main(int argc, char* argv[] )
{
int i=0, count=0;
//unsigned long int num[1000];
unsigned long int *ptr;
ptr = (unsigned long int*) malloc (sizeof(unsigned long int));
char file1[30], file2[30];
int bin[1000][32];
int ch;
argv++;
strcpy(file1,*argv);
FILE *fp;
fp=fopen(file1, "r");
while((fscanf(fp,"%ld",ptr))==1)
{
ptr++;
count++;
}
ptr=ptr-count;
for(i=0; i<count;i++,ptr++)
printf("%ld\n",*ptr);
return 0;
}
The input file contains the following:
1206215586
3241580200
3270055958
2720116784
3423335924
1851806274
204254658
2047265792
19088743
The output is just this:
1206215586
3241580200
3270055958
2720116784
3423335924
Thanks in advance.
You need to allocate enough space to store your integers in. To do this , use the realloc function on the original pointer.
The fact that you write ptr++ makes it awkward to call realloc on the original pointer and save the result. So it would be a better idea to not use ptr++. Instead you can use ptr[count] and leave ptr always pointing to the start of the allocation.
For example the main loop could be:
while((fscanf(fp,"%lu",&ptr[count]))==1)
{
count++;
void *new = realloc(ptr, (count+1) * sizeof(*ptr));
if ( !new )
break; // maybe print error message, etc.
ptr = new;
}
// don't do this any more
// ptr=ptr-count;
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 7 years ago.
Improve this question
Im trying to write a program in c that allocates memory for a 'char' array with dimension of n,m. Below is what i tried but every time i run it, no matter what dimensions i give it returns a value of -2 without even printing "Error in memory allocation.".
What do you guys think?
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i,j,n,m;
char **p;
scanf("%d %d",&n,&m); //get array dimensions
p=malloc(n*sizeof(char *));
if (p==NULL){
printf("Error in memory allocation.\n");
return -1;
}
for (i=0;i<n;i++){
p[i]=malloc(m*sizeof(char));
if (p[i]==NULL)
printf("Error in memory allocation.\n");
return -2;
}
}
Thanks!
if (p[i]==NULL)
printf("Error in memory allocation.\n");
return -2;
must be
if (p[i]==NULL) {
printf("Error in memory allocation.\n");
return -2;
}
Some coding guides require to always put {} with if statements (even with a single statement) to avoid this kind of issue.
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.