Stack around variable "struc" is corrupted - c

So I'm facing a problem with Visual Studio 2013
This code shows no compilation errors but a debug error
Stack around the variable 'Joueur' was corrupted.
This is the code:
== Source.cpp
#include <stdlib.h>
#include <stdio.h>
#include "Header.h"
int main()
{
Personne Joueur[2];
printf("***** Bienvenue ***** \n \n \n");
int k ;
for (k = 0; k < 3; k++)
{
printf("Bonjour Joueur %d :\n", k+1);
printf("Nom: ");
fgets(Joueur[k].Nom,50,stdin);
printf("Votre nom est: %s", &Joueur[k].Nom);
printf("\n \n");
}
return 0;
}
== Header.h
typedef struct Personne Personne;
struct Personne
{
char Nom[1500];
char Prenom[1500];
int Age;
int Tel;
double Moy;
};

Change Personne Joueur[2]; -> Personne Joueur[3];
You do not have enough memory to hold more than 2 structures.

You are trying to more values into Joueur[2] than memory. So, there is not enough memory to hold the more then two values.
You are overrunning the allocated memory and it invokes undefined behaviour. So, Define a array bigger enough to hold the values. SO, change your array value according to sas answer.

Related

Absolutely can't see what causes a segmentation fault even after 2 whole days

The problem is with the printf in the main. I am not a programmer and this is a bit self taught so don't butcher me. The function creation_noe creates a linked list of noes. If we want 3 of them then the link will start with the one numbered as 3, but the printf in the main gives me 1 3 3. I am lost.
Thank you for your help.
PS: the program is a lot longer this is only the needed part. in case you have question I am here to answer them. I just don't want you to waste your time on the rest which is irrelevant. thanks
P.P.S: The terms are in French
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct matrice
{
char nom[20];
int n,m;
double **tab;
struct matrice *next;
};
struct element
{
int num;
int n1, n2;
double k;
struct element *next;
};
struct noeud
{
int num;
double u;
double f;
struct noeud *next;
};
struct maillage
{
int nb_noe, nb_elt;
struct noeud *lst_noe;
struct element *lst_elt;
struct matrice *K, *U, *F;
};
typedef struct element* elements;
typedef struct noeud* noeuds;
typedef struct maillage* maillages;
/*==============================================*/
/* Recherche */
/*==============================================*/
noeuds recherche_noe(maillages mail,int num){
int i;
maillages temp=mail;
printf("%d %d ",num,mail->lst_noe->num);
while((temp->lst_noe!=NULL) && (temp->lst_noe->num!=num)){
temp->lst_noe=temp->lst_noe->next;
}
if(temp->lst_noe->num==num){
return temp->lst_noe;
}
return NULL;
}
/*==============================================*/
/* creation */
/*==============================================*/
void creation_noeud(maillages mail){
int i;
noeuds new = (noeuds)malloc(sizeof(struct noeud));
mail->lst_noe=NULL;
for (i=0;i<mail->nb_noe;i++){
new->num = i+1;
printf("Deplacement du noeud %d: ",new->num);
scanf("%lf", &new->u);
new->next=mail->lst_noe;
mail->lst_noe=new;
}
}
int main(){
int i;
maillages mail=malloc(sizeof(struct maillage));
printf("Donner le nombre de noeuds voulu: ");
scanf("%d",&mail->nb_noe);
printf("Donner le nombre d'elements voulu: ");
scanf("%d",&mail->nb_elt);
creation_noeud(mail);
//creation_element(mail);
printf("aaa %d %d %d aaa \n",1,mail->lst_noe->num,mail->lst_noe->next->num,mail->lst_noe->next->next->num);
}
mail->lst_noe=NULL;
...
new->next=mail->lst_noe;
mail->lst_noe=new;
and then
mail->lst_noe->next->next->num
This is bound to give segfault as mail->lst_noe->next is nothing but mail->lst_noe which itself is NULL. Dereferencing a null pointer gives you SEGFAULT.
I strongly recommend using a tool to identify the problem in the future.
Valgrind is good for catching memory errors. You compile your program with debug flags (the -g option) and then run
valgrind ./<myprogram>
You will need to have installed valgrind.
Also if you use the clang compiler you can use the -fsanitize=memory compiler option.
Both of these techniques will tell you the line of code that causes the memory error.

Error while printing the given inputs

This code:
#include<stdio.h>
struct my_structure {
char name[20];
int number;
int rank;
};
int main()
{
int n,i;
scanf("%d",&n);
struct my_structure variable[n];
struct my_structure *ptr;
ptr = &variable;
for(i=0; i<n; i++)
{
scanf("%s",ptr->name);
scanf("%d",&ptr->number);
scanf("%d",&ptr->rank);
}
for(i=0; i<n; i++)
{
printf("NAME: %s\n", ptr->name);
printf("NUMBER: %d\n", ptr->number);
printf("RANK: %d", ptr->rank);
}
return 0;
}
It is not printing correctly. I want to print the 1st input first and then the second. But I'm getting the output like printing only the second input two times.
Two changes you need to do:-
ptr = variable ;
Then you can increase ptr in loop.(The use of scanf is not robust - how to do it better is shown in the end of the answer).
for(i=0;i<n;i++)
{
scanf("%s",ptr->name);
scanf("%d",&ptr->number);
...
ptr++;
}
After that again do the assignment ptr = variable;.
You can also do this (Illustration on the users code)
..
scanf("%s",ptr[i].name);
scanf("%d",&ptr[i].number);
...
printf("%s",ptr[i].name);
printf("%d",ptr[i].number);
Earlier you have type mismatch in assignment. You were assigning a struct my_structure (*)[n] to struct my_structure*. Check the return value of scanf and also check the value n (n>1).
You can do it on your way also:(Illustration on the users code)
struct my_structure (*ptr)[n] = &variable;
for(i=0;i<n;i++)
{
scanf("%s",(*ptr)[i].name);
scanf("%d",&((*ptr)[i].number));
scanf("%d",&((*ptr)[i].rank));
}
...
printf("%s",(*ptr)[i].name);
printf("%d",((*ptr)[i].number));
Correct way to use scanf would be:-(Notice that here 19 is used - otherwise there is a chance of buffer overrun - and this is being checked using this.This is one less than the size of the array because of the \0 character ) (Correct way)
if(scanf("%19s",ptr[i].name) != 1){
/* error in input */
fprintf(stderr,"Error in input\n");
exit(EXIT_FAILURE);
}
Compile your program with all warnings enabled. gcc -Wall -Werror progname.c.
Clearly you don't even need to use this pointer - why would you do that? You can simply use the array itself in the scanf or printf.

Dynamic Alocation in c not working

