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.
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 3 years ago.
Improve this question
I am declaring a 2d array in a headers file like this : int **arr;
Then I'm allocating memory and I initialize it with zeros.
However I'm getting segmentation fault.
Here is my code :
arr = (int **)malloc(d * sizeof(int *));
for (int u=0; u<d; u++)
arr[u] = (int *)malloc(q * sizeof(int));
for(int i=0; i<d+1; i++)
{
for(int j=0; j<q+1; j++)
{
arr[i][j]=0;
}
}
d+1 and q+1 both outside the boundary.
Use d and q
If you want to initialize with zero use calloc() which is simple to use and reduces redundant operations
arr = (int **)malloc(d * sizeof(int *));
for (int u=0; u<d; u++)
scoreBoard[u] = (int *)calloc(q , sizeof(int));
This code will create 2d int array and initialize with zero
You are getting a segmentation fault because you are overstepping the array's bounds.
for (int i = 0; i < d + 1; i++)
Should become:
for (int i = 0; i < d; i++)
And similarly for the other one. Don't forget array indices go from 0 to 1 less than the size (in elements) of the array.
Also:
Was the memory for scoreboard allocated? Currently, you create an array called arr rather than for the scoreboard you are initializing, so scoreboard[u] may also be out of bounds regardless of the value of u.
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 4 years ago.
Improve this question
I'm trying to have this string print out individual names. Instead of printing one name at a time, it prints all the names out. Can someone show me what I'm doing wrong? Thanks in advance.
#include <stdio.h>
#define NUM_OF_NAMES 8
#define NAME_SIZE 3
void printNames(char names[][NAME_SIZE], int size);
int main()
{
char nameList[NUM_OF_NAMES][NAME_SIZE] = {"Bob",
"Sue",
"Jak",
"Rod",
"Jon",
"Ash",
"Deb",
"Kay"};
printf("\n\n\nLIST\n");
printNames(nameList, NUM_OF_NAMES);
}
void printNames(char names[][NAME_SIZE], int size)
{
for(int index = 0; index < size; index++)
{
printf("%4s\n", names[index]);
}
return;
}
The reason the program prints all names together is that the individual three-character names are not null-terminated. printf tries printing the first name, does not find null terminator, goes into the next name, then the next one, and so on. In the end, this is undefined behavior, because the eventual null termination of the whole arrays is not there.
This problem happens because NAME_SIZE is too small - it does not accommodate null terminator.
Fix this by changing NAME_SIZE:
#define NAME_SIZE 4
Enabling warnings and treating them as errors would help you avoid this problem in the future.
Firstly, in char nameList[NUM_OF_NAMES][NAME_SIZE] you have defined NAME_SIZE as 3 and you are storing exactly 3 char into that, so there is no memory space kept for '\0' null terminator, as char buffer should be null terminated. To avoid this problem increase the NAME_SIZE. for e.g
#define NAME_SIZE 4
Secondly, from main() you are passing 2D array nameList you have to catch with pointer to an array like char (*names)[NAME_SIZE] not with 2D array as you did.
Here is the modified Code
#define NUM_OF_NAMES 8
#define NAME_SIZE 4
void printNames(char (*names)[NAME_SIZE], int size) { /*catch with pointer to an array of NAME_SIZE char */
for(int index = 0; index < size; index++) {
printf("%4s\n", names[index]);
}
}
int main(void ){
char nameList[NUM_OF_NAMES][NAME_SIZE] = {"Bob",
"Sue",
"Jak",
"Rod",
"Jon",
"Ash",
"Deb",
"Kay"};
printf("\n\n\nLIST\n");
printNames(nameList, NUM_OF_NAMES);
return 0;
}
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 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
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.