Exception thrown at 0x00007FFCCCB30369 (ucrtbased.dll) in Project3.exe: 0xC0000005: Access violation writing location 0x000000894B700000 [duplicate] - c

This question already has an answer here:
C++ scanf error (Access violation while saving)
(1 answer)
Closed yesterday.
I'm studying C language, I ran these codes in Microsoft Visual Studio, but I got an access violation exception, Could you please help me find out the reason?
#include <stdio.h>
int main() {
char name[10];
printf("Enter your name\n");
scanf_s("%s", name);
printf("Welcome %s\n", name);
return 0;
}
I've looked for numerous solutions to this, however some are no effective. I tried removing the & from the name, but that didn't help.

You need to provide the length of name to scanf_s too:
scanf_s("%s", name, (unsigned)sizeof name);
Without the lenght, the program will have undefined behavior and may therefore crash

Related

Why am I able to copy more bytes than defined in the char array? [duplicate]

This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Closed 3 years ago.
I have the following code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char buffer[2];
strcpy(buffer, "12345678910");
printf("%s\n", buffer);
return 0;
}
Since, I have already defined the char array with size 2, I shouldn't be able to put in more than 2 char plus null terminating character. Yet, it is able to take more than that without any buffer overflows or segmentation faults. Even if I copy the string strcpy(buffer, "123456789101298362936129736129369182");, it works fine. The error is generated when I push strcpy(buffer, "1234567891012983629361297361293691823691823869182632918263918");.
More of a theroetical question than a practical, but I hope it helps the new and the experienced programmers alike since it talks about the fundamentals, and helps improving coding ethics. Thanks in advance.
The simple answer is that C does not protect you from yourself. It's YOUR responsibility to check boundaries. The program will happily read and write wherever you instruct it to. However, the operating system may say something if you do this, which is usually a "segmentation fault". A worse scenario is that it may overwrite other variables.
This is a source of many bugs in C, so be careful. Whenever you're writing outside outside a buffer, you're invoking undefined behavior and these can manifest themselves in various ways, including the program working as it should, overwriting variables and segmentation faults.
I shouldn't be able to put in more than 2 char plus null terminating character
This is a common bug. It's NOT "plus null terminating character". It's INCLUDING null terminating character.

Exception in c scanf_s

I am trying to write a simple code to input values of an int and a char. Visual studio is throwing an exception
#include<stdio.h>
int main() {
int i;
char c;
printf(" Enter the values");
scanf_s("%c %d",&c,&i);
return 0;
}
As i run the program and input values, visual studio is throwing an exception saying : Exception thrown at 0x599C939E (ucrtbased.dll) in main.exe: 0xC0000005: Access violation writing location 0x0032133E
You need to specify the sizeof memory you want to allocate for your char.
scanf_s("%c %d",&c,1,&i);
Won't return any errors.
Since the scanf() function is kind of "unsafe", VS forces you to use the scanf_s function, which is a safer option.
This way, the user won't be able to trick the input.
For format specifiers as c and s there is required to specify the size of the buffer after the corresponding pointer in the list of arguments.
In your case the function call will look like
scanf_s("%c %d",&c, 1, &i);
For format specifier s the size of the buffer also have to take into account the terminating zero.

Different output when printing a null char pointer with and without a newline appended [duplicate]

This question already has answers here:
What is the behavior of printing NULL with printf's %s specifier?
(4 answers)
Closed 5 years ago.
I tried the following code on Linux
#include<stdio.h>
int main()
{
char *p=NULL;
printf("%s",p);
return 0;
}
#include<stdio.h>
int main()
{
char *p=NULL;
printf("%s\n",p);
return 0;
}
The first one outputs: (null)
While the second one causes a segmentation fault.
Why does \n make such a difference?
Both of your examples are undefined behavior per standard. Calling printf with %s and passing a NULL pointer is UB.
Therefore it makes no sense to discuss the outcome. On one system you might get one result and on another system you get another result.
Also see https://stackoverflow.com/a/11589479/4386427

Segmentation fault (core dumped) [duplicate]

This question already has answers here:
Why does scanf require &?
(7 answers)
Closed 5 years ago.
Below is a block of code from my program. I'm getting error of Segmentation fault (core dumped) after entering name and age in first loop.
#include<stdio.h>
#include <string.h>
struct Cricketer
{
char name[25];
int age;
float avg_run;
};
int main(){
struct Cricketer c[3];
int i,j;
for (i=0 ; i<3;i++){
printf("Enter name: \n");
scanf("%s",c[i].name);
printf("Enter age: \n");
scanf("%d",c[i].age);
printf("Enter average run: \n");
scanf("%f",c[i].avg_run);
}
return 0;
}
And I couldn't find what is causing this program.
Your error is here:
scanf("%d",c[i].age);
Change it for:
scanf("%d",&(c[i].age));
When using %d you have to pass the address of the int variable. And the same for floats:
scanf("%f",&(c[i].avg_run));
When using scanf, the second argument has to be the address of a variable. With your variable name there is no problem because it already refers to the address of the buffer in which you want to store the string.
When using scanf, the second argument should be the address of a variable. In your program c[i].age and c[i].avg_run are the variables themselves and not the addresses. Use the & operator to get the address of a variable. For Example, &(c[i].age) or just &c[i].age.
What you are passing to scanf as mentioned above are some numbers which might or might not be valid memory addresses. Thus invoking undefined behavior.
c[i].name happens to be fine because referring to an array (name in this case) by just the name evaluates to the base address of the array.
It is a good idea to have compiler warnings enabled. More important is one reads and understands the warnings. Read your compiler manual for more info.

modifying a char * under linux crashes but not under cygwin. why? [duplicate]

This question already has answers here:
Why does this small C program crash?
(4 answers)
Closed 9 years ago.
This code compiles with no errors under cygwin and under linux.
But when i run it, it runs with no errors in cygwin but it core-dumps under linux.
can someone shed some light about the memory management of these systems that would explain why the different behaviors?
#include <stdio.h>
void foo(char *p){
p[0]='A';
}
void main(){
char *string ="Hello world!";
foo(string);
printf("%s\n", string);
}
Thanks for the answers and makes sense that behavior is not defined, however i was interested in the differences of the underlying systems that lead to these 2 distinct undefined behaviors. I imagine its related to how they manage memory but looking for someone who is familiar with the internals who can explain why one ends up crashing while the other one does not.
In C++ string literals must not be modified. And with that pointer that's what you're trying to do.
If you want to modify it, you'll have to declare it like this:
char string[] = "Hello world!";
Modifying char* causes undefined behaviour , just because it does not crash , does not mean it won't. That is what undefined means , the behavior is not predictable , in your case , the program not crashing is also not predictable.
modification of a constant string is undefined behavior.
Also please define main() as
int main(void)
{
//your program
return 0;
}

Resources