I'm getting exit(1)(if there's some error in the allocation) every time i try to run the case 1. And i have no ideia why, can i get some help ?
#include <stdio.h>
#include <stdlib.h>
typedef struct hospede{ //guest
int numreg; //register num
char nome[80];
int acompanhante; //n of ppl
int dias; //time to stay
estado tabela;
}hospede;
typedef struct quarto{ //room
int num; //num do quarto
char categoria; //[S] ou [F]
char status; //[L] ou [O]
estado stats;
}quarto;
void alocaHospede(hospede **hosp, int tam);
void alocaQuartos(quarto **quar, int tam);
void geraQuartos(quarto *quar);
void checkIn(hospede *hosp, int tam);
int main()
{
hospede *hpd = NULL;
quarto *qrt = NULL;
quarto quart;
int qtd = 0;
char op;
int x;
qrt = &quart;
geraQuartos(qrt);
do{
printf("\n1-CheckIn\n>");
scanf("%i", &x);
fflush(stdin);
switch(x){
case 1:
alocaHospede(&hpd, qtd+1);
checkIn(hpd, qtd);
}
printf("\nDeseja continuar? \n");
scanf("%c", &op);
fflush(stdin);
}while(op!='n' && op!='N');
return 0;
}
void checkIn(hospede *hosp, int tam){
printf("\nwork\n");
}//checkIn
void alocaHospede(hospede **hosp, int tam){
*hosp = (hospede*)realloc(*hosp, tam*sizeof(hospede));
if(*hosp == NULL)
exit(1);
}//aloca hospede
void alocaQuartos(quarto **quar, int tam){
if((*quar = (quarto *) realloc(*quar, tam * sizeof(quarto)))== NULL)
exit(1);
}//alocaQuartos
void geraQuartos(quarto *quar){
int i;
for(i=0;i<15;i++,quar++){
(*quar).num = i+1;
}
}//geraQuartos
OBS:
I Removed some structures and unions that aren't being used yet to make the code shorter, also i'll do the allocation to the rooms as well, i think it's the same problem.
The for loop in geraQuartos is treating its argument as a pointer to an array of 15 quartos structures. But it's a pointer to just one structure, so it's writing outside the bounds of the object. This is causing undefined behavior, and in this case it's overwriting hpd so it's not NULL. This then causes realloc to fail because the first argument isn't NULL or a pointer that was previously returned by malloc.
Change the declaration of quart to:
quarto quart[15];
and then do:
geraQuartos(quart);
geraQuartos(qrt) causes stack overflow since quart is only 1-entry-lengthed but you writes 1st ~ 15th entries inside the function.
Anything strange could happen on stack overflow.

array of structs in c-user input

I am new to programming in general and to C in particular.
I am trying to write a program that uses an array of structs, but I am experiencing problems if that struct contains strings.
Somehow the compiler crashes after the user has given the last input.
The struct below is just a simplified version containing only one item, because the problem seems to be reading strings into the array.
Any help is much appreciated, thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char* name;
}student;
int main()
{
int size;
printf("enter number of entries\n");
scanf("%d" , &size);
student* all=malloc(size*sizeof(student));
int i;
for(i=0;i<size;i++)
{
printf("enter name\n");
scanf("%s" , all[i].name);
}
return 0;
}
Before taking input scanf("%s" , all[i].name); , you need to allocate memory to all[i].name .
An example-
for(i=0;i<size;i++)
{
all[i].name=malloc(20*sizeof(*(all[i].name)));
if(all[i].name!=NULL){
printf("enter name\n");
scanf("%19s" , all[i].name);
}
}
//use these strings
for(i=0;i<size;i++){
free(all[i].name); //free the allocated memory
}
free(all);
Or in your structure instead of char * ,declare name as a char array (if you don't want to use dynamic allocation)-
typedef struct{
char name[20]; //give any desired size
}student;
/* no need to free in this case */
No memory is allocated for the students names (char* name), so when trying to scanf to that pointer, invalid memory is accessed and the program crashes.
The easiest way is to declare name as an array: char name[28];
The return value of malloc() needs to be checked too, in case there was problem allocating the memory for the students, which would return a NULL pointer. At the end, the allocated memory needs to be freed with free().
For example:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[28];
unsigned int age;
} student;
int main()
{
size_t size = 0;
printf("\nEnter number of entries: ");
scanf("%zu", &size);
// add some check for size
student* students = (student*)malloc(size * sizeof(student));
if (students == NULL) {
printf("\nProblem with allocating memory:\n"
" - size: %zu\n"
" - total size needed: %zu\n",
size, size * sizeof(student));
return 0;
}
for (size_t i = 0; i < size; ++i) {
printf("Enter name: ");
scanf("%27s", students[i].name);
printf(" Enter age: ");
scanf("%u", &students[i].age);
}
printf("\nList of students:\n");
for (size_t i = 0; i < size; ++i) {
printf("%s (%u)\n", students[i].name, students[i].age);
}
free(students); // free the allocated memory
return 0;
}

Stack Smashing Detected at End of Program

I am currently testing a program on a smaller scale to distinguish a problem when I attempt to exit the program by return 0; at the end of the main function.
Main.c
#include <stdio.h>
#include <stdlib.h>
#include "Header.h"
int main (void)
{
int i;
int Fin = 0;
Student sStu;
Array aAry;
Student *Stu = &sStu;
Array *Ary = &aAry;
InitArray(Ary, 1);
while(Fin != 2)
{
printf("Please choose a selection.\n");
printf("1. Add Student\n");
printf("2. Print Students\n");
printf("3. Exit\n");
scanf("%d", &i);
switch(i)
{
case 1:
{
AddStudent(Stu, Ary);
break;
}
case 2:
{
for(i = 0; i < Ary->Size; i++)
{
printf("%s %d\n", Stu[i].Name, Stu[i].IDn);
}
break;
}
case 3:
{
return 0;
}
}
}
}
Header.h
#ifndef HEADER_H_
#define HEADER_H_
typedef struct student
{
char Name[30];
int IDn;
}Student;
typedef struct array
{
Student *Student;
size_t Used;
size_t Size;
}Array;
void InitArray(Array *Ary, int InitSize);
void AddArray(Array *Ary);
Student AddStudent(Student *Stu, Array *Ary);
#endif
Grade.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Header.h"
void InitArray(Array *Ary, int InitSize)
{
Ary->Student = malloc(InitSize * sizeof(Student));
Ary->Used = 0;
Ary->Size = InitSize;
memset(&Ary->Student[0], 0 , sizeof(Student) * InitSize);
}
void AddArray(Array *Ary)
{
Student Stud;
if(Ary->Used == Ary->Size)
{
Ary->Size *= 2;
Ary->Student = realloc(Ary->Student, Ary->Size * sizeof(Student));
}
strcpy(Ary->Student[Ary->Used].Name, Stud.Name);
Ary->Student[Ary->Used].IDn = Stud.IDn;
Ary->Used++;
}
Student AddStudent(Student *Stu, Array *Ary)
{
int i;
printf("\nStudent ID numbers cannot be the same!\n");
printf("Please enter the student's name: ");
scanf("%s", Stu[Ary->Used].Name);
printf("Please enter the student's ID Number: ");
scanf("%d", &Stu[Ary->Used].IDn);
AddArray(Ary);
printf("\n");
return;
}
At the end of the file, when I try to return 0; I get this error:
* stack smashing detected *: ./a.out terminated
Segmentation fault (core dumped)
When I use valgrind, I get this output:
==9966== Invalid write of size 1
==9966== at 0x402C6C3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==9966== by 0x8048687: AddArray (in /home/ctj0033/CSCE_1040/Homework/2_Homework/2_Test/a.out)
==9966== by 0x804874B: AddStudent (in /home/ctj0033/CSCE_1040/Homework/2_Homework/2_Test/a.out)
==9966== by 0x804881C: main (in /home/ctj0033/CSCE_1040/Homework/2_Homework/2_Test/a.out)
==9966== Address 0x41f804c is 0 bytes after a block of size 36 alloc'd
valgrind terminates due to an fatal error during my attempt to exit my program.
It only happens at the end and I can print the contents of the struct array properly. I have looked into realloc(); since I am fairly sure the error lies within that, however I am uncertain precisely what I have done wrong. I have attempted to alter the way I allocate the memory in InitArray(); and the way I use realloc(); but to no avail.
What have I done wrong?
Your immediate problem is that you're trying to use an "invalid pointer". (Actually, it's a completely valid pointer, it just points to a wrong place)
sStu is a Student, and Stu is a pointer to it. But when you add your second student, you write to Stu[1] - which is the space after sStu. Most likely there's something else in this space, so when you write to Stu[1], you overwrite something else. (It's unpredictable exactly what will be in that space.)
It looks like you want to use Ary->Student everywhere you have Stu.
(If you do that, you might find some more problems - such as the fact that you check if you need to expand the array after writing to the next element)

Resources