LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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

Reply
 
Search this Thread
Old 05-08-2004, 05:07 AM   #1
Maidros
Member
 
Registered: Jan 2004
Location: Haifa, Israel
Distribution: Debian, FreeBSD, Solaris
Posts: 101

Rep: Reputation: 15
Extremely strange bug in gcc/g++


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
 
Old 05-08-2004, 06:34 AM   #2
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
# include <stdio.h>

#define ARRAY_MAX 10

/* 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;

for (k=0;k<max_val;k++)
{
array[k] = k*max_val;
}

/*
// flushing stdin is useless - fflush flushed output buffers
fflush(stdin);
*/
fflush(stdout);

for (k=0;k<max_val;k++)
printf ("%d ", array[k]);

/* you should return "int" from main */
return 0;
}
 
Old 05-08-2004, 06:41 AM   #3
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 54
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.
 
Old 05-08-2004, 07:28 AM   #4
Maidros
Member
 
Registered: Jan 2004
Location: Haifa, Israel
Distribution: Debian, FreeBSD, Solaris
Posts: 101

Original Poster
Rep: Reputation: 15
Quote:
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ati fglrx driver... strange bug kule Slackware 8 03-08-2005 03:49 PM
Strange problem or bug bato Slackware 3 02-18-2005 02:56 PM
KDE: A strange bug (SuSE 9.1) SuSExplorer Suse/Novell 4 12-31-2004 11:25 PM
bug reports on glibc or gcc tvburger Linux From Scratch 1 12-06-2004 01:43 AM
gcc bug? fab12 Programming 2 02-07-2004 08:44 AM


All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration