C - crash when running this application [duplicate] - c

This question already has answers here:
Modifying String Literal [duplicate]
(4 answers)
Closed 8 years ago.
Where is the problem? When running, the application crashes...
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void test(char* x) {
(*x)++;
}
int main() {
char* x = "xD";
test(x);
puts(x);
getch();
return 0;
}

You are Trying to modify a string literal that is stored in a read-only memory adress, because with char* x = "xD"; you declare a pointer to that kind of data. use this char x [] = "xD"; instead, that is NOT a pointer, is an array that you are allowed to modify because it is stored in the stack. or if you want to use a pointer you need to allocate memory for it.

it crashes in the line (*x)++; because x Points to a read only Memory due to the Definition char* x = "xD";.
Change it to char x[] = "xD";. so x is an Array and it´s values can be changed

Related

pointer array doesn't gets printed [duplicate]

This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 1 year ago.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i;
int *as[2];
for(i=0;i<2;i++)
{
*as[i]=i;
printf("%d\n",*as[i]);
}
}
This is my code. I am expecting to print values 1
2
But when I run the code the console prints nothing. What have I done wrong?
The * character in C language is not a simple decoration. It declares a pointer that has to point to something. Here you declared an array of two pointers, but never initialized the pointer themselves. And dereferencing an uninitialized pointer is explicitely Undefined Behaviour (the hell for C programmers). If you want to process integers, get rid of the pointers:
int as[2];
for(i=0;i<2;i++)
{
as[i]=i;
printf("%d\n",as[i]);
}
Or if you really want to process pointers, ensure that they are initialized to point to a valid object:
int *as[2];
int data[2]
for(i=0;i<2;i++)
{
as[i] = &(data[i]); // Ok, as[i] now points to a valid integer
*as[i]=i;
printf("%d\n",*as[i]);
}
BTW, the idiomatic way to get the address of an element of an array would be:
as[i] = data + i;

Memory allocation of a float array in C [duplicate]

This question already has answers here:
Determine size of dynamically allocated memory in C
(15 answers)
Closed 4 years ago.
I would like to create a float array of 9 elements. Thus I made use of malloc primitve function to do so :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void){
float* y;
printf("sizeof(float)=%ld\n",sizeof(float));
y = malloc (10*sizeof(float));
printf("sizeof(y)=%ld\n", sizeof(y));
float *x;
x = (float *) malloc(9*sizeof(float));
printf("sizeof(x)=%ld\n", sizeof(x));
}
The output is the following:
sizeof(float)=4
sizeof(y)=8
sizeof(x)=8
Therefore I am wondering why both sizeof(y) and sizeof(x) return 8. Shouldń't they return 10*sizeof(float)=10*4=40 and 9*sizeof(float)=9*4=36 respectively? Could someone please clarify to me why am I getting 8 instead for both arrays?
x and y are not declared as arrays. They are pointers to memory locations. Depending on the system, the pointers could be 8 bytes. That's what you're seeing.
If you were to declare an array int z[10], and did sizeof(z), you'd get 10*sizeof(int).

Why is the output of this programm garbage? [duplicate]

This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Closed 5 years ago.
I am wondering why the output is garbage? And how can I solve this problem?
#include<stdio.h>
char *Func(void);
int main()
{
char *string1;
string1 = Func();
puts(string1);
return 0;
}
char *Func(void)
{
char string2[10]="medo";
return string2;
}
Because string2 is only valid in the scope of Func.
An array decays into a pointer when assigning it to a pointer, passing it to a function or when used with return in a function returning a pointer.
An array decaying into a pointer means that it is converted to a pointer and the pointer points to the first element of the array.
With an array like this:
int arr[] = { 1, 2, 3 };
These are equivalent:
int *p1 = arr;
int *p2 = &arr[0];
The problem with Func is that it is returning a pointer that points to a memory location that is only valid while Func is running. Once Func returns, it is no longer valid and the program flow might replace these values immediately.
In your main function you pass this pointer to printf and because the location where string1 is pointing to isn't valid in main (it was only valid in Func), you get garbage. Actually that is undefined behaviour, and medo could have been printed as well.
How to fix it: declare an array in main, pass it to Func, let Func use the
passed array and return:
#include<stdio.h>
#include<string.h>
Func(char* string2);
int main()
{
char string1[20];
Func(string1);
puts(string1);
return 0;
}
void Func(char *string2)
{
strcpy(string2, "medo");
}
Because the lifetime of the string has already ended by the time you print it.
Local variables, such as string2 in Func, are often stored in the stack, which is a very dynamic structure that changes every time a function is called or returned. (The actual behavior of the program stack is a bit too complex to elaborate here, but keep in mind that stuff in the stack doesn't survive the function that puts it there.)
By the time the function returns, string2 is no longer necessary, and thus it might be overwritten with garbage data. Nevertheless, Func returns a pointer to string2 (remember that the name of an array is a pointer to its contents) — thus returning a pointer to the stack-allocated string that is being overwritten with garbage data. Printing the data referenced by this pointer (in main, via string1) just prints that garbage.
There are two ways of solving this issue. One is to let the caller handle memory allocation, and pass the allocated area to the callee, like so:
#include <stdio.h>
#include <string.h>
void Func(char *);
int main (void) {
char string[10];
Func(string);
puts(string);
return 0;
}
void Func (char * string) {
strcpy(string, "test");
}
...and another way is to let the callee handle the allocation, but remembering that the caller must free it, like so:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * Func(void);
int main (void) {
char * string = Func();
puts(string);
free(string);
return 0;
}
char * Func (void) {
char * string = malloc(10);
strcpy(string, "test");
return string;
}
Either way would solve your issue, and which one you choose is a matter of design.

Two different functions with same body with different parameters return values in same memory location [duplicate]

This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 5 years ago.
I cannot understand what happens here clearly.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int *f(int x){
int p;
p=x;
return &p;
}
int *g(int x){
int y;
y=x;
return &y;
}
int main(){
int *x,*y;
x=f(1000);
y=g(250);
*x = *x + 250;
printf("%d\n",*y);
return 0;
}
output:- 500
How come line "*x = *x + 250" change "*y" value? why the output is not 250?
int *f(int x){
int p;
p=x;
return &p;
}
In this function (and in g), you are returning the address of a local variable. When the caller uses this address, it is invalid, because it is referring to a variable which is destroyed (its like using a pointer to freed dynamic memory). This results in undefined behavior.

arrays and pointers arithmetic [duplicate]

This question already has answers here:
Is an array name a pointer?
(8 answers)
Closed 8 years ago.
Suppose an array int a[10].
Why we can't do a=a+1 ? but the same is valid with a pointer variable.
int *ptr = a;
ptr = a+1;
How are both scenarios seen practically?
Because array locations are constant.
You can't change the value of a, since that represents the starting address of the array. Moving it doesn't make any sense.
With int *ptr; your variable ptr is just a single pointer and can of course be set to point to anywhere you like.
There's no contradiction here. It's a little like with functions, the name of a function evaluates to its address (called "a function pointer") but you can't assign to that either.
Because Array name is constant pointer pointing to its first element.You cannot change the value of constant variables,i,e what the const keyword is used for.
int a[10]; //here a (array variable is Const i,e you Cannot a=a+1)
int* const p=&a[0]; //here Also Same,Now p is Const i,e you Cannot p=p+1.
But Here:
int* pp=&a[0];//Here pp=pp+1 will work, Because pp is not Const.
You can if the array is a function argument
#include <stdio.h>
int rval(int a[10]) {
a = a + 1;
return a[0];
}
int main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
printf ("%d\n", rval(a));
return 0;
}
Program output
1

Resources