Segmentation fault initialising an array - c

The following program produces a segmentation fault, and I'm not sure why. malloc succeeds, so it doesn't seem to be an initialisation error, but for some reason, it segfaults when I access the 253900th element. The array is only 4 * 1e6 bytes, or a about a Megabyte.
This does produce a lot of output
#include <stdlib.h>
#include <stdio.h>
int *long_array(size_t N) {
int *arr = (int *) malloc(N);
if (arr == NULL) { printf("could not malloc"); exit(1); }
for (size_t i = 0; i < N; i++) {
printf(".. %ld ", i);
arr[i] = 10;
}
printf("done with loop\n");
return arr;
}
int main(void) {
int *arr = long_array(1000000);
printf("%d", arr[5050]);
return 0;
}
I compile this with gcc -std=c99 and run the output to see the final few numbers printed before the segfault:
253899 .. 253900 .. 2
segmentation fault (core dumped) ./a.out
I don't understand why accessing a particular index is causing the segmentation fault. I can guess that I must be accessing a memory location outside of my processes address space, but this seems like a bug if I successfully allocated the memory from within my address space.

malloc(sizeof(int)*N) instead of malloc(N).
Otherwise you'd get an array of N bytes, not N integers.

use
int *arr = (int *) malloc(N * sizeof(int));

Your code is correct but the problem here is that your did not consider the size of an integer
while allocating memory. You just allocated :
int *arr=(int *)malloc(N);
here you just allocated size of N but you have to consider size of integer in your machine.
If you allocate just 10000000 size of memory and sizeof(int) in you machine is 4 then it can just access (10000000/4) memory .........
So you have to allocate memory like:
int *arr=(int *)malloc(N*sizeof(int))
or
int *arr=(int *)malloc(N*sizeof(*arr));

Related

Segmentation fault in a two dimensional array using scanf

I've been getting segmentation fault in this piece of code, in the part where I scanf for the values of the board. In the beginning, I scan two times for the dimensions.
#include <stdio.h>
#include <stdlib.h>
#include "maxsum.h"
int main() {
int x,y,n,m,**board;
scanf("%d %d",&n,&m);
board=malloc(n*sizeof(int));
if (board==NULL) {
printf("Unable to allocate memory \n");
return 1;
}
for (x=0;x<n;x++) {
*(board+x)=malloc(m*sizeof(int));
if (*(board+x)==NULL) {
printf("Unable to allocate memory \n");
return 1;
}
}
for (x=0;x<n;x++) {
for (y=0;y<m;y++) {
scanf("%d",&board[x][y]); // the error happens in this line ,and only when variable x is about to become n-1 when n>4 (very confused as to why)//
}
}
printf("%d \n",Solve(n,m,board)); //not relevant //
return 0;
}
You're not allocating the right amount of memory:
board=malloc(n*sizeof(int));
You're setting up board as an array of int *, but you're only allocating space for an array of int. Most likely an int is smaller than an int * on your system, so you're not allocating enough. You subsequently write past the end of allocated memory resulting in undefined behavior.
Change this allocation to:
board=malloc(n*sizeof(int *));
Or better yet:
board=malloc(n*sizeof(*board));

HEAP CORRUPTION DETECTED memory leak in C

I got and error which says
Debug assertation failed and heat corruption detected
like everything is working good in my program but I get that error. Where is the memory leak here? I have to free that memory in the main because my functions need to return pointers.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int *dynamic_reader(unsigned int n) {
/*making new array and checking if allocation succeeded*/
int *mem;
mem = malloc(n * sizeof(int));
if (!mem) {
printf("Memory allocation failed\n");
exit(-1);
}
/*letting the user to input the values for the array*/
int i = 0;
while (i < n) {
scanf("\n%d", &mem[i]);
i++;
}
/*printing the array just to make sure everything is good*/
for (int j = 0; j < n; j++) {
printf("%d ", mem[j]);
}
return mem;
}
int *insert_into_array(int *arr, unsigned int num, int newval) {
/*making new bigger array*/
int *newarr = realloc(arr, (num + 1) * sizeof(int));
/*adding the integer to this new array */
newarr[num] = newval;
printf("\n");
/*printing to make sure everything is correct*/
for (int j = 0; j < num + 1; j++) {
printf("%d ", newarr[j]);
}
return newarr;
}
int main(void) {
/*In dynamic_reader function I need to make an array which size is given as a parameter*/
/*In this case I choosed 3*/
int *arr = dynamic_reader(3);
int num = 3;
/*In insert_into_array function I need to add one integer to this array I made in dynamic_reader*/
/*The parameters are the array, the number of elements in the array already done and the integer I want to add*/
int *c = insert_into_array(arr, num, 9);
/*I free the memory here because I need to return the pointers of these arrays in the function so it cant be done there*/
free(arr);
free(c);
}
You are double freeing your memory. Check the documentation for realloc. Realloc will either 1) expand the passed buffer or 2) will allocate a new buffer, copy the data, and free the original buffer. When you do:
free(arr);
free(c);
You are double freeing a value that was either once already freed by realloc or already freed by the first free(arr)
Additionally, you should check if realloc fails (returns NULL) and if so, handle the case appropriately.
First you malloc an array, which you return to your main function as arr. Then in another function, you realloc where arr is an argument to the realloc, but some other pointer stores the results. Depending on what happened in realloc, you've either got arr and newarr pointing to the same location, or newarr pointing to a valid location and arr pointing to an invalid location that has been freed. Either way, freeing both of them at the end is a problem.
No need to free(arr), that is taken care of when you realloc() it. The pointer returned by realloc() will either point to memory which includes the original memory, or free the old memory after copying its content to a new, larger chunk of memory.

Segmentation fault in C. What is the reason?

I am getting segmentation fault in C and I have no idea what is the reason. As far as I know segmentation fault occurs when system run our of memory. I checked my loops and they seem to have clear termination conditions. Thus, I am confused what causes my code to crash. As I checked this fault should occurs in mygetline or readlines functions.
Is there any debugger that I can use to find our why program crashes?
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
#define MAXLINENUM 10
int readlines(char *lines[]);
int mygetline(char line[]); /* getline: read a line into s, returns length */
void writelines(char *lines[], int nlines);
int main()
{
int i = 0;
char *lines[MAXLINE];
i = readlines(lines);
writelines(lines, i);
return 0;
}
int mygetline(char line[]){
int i, c;
for(i = 0; i < MAXLINE-1 && (c = getchar()) != '\n' && c != EOF; i++){
line[i] = c;
}
line[i] = '\0';
return i;
}
int readlines(char *lines[]){
int i, c;
i = 0;
while((c = mygetline(lines++)) > 0 && i < MAXLINENUM){ //line[i]
i++;
}
lines[--i] = '\0';
return i;
}
void writelines(char *lineptr[], int nlines) {
int i;
for (i = 0; i < nlines; i++)
printf("%s\n", lineptr[i]);
}
The reason for the segmentation fault is that you don't allocate any storage for the strings. You declare an array of char* here:
char *lines[MAXLINE];
But nowhere do you ever allocate memory to hold the actual strings.
To continue using an array of char*, allocated that way, you'd need to use malloc to allocate memory for the actual strings.
lines[i] = malloc(...);
Obviously you'll need to:
Decide how much memory to allocate for each string.
Call free to dispose of the memory when you are done with it.
Add some code to check that you don't write beyond the end of any of your buffers.
You created an array char *lines[MAXLINE] which is an array of pointers. However, you've not initialized those pointers, so they're pointing to garbage. You then pass the garbage pointer into mygetline which writes to the garbage pointer, and this is the problem.
Not only do you need to allocate memory for the line to be stores in, but you also need to get mygetline how many characters it can read. If you allocate space for 10 characters, but the user enters 20 you're going to have a similar problem.
The reason that you get a segfault is that your code reads the data into lines without allocating any memory for the characters that you read.
Your main allocates memory for the pointers to strings, but it does not initialize these pointers. You need to add code to set these pointers to valid blocks of memory, for example, by allocating some max number of characters with malloc:
while(i < MAXLINENUM) {
lines[i] = malloc(MAXLINE);
if (!mygetline(lines[i++])) {
break;
}
}
Also note that you confused MAXLINE with MAXLINENUM in the declaration of lines.
The reason for the segmentation fault is the memory allocation.
You can not allocate memory like char *lines[MAXLINE];.
Try the following code
If you are Using 1D array
char *lines;
lines=(char *)malloc(MAXLINE*sizeof(char));
If you are using 2D array go with this code
char **lines;
lines=(char **)malloc(MAXLINENUM*sizeof(char *));
for(i=0;i<MAXLINENUM;i++)
lines[i]=(char *)malloc(MAXLINE*sizeof(char));

C programming to increase the size of a string

