473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Compiling in VS gives strange results

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.

Nov 17 '05 #1
6 1528
"Joe Piscapo" <jo*****@hotmail.com> wrote in message
news:c1*************@ID-198839.news.uni-berlin.de...
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
Nov 17 '05 #2
Joe Piscapo wrote:
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
Nov 17 '05 #3
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 ();

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:40**********************@aconews.univie.ac.at ...
Joe Piscapo wrote:
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

Nov 17 '05 #4
Joe Piscapo wrote:
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
Nov 17 '05 #5
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.

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:40***********************@aconews.univie.ac.a t...
Joe Piscapo wrote:
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

Nov 17 '05 #6
Joe Piscapo wrote:
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
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
12
by: Markus Ewald | last post by:
I'm just experimenting around with the VisualC++ 2005 Express Edition Beta and found some strange behavior for which I can't seem to find a workaround. What I've done is set up two static library...
0
by: phil-news-nospam | last post by:
I was experimenting with buttons in HTML/CSS and encountered some strange behaviour when changing the padding property setting in the stylesheet. Maybe it's not a stylesheet issue, but since that's...
17
by: aamircheema | last post by:
Hi, When I compile my program adding -O option, the running time of my program is much smaller. For example if i compile my program like this g++ -Wall -g prog2.cc avltree.cc cells_list.cc...
5
by: =?Utf-8?B?RmFlc3NsZXIgR2lsbGVz?= | last post by:
Hello, I'm facing a strange problem. We have an Asp.net 2.0 Web Application and somehow the date format is changing from one client to another even if all the code is running server-side... ...
2
by: renagade629 | last post by:
Can anybody help me understand what i'm doing wrong or what I'm missing? Is there anyother good and commendable C++ program I can use (free) from the internet like Dev C++? I'm having trouble doing...
13
by: Albert | last post by:
Hi I'm using the lcc compiler for win32. I tried compiling a program but there's an error stating: "cpp: Can't open input file clrscr()" I don't get it - I've included <tcconio.h>. (strange why...
14
by: yeah | last post by:
hi all I am working with linux environment and i compiled device driver programs using native compiler "gcc".In one of my macro i have these lines #if (1 << HARDIRQ_BITS) < MAX_HARDIRQS_PER_CPU...
63
by: Kapteyn's Star | last post by:
Hi newsgroup The program here given is refused by GCC with a error i cannot understand. It says rnd00.c: In function ‘main’: rnd00.c:26: error: expected expression before ‘]’ token ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.