Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2004

Tip: Looking for answers? Try searching our database.

Compiling in VS gives strange results

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe Piscapo - 28 Feb 2004 01:26 GMT
Hello,
When I compile my program in Visual Studio it does not work for every input,
some numbers make it crash.  But compiling in gcc works for those numbers
that made it crash in Visual Studio.  Someone told me to check if Visual
Studio is using the gcc libraries.  How do I do that?  I found an option for
Runtime Library that has options like Single-threaded, etc.
William DePalo [MVP VC++] - 28 Feb 2004 02:29 GMT
> When I compile my program in Visual Studio it does not work for every input,
> some numbers make it crash.

Hmm. If this problem is repeatable, post the few lines of code along with
the data that cause the crash.

> Someone told me to check if Visual
> Studio is using the gcc libraries.  How do I do that?

The source to whatever common libaries there may be is most definitely
different.

Regards,
Will
Christoph Rabel - 28 Feb 2004 11:06 GMT
> When I compile my program in Visual Studio it does not work for every input,
> some numbers make it crash.  But compiling in gcc works for those numbers
> that made it crash in Visual Studio.  Someone told me to check if Visual
> Studio is using the gcc libraries.  How do I do that?  I found an option for
> Runtime Library that has options like Single-threaded, etc.

It could be an uninitialized variable.

At least on Linux you definitely get zeroed memory at the
start of the program. I dont know if gcc does this also in
Windows.

VC does not zero out the memory, so it has some random
numbers in it.

I had this problem frequently when trying to port code. The
error lies in the program, in assuming that the memory is NULL.

hth

Christoph
Joe Piscapo - 29 Feb 2004 20:06 GMT
Try any numbers, but these numbers make the program crash 2468,
8902

conjecture.c

#include <stdio.h>

#include "sieve.h"

#include <string.h>

int getPrimes (int a[], int low, int high, int num, int lowIndex,

int HighIndex);

int getNextIndex (int a[], int index, int s);

extern int holder[];

/**

* assigns two variables to store primes, one the

* smallest prime (low prime) and another stores the highest

* prime that is less than num (high prime)

*/

int

main ()

{

findPrimes ();

int infinite = 1;

while (infinite)

{

int num; // the even number that can be represented by two primes

if (scanf ("%d", &num) == EOF)

break;

if (num % 2 != 0 || num < 3 || num > 100000)

{

fprintf (stderr, "%d is an invalid number\n", num);

continue;

}

int high = 0; // the low prime

int low = 0; // the high prime

int highIndex = 0;

int lowIndex = 0;

low = holder[lowIndex]; // initially low prime is first element (2)

int i = 0;

for (; i < N - 1; i++) // find the high prime

{

if (holder[i] != 0 && holder[i] < num)

high = holder[i];

highIndex = i;

}

getPrimes (holder, low, high, num, lowIndex, highIndex);

}

return 0;

}

/**

* given both the low and high prime, this function

* will increase the low prime and decrease the

* high prime until low prime + high prime = num

*/

int

getPrimes (int a[], int low, int high, int num, int lowIndex, int highIndex)

{

if (lowIndex > N - 1 || highIndex < 0)

{

printf ("No primes found\n");

return 1;

}

else if (high + low == num)

{

printf ("%d %d\n", low, high);

return 0;

}

else if (high + low < num) // get the next biggest low prime

{

lowIndex = getNextIndex (holder, lowIndex, 0);

low = holder[lowIndex];

return getPrimes (holder, low, high, num, lowIndex, highIndex);

}

else if (high + low > num) // get the next smallest high prime

{

highIndex = getNextIndex (holder, highIndex, 1);

high = holder[highIndex];

return getPrimes (holder, low, high, num, lowIndex, highIndex);

}

return 1;

}

/**

* returns the index of the next prime

* if s = 0 then search to the right of current index in the array

* if s = 1 then search to the left of current index in the array

*/

int

getNextIndex (int a[], int index, int s)

{

if (s == 0)

{

int i = index + 1;

for (; i < N - 1; i++)

{

if (a[i] != 0)

return i;

}

}

else

{

int i = index - 1;

for (; i >= 0; i--)

{

if (a[i] != 0)

return i;

}

}

return 1;

}

===========

sieve.c

#include <math.h>

#include <stdio.h>

#include "sieve.h"

int findPrimes ();

int holder[N];

/**

* findPrimes() stores all the primes

* from 2 to N in an array.

*/

int

findPrimes ()

{

int i = 0;

int j = 2;

for (; i < N - 1; i++) // delete first two elements

{ // and shift all elements two places to the left

holder[i] = j;

++j;

}

float fN = N;

float square = sqrt (fN);

int prime = 0;

int k = 0;

for (; k < N - 1; k++)

{ // loop finds next prime

if (holder[k] == 0)

continue;

prime = holder[k];

if (holder[k] > square) // no need if to continue

break; // if prime is > square of N

int p = k + 1;

for (; p < N - 1; p++)

{ // loop finds multiples of prime

if (holder[p] == 0) // then get next element

continue;

if (holder[p] % prime == 0)

{ // its a multiple

holder[p] = 0;

}

}

}

return 0;

}

===============

the header file

enum

{ N = 100000 };

extern int holder[];

int findPrimes ();

> > When I compile my program in Visual Studio it does not work for every input,
> > some numbers make it crash.  But compiling in gcc works for those numbers
[quoted text clipped - 17 lines]
>
> Christoph
Christoph Rabel - 29 Feb 2004 21:47 GMT
> Try any numbers, but these numbers make the program crash 2468,
> 8902

[Code snipped]

I tried with VS 6 and VS 7.1 and your code doesnt crash for me.

I get the following:

2468
31 2437
8902
41 8861

Dont know if these output is correct I didnt look at what
your program does (the algorithm)

What might be is that the array of 100000 ints is too big
and you get a stack overflow.

Why dont you use at least an array of char or if you can
switch to C++ even better a vector<bool>. vector<bool> would
only need about 12500 Bytes for 100000 Elements?

Or create the array on the heap:

int * holder = new int[N];

hth

Christoph
Joe Piscapo - 29 Feb 2004 22:41 GMT
Yes those outputs are correct it finds 2 primes that add to the number.  Hmm
can you tell me how you compile it?  Did you have to create a C++ project,
then remove the c++ files and import the .c files?  I remember I had to make
sure "Not Using Precompiled Headers" was selected.
There are also an option for Compile as C++ Code, etc.

> > Try any numbers, but these numbers make the program crash 2468,
> > 8902
[quoted text clipped - 27 lines]
>
> Christoph
Christoph Rabel - 29 Feb 2004 23:12 GMT
> Yes those outputs are correct it finds 2 primes that add to the number.  Hmm
> can you tell me how you compile it?  Did you have to create a C++ project,
> then remove the c++ files and import the .c files?  I remember I had to make
> sure "Not Using Precompiled Headers" was selected.
> There are also an option for Compile as C++ Code, etc.

First: Please dont top-post!

There is a patch/addin for outlook that fixes most of its
annoying properties, look here:

http://home.in.tum.de/~jain/software/oe-quotefix/

I did nothing special to compile it. I used my standard C++
project settings and just copied your code into it. I did
ignore your filenames, everything was compiled as C++

I used Precompiled Headers, but that doesnt matter, it only
affects compile times.

After a bit testing now I got a Stack overflow during
debugging. Consider my advises for this problem, probably
putting the array on the heap with malloc or new will solve it.
And maybe rearrange your code, the loop in findprimes is not
necessary and you can simply use a char instead of the
number if you discard this loop, decreasing them size of
your array.

hth

Christoph

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.