So I wrote this code but it gives me the same answer everytime. I am increasing the memory allocated to the pointer in steps of 4 and then print the value.
#include <stdio.h>
int main(void) {
int n=0;
char *name = "hello";
scanf("%d",&n);
for(int i =0; i<n;i++){
name += sizeof(int);
printf("%d \n", (sizeof(&name)));
}
return 0;
}
can someone help me? I don't know whats wrong here. I don't need a different code, I just want to understand what's wrong with this.
Try the following, error checking was left out for clarity:
#include <stdio.h>
int main(void)
{
int n=0;
char *name = null;
scanf("%d",&n);
for(int i=0; i<n;i++)
{
char *buffer = null;
//allocate/reallocate the buffer. increases by 4 bytes every iteration
buffer = (char*) realloc(name, (i+1)*4);
name = buffer;
printf("%d \n", (sizeof(&name)));
}
//release the memory used by the buffer
free(name);
return 0;
}
Here are some explanations of what is happening.
#include <stdio.h>
int main(void) {
int n=0;
// this does not actually allocate any memory. It sets the POINTER name to point (like an arrow) to a read-only block that contains "hello"
char *name = "hello";
// string literals generally come in fixed read-only memory
scanf("%d",&n);
for(int i =0; i<n;i++){
// this causes the pointer memory address to be incremented by sizeof(int) (typically 4)
// after the first increment if it will point to a string "o" (incremented by 4 characters)
// after the second increment it will point to some undefined memory behind "hello" in your virtual address space and will have undefined behaviour when accessed
name += sizeof(int);
// sizeof(&name) will give you the size of a char **. Pointer to a character pointer.
// Wich is the same size as all pointers.
// = sizeof(void *) = 8 for 64-bit systems, 4 for 32-bit systems
printf("%d \n", (sizeof(&name)));
}
return 0;
}
This is the way to do it:
#include <stdio.h>
int main(void) {
int n=0;
// allocate 10 bytes of memory and assign that memory address to name
char *name = malloc(10);
// the size of that memory needs to be kept in a separate variable
size_t name_length = 10;
// copy the desired contents into that memory
memcpy(name, "hello", sizeof("hello"));
scanf("%d",&n);
for(int i =0; i<n;i++){
// reallocate the memory into something with sizeof(int) more bytes
void * tmp = realloc(name, name_length += sizeof(int));
// this can fail
if (tmp) {
name = tmp;
} else {
perror("realloc");
exit(-1);
}
printf("%d \n", name_length);
}
return 0;
}
You have not allocated any memory for the pointer at all in the code you provide. You will have to deal with dynamic memory if you want to change the size of the allocated chunk. You will have to initially use malloc and then use realloc to allocate more memory on each step.
Let's step through your code one by one:
char *name = "hello";
this create an array of chars 'h','e','l','l','o',0 and assignes the memory address of the first character to name
for(int i =0; i<n;i++){
name += sizeof(int);
printf("%d \n", (sizeof(&name)));
}
here you add to the name pointer the size of int, which increments this pointer by 4 each pass.
Since this is a char pointer, the pointer is incremented by 4 bytes - since sizeof(int) == 4
You cannot increase the size of your hello char array, since it is not a dynamic array.
If you wish to be able to resize the string, you should malloc and copy the chars to the bigger array.

Segmentation Fault when attempting to print value in C

The following C code returns a "segmentation fault" error. I do not understand why it does not return the value 20. What is my error?
#include <stdio.h>
int main()
{
int* n;
*n = 20;
printf("%i\n",*n);
return 0;
}
You haven't allocated memory to n, so
*n = 20;
attempts to write unspecified memory.
Try
#include <stdlib.h>
int *n = malloc(sizeof *n);
/* use n */
free(n);
You haven't allocated space for your int, you've only declared a pointer to an int.
The pointer is uninitialized, and so writing to that unknown space in memory is undefined behavior and causes problems. This typically causes a segfault.
You can allocate a slot for an integer using malloc:
n = malloc(sizeof(int));
And use a corresponding call to free to free up the memory later:
free(n);
But allocating a single slot for an integer is pretty unusual, typically you would allocate the int on the stack:
int n;
n = 20;
You are trying to write 20 in garbage value. You must allocate space for it by using one of *alloc() functions or creating an int on stack and getting the andress of it(as Richard J. Ross III mentioned on comments).
dynamic allocation:
int n*;
n = malloc(sizeof(int)); /* allocate space for an int */
if(n != NULL) {
/* do something.. */
free(n); /* free 'n' */
} else {
/*No space available. */
}
or on the stack:
int int_on_stack;
int *n = &int_on_stack;
*n = 20;
printf("%i\n", *n); // 20

Resources