Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

Programming problem!

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
4 posts • Page 1 of 1
Author
Message
lefsha
Veteran
Veteran
Posts: 1235
Joined: Mon Aug 30, 2004 5:02 pm
Location: Burgas, Bulgaria

Programming problem!

  • Quote

Post by lefsha » Thu Jun 16, 2005 10:02 pm

I just wrote some prog.

It was not perfect - some bugs in it.
But OK it still works.

After some update to gcc4.0 and glibc a could not start my program without
SEG FAULT.
If i try to debug it to find out where the bug is it set me the break point just
after the begining of main() function.

OK. I have commented complete source just left return 0; at the end.
It works. Then I add more and more source to the working code to get know
where it breaks. And I find quite normal code:

Code: Select all

{
		FILE *out = fopen("source.ascii","wb");
		for(int i = 0; i < dim; i++)
		{
			fprintf(out, "%.8e\t%.8e\n", x_dat[i], y_dat[i]);
		}
		fclose(out);
}
As you see nothing special.

Then I switched back to gcc 3.4.4 and glibc 2.3.5 - it was OK before with them.
Nothing is changing.

I tried to compile glibc with old compiler - it does not help.

Then I found info about valgrind - good tool for debug
and what it does says to me:

Code: Select all


and many times before. ->
-> ==18263== Conditional jump or move depends on uninitialised value(s)
==18263==    at 0x1B8EDE89: (within /lib/ld-2.3.5.so)
==18263==    by 0x1B8E6AC2: (within /lib/ld-2.3.5.so)
==18263==    by 0x1B8F4909: (within /lib/ld-2.3.5.so)
==18263==    by 0x756E694B: ???
==18263==
==18263== Conditional jump or move depends on uninitialised value(s)
==18263==    at 0x1B8EDFC0: (within /lib/ld-2.3.5.so)
==18263==    by 0x1B8E6AC2: (within /lib/ld-2.3.5.so)
==18263==    by 0x1B8F4909: (within /lib/ld-2.3.5.so)
==18263==    by 0x756E694B: ???
==18263==
==18263== Invalid read of size 1
==18263==    at 0x1BA5C832: vfprintf (in /lib/libc-2.3.5.so)
==18263==    by 0x1BA64F02: fprintf (in /lib/libc-2.3.5.so)
==18263==    by 0x1BA34F36: __libc_start_main (in /lib/libc-2.3.5.so)
==18263==  Address 0x46 is not stack'd, malloc'd or (recently) free'd
==18263==
==18263== Process terminating with default action of signal 11 (SIGSEGV)
==18263==  Access not within mapped region at address 0x46
==18263==    at 0x1BA5C832: vfprintf (in /lib/libc-2.3.5.so)
==18263==    by 0x1BA64F02: fprintf (in /lib/libc-2.3.5.so)
==18263==    by 0x1BA34F36: __libc_start_main (in /lib/libc-2.3.5.so)
==18263==
==18263== ERROR SUMMARY: 22 errors from 8 contexts (suppressed: 0 from 0)
==18263== malloc/free: in use at exit: 22416 bytes in 2 blocks.
==18263== malloc/free: 5 allocs, 3 frees, 26182 bytes allocated.
==18263== For counts of detected errors, rerun with: -v
==18263== searching for pointers to 2 not-freed blocks.
==18263== checked 161680 bytes.
==18263==
==18263== LEAK SUMMARY:
==18263==    definitely lost: 0 bytes in 0 blocks.
==18263==      possibly lost: 0 bytes in 0 blocks.
==18263==    still reachable: 22416 bytes in 2 blocks.
==18263==         suppressed: 0 bytes in 0 blocks.
==18263== Reachable blocks (those to which a pointer was found) are not shown.
==18263== To see them, rerun with: --show-reachable=yes
Segmentation fault
So what does it mean?

Code: Select all

==18263== Conditional jump or move depends on uninitialised value(s)
==18263==    at 0x1B8EDE89: (within /lib/ld-2.3.5.so)
==18263==    by 0x1B8E6AC2: (within /lib/ld-2.3.5.so)
==18263==    by 0x1B8F4909: (within /lib/ld-2.3.5.so)
==18263==    by 0x756E694B: ???
Such messages I have had before. I don't know where it came from,
but program still works in such case.

I'm quite sure that my code is more or less right.
There are some other errors - but they doesn't related to this problem.

Thanks in advance!
Lefsha
Top
30726
Veteran
Veteran
Posts: 1501
Joined: Wed Sep 24, 2003 11:01 pm

  • Quote

Post by 30726 » Thu Jun 16, 2005 10:56 pm

Mind posting the source code? Check if the call to fopen was successful:

Code: Select all

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

...

if (!out)
{       
        perror("Failed to open file for writing");
        return EXIT_FAILURE;
}
Top
apmurray
Apprentice
Apprentice
Posts: 221
Joined: Sat May 29, 2004 2:02 pm
Location: Adelaide, Australia
Contact:
Contact apmurray
Website

  • Quote

Post by apmurray » Thu Jun 16, 2005 11:04 pm

I am not an expert with valgrind but I think thats just to do with the linking, so I wouldn't be too worried..
Is this a C or C++ program (C I assume), so you shouldn't be declaring a new variable i at the start of your for loop, this should be declared before the for loop starts ie:

Code: Select all

{
      int i;
      FILE *out = fopen("source.ascii","wb");
      for(i = 0; i < dim; i++)
      {
         fprintf(out, "%.8e\t%.8e\n", x_dat[i], y_dat[i]);
      }
      fclose(out);
}
But I suspect that the segfault is due to you overunning the bounds of the array or something, as where do you define how big dim is?? Also you should do a

Code: Select all

if (NULL == (FILE *out = fopen("source.ascii","wb")) { // there is no need to explicitly check against NULL but is just more readable
     printf("Could not open file %s for writing.", "source.ascii");
     return 1;
}
As then you won't try to write to the file if it could not be opened.
Top
lefsha
Veteran
Veteran
Posts: 1235
Joined: Mon Aug 30, 2004 5:02 pm
Location: Burgas, Bulgaria

  • Quote

Post by lefsha » Fri Jun 17, 2005 12:21 am

I'm sorry.

It was my mistake.

I set under Anjuta, that it is only C++ code!
I thought that is no difference.
But it has build program without any notification that something
is not OK or that some libs are missed.

I have no experience with C++ input output,
that is why I use in this case always C construction.
It works and I don't need something else.

But I forgot to say that it is C and C++ code....

And because I've got no warnings, I thought that all
seems to be OK.

Well, every day you have to learn till you die.

Thanks to all!
Lefsha
Top
Post Reply

4 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic