C Programming EXC_BAD_ACCESS on very basic C program - c

I am quite new to C programming. I need to write a programm that works with dynamic Arrays. (Takes in Values and doubles the size of the Array, when it is full). My programm is finished and compilation works but I keep on getting this Error in the commented line: Thread 1: EXC_BAD_ACCESS (code=2, address:(some long adress).
I have read on this and it seems that I may be pointing to null. But a null test didn't do the job. I'Ve had this problem in other programms before and I seem to be missing a basic point.
Can somebody help me with this please!
This is my code:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int *value;
int size;
int MAX;
} DynArray;
void dyn_array_add (DynArray* array){
int wert;
int *temp;
printf("Geben Sie einen Wert ein:\n");
scanf("%i", &wert);
if (array->MAX==array->size) {
for (int i= 0; i<array->MAX; i++) {
temp[i] = array->value[i]; // error occurs HERE
}
free(array->value);
array->MAX = array->MAX*2;
array->value=malloc(sizeof(int)* array->MAX);
for (int i= 0; i<array->MAX; i++) {
array->value[i] = temp[i];
}
}
array->value[array->size]= wert;
array->size++;
for (int i = 0; i < array->MAX; i++) {
printf("Value[%i]: %i \n", i, array->value[i]);
}
}
int main(int argc, const char * argv[]) {
DynArray* array;
array = (DynArray*)malloc(sizeof(DynArray));
array->MAX=5;
array->size=0;
array->value=malloc(sizeof(int)* array->MAX);
while (1) {
dyn_array_add(array);
}
return 0;
}

temp[i] = array->value[i]; // error occurs HERE
Obviously, because you have not allocated memory for temp.

Related

Free dynamically allocated memory in C

because I am new in C, I am not sure how to ask it, but here is my Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 500
int main(int argc, char *argv[]) {
for (int j=0; j<ARRAY_SIZE; ++j) {
printf("Memory Size: %d\n", j);
int bytes = (1024*1024);
char *data;
data = (char *) malloc(bytes);
for(int i=0;i<bytes;i++){
data[i] = (char) rand();
}
}
//Free all Char*data that I have declared inside the for loop here
return 0;
}
So I need to free my data variables that I have allocated inside the for loop. How is it possible? I am testing some portion of my memory blocks. So I am running it because I wanna see how far it goes. So the above code gets me to that point. Now I am trying to run a loop below threshold point so that I can assure, the memory that I am working with is good and can sustain. To do so, I need to clear the memory that I have created inside the loop.
Thanks in advance
I think you'll want an array of pointers and then free those pointers after the loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 500
int main(int argc, char *argv[]) {
char * data[ARRAY_SIZE] = {0};
for (int j=0; j<ARRAY_SIZE; ++j) {
printf("Memory Size: %d\n", j);
int bytes = (1024*1024);
data[j] = (char *) malloc(bytes);
for(int i=0;i<bytes;i++){
data[j][i] = (char) rand();
}
}
for (int j=0; j<ARRAY_SIZE; ++j) {
free(data[j]);
}
return 0;
}
Since you assigned the return value of malloc to data which is defined inside of the loop, that memory is lost at the end of each iteration of the loop.
You need to add a call to free at the bottom of the loop.
for (int j=0; j<ARRAY_SIZE; ++j) {
printf("Memory Size: %d\n", j);
int bytes = (1024*1024);
char *data;
data = (char *) malloc(bytes);
for(int i=0;i<bytes;i++){
data[i] = (char) rand();
}
free(data);
}

Segmentation fault as soon as i run my code. Seems like problem with arrays

I wrote code which prompts the user for a number, then tell which digits are of that number are repeated.
While there is no problem with compilation, it shows segmentation fault(core dumped) when i run it.
Code :
#include <stdio.h>
int main(void)
{
int n=0;
int digit[n];
int t;
for (int num = 0;num<10;num++)
{
digit[num] = 0;
}
scanf("%d",&t);
int y=t;
int c = 0;
do
{
c++;
y = y/10;
}while(y>0);
int ko;
for (int no=0;no<c;no++)
{
ko = t%10;
digit[ko]++;
t=t/10;
}
int count = 0;
for (int co = 0;co<10;co++)
{
if (count>0)
printf(" ");
if (digit[co]>1)
{
printf("%d",co);
count ++;
}
}
}
The problem lies here:
int n=0;
int digit[n];
This makes digit an array of 0 ints, which is not what you need.
Looking at
for (int num = 0;num<10;num++)
{
digit[num] = 0;
}
This makes me think that you need to iterate over 10 array elements.
So make it,
int digit[10];
Or even better define a macro and use it elsewhere.
#define LIMIT_ARRAY_ELEMENTS (10)
and then
int digit[LIMIT_ARRAY_ELEMENTS];
and
for (int num = 0; num < LIMIT_ARRAY_ELEMENTS; num++)
The segmentation fault you see is because you have not allocated enough memory for what you are trying to do.

Why is the code printing me only the value of the last pointer in the array of pointers to a string?

Here is my code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(){
char* a[5] = { "tomer","tomer","tomer","tomer","tomer" };
char t[] = "ppppr";
char* n = &t;
for (int i= 0;i < 3;i++) {
scanf(" %s",n);
a[i] = n;
}
for ( int j= 0;j < 3;j++) {
printf("%s\n", a[j]);
}
system("pause");
return 0;
}
What is the reason this outputs three times the last vlaue in the array?
Because you are allocating the memory for new string only once which is
char t[] = "ppppr";
BTW, there are other problems with in your code.
you are returning 0 from main but its return type is void instead of
int
you should do char* n = t; not &t

strcpy() generates segmentation fault on variable sized array of strings

I'm learning "C" language, I have created a program as below that would copy command-line arguments to another variable sized array (of strings) and print that array starting from index 2.
#include <stdio.h>
#include <string.h>
void print_array(const char* arr[]) {
for (int i = 0; arr[i]; i++) {
printf("%s\n", arr[i]);
}
}
void copy_array(char* dest[], const char* src[]) {
for (int i = 0; src[i]; i++) {
strcpy(dest[i], src[i]);
}
}
int main(int argc, const char* argv[]) {
if (argc < 2)
return 1;
char args[argc][256];
copy_array(args, argv);
print_array(&args[2]);
return 0;
}
The problem is, when I call it via './a.out one two three four five', it throws a segmentation fault. Using gdc, I could see that strcpy() did this. I'm just learning it, can't really find the cause. Can someone point me to what I'm doing wrong?
Your problem is that an array of arrays of char is not the same as an array of pointers to char. That may seem pedantic, but underlying structure is different.
In fact, your compiler should be issuing a warning (if it's not, you should increase your warning levels).
More specifically, if a is an array of array of chars, then a[i] is an address relative to the start of your array that can only be calculated because the compiler knows how large each sub array is (e.g. 256 in your case).
Whereas, if a is an array of pointers to char, no calculation is required, a[i] contains the value of the pointer itself.
By passing an array of arrays where an array of pointers is expected, you are causing the compiler to calculate the address incorrectly (i.e. undefined behavior) and hence your segfault.
If you want to be consistent and use an array of pointers everywhere, your code might look more like:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_array(char* arr[]) {
for (int i = 0; arr[i]; i++) {
printf("%s\n", arr[i]);
}
}
void copy_array(char* dest[], char* src[]) {
int i;
for (i = 0; src[i]; i++) {
dest[i] = malloc(strlen(src[i]) + 1);
strcpy(dest[i], src[i]);
}
dest[i] = NULL;
}
int main(int argc, char* argv[]) {
if (argc < 2)
return 1;
char *args[argc + 1];
copy_array(args, argv);
print_array(&args[2]);
return 0;
}
It seems the copy_array loop execute after the actual length of the array
void copy_array(char* dest[], const char* src[]) {
for (int i = 0; src[i]; i++) {
strcpy(dest[i], src[i]);
}
}
should be changed to
void copy_array(char* dest[], const char* src[], int count) {
for (int i = 0; i < count; i++) {
strcpy(dest[i], src[i]);
}
}
Also print array should be changed to:
void print_array(const char* arr[], int count) {
for (int i = 0; i < count; i++) {
printf("%s\n", arr[i]);
}
}

RunTime Check Failure #2 - Stack around the variable "tab" was corrupted

I'm training in C language and I'm doing an program that create a simple array and sort him.
Here's the code :
Main.c :
#include <stdlib.h>
#include <stdio.h>
int main(int argc, int *argv[])
{
int tab[5]={2,5,3,9,4}, i=0, longueur=0;
/*for(i=0; i<5; i++)
{
tab[i] = i;
}*/
longueur = sizeof(tab)/sizeof(tab[0]);
trierTab(tab,longueur);
afficherTab(tab,longueur);
return 0;
Tableaux.c :
#include "tableaux.h"
//Sort the array
void trierTab(int tab[],int longueur)
{
int i=0, j=0,max=0,indiceMax=0,temp=0;
for(i=longueur-1; i>=1;i--)
{
for(j=0;j<=i;j++)
{
if(tab[j]>max)
{
max = tab[j];
indiceMax = j;
}
}
temp=tab[j];
tab[j]=max;
tab[indiceMax]=temp;
}
}
//Print the array
void afficherTab(int tab[], int longueur)
{
int i=0;
for(i=0;i<longueur;i++)
{
printf("| %d |",tab[i]);
}
}
I guess that there is an out of bound error, but I can't figure out where she is :s
Can someone help me please ? ^^
When you exit from this for loop the first time:
for(j=0;j<=i;j++)
{
if(tab[j]>max)
{
max = tab[j];
indiceMax = j;
}
}
The value of j is 5.
Then you assign:
temp=tab[j];
So you are reading out of the bounds of tab (temp=tab[5];)

Resources