Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have Linux installed on both my laptop (Debian Woody r3.2) and my desktop (RedHat 8). On both of these machines there is a very strange bug in gcc/g++. I am posting the code that runs and the code that causes a segmentation fault. If anyone knows why this bug occurs, please let me know.
The code that causes a segmentation fault is given below
# include <stdio.h>
void main (void)
{
int k,max_val;
int *array;
max_val = 10;
for (k=0;k<max_val;k++)
{
array[k] = k*max_val;
}
fflush(stdin);
fflush(stdout);
for (k=0;k<max_val;k++)
printf ("%d ", array[k]);
}
This compiles correctly in both gcc/g++ but causes a segmentation fault while running.
The code that runs correctly is given below
# include <stdio.h>
void main (void)
{
int k,max_val, pos, val;
int *array;
max_val = 10;
for (k=0;k<max_val;k++)
{
array[k] = k*max_val;
}
fflush(stdin);
fflush(stdout);
for (k=0;k<max_val;k++)
printf ("%d ", array[k]);
}
This runs perfectly and causes the array of 10 elements
0 10 20 30 40 50 60 70 80 90
to be displayed. It will be observed that there is absolutely no change between the two segments in the program except for the introduction of two unused variables in segment 2. It is beyond me why code segment 2 works and 1 does not. If any one has an answer, please let me know.
Regards,
Maidros
/* main should return "int" value */
int main (void)
{
int k,max_val;
/* you need to allocated storage, not an non initialized pointer */
int array[ARRAY_MAX];
max_val = ARRAY_MAX;
You have a pointer to an integer which you then treat as an array without having requested the appropriate memory using alloc. That explains the segmentation fault. As for why the second one works, well I think you're just lucky.
Code:
int main (void){
int k,max_val;
max_val = 10;
int *array = (int *)malloc(sizeof(int)*max_val);
for (k=0;k<max_val;k++){
array[k] = k*max_val;
}
fflush(stdin);
fflush(stdout);
for (k=0;k<max_val;k++)
printf ("%d ", array[k]);
return 0;
}
works perfectly. Oh you'll need to include stdlib.h as well. (for malloc). And you need max_val defined before calling malloc on it.
Originally posted by Komakino You have a pointer to an integer which you then treat as an array without having requested the appropriate memory using alloc. That explains the segmentation fault. As for why the second one works, well I think you're just lucky.
Code:
int main (void){
int k,max_val;
max_val = 10;
int *array = (int *)malloc(sizeof(int)*max_val);
for (k=0;k<max_val;k++){
array[k] = k*max_val;
}
fflush(stdin);
fflush(stdout);
for (k=0;k<max_val;k++)
printf ("%d ", array[k]);
return 0;
}
works perfectly. Oh you'll need to include stdlib.h as well. (for malloc). And you need max_val defined before calling malloc on it.
Thanks for the reply. I knew why the first one was not working, but I was shocked by the second one working. It should also not have worked, as you can see. Thanks for the reply anyway.
Regards,
Maidros
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.