Jump to content

C Program Compiles but Segfaults [Commented Code]

LtStaffel

Hello everyone,

 

This is a prime number generator up to a given number. It compiles but segmentation faults 70% of the time and does not print when it doesn't segfault. Any idea what's wrong with it? This is a C version of what I already wrote (and is working) in Python. I suspect it has to do with my use of sizeof().

 

Thanks in advance

 

Code:

Spoiler

// obvious include
#include <stdio.h>

// main
int main(int argc, char *argv[]){
    // grab limiting number from first CLI argument
	int limit = (int)argv[1];
    // I want each value to be the same as its index
	int nums[limit+1];
    // populating the array
	for(int i = 0; i <= sizeof(nums); i++){
		nums[i] = i;
	}

    // I don't want to use the number 1 because it will mark off all my numbers
	nums[1] = 0;

    // start a loop
	for(int k = 0; k < sizeof(nums); k++){
        // if the number is not marked
		if(nums[k] != 0){
            // print the number we are on that is not marked
			printf("%d ",nums[k]);
            // mark every multiple of our current number
			for(int j = k; j+k <= limit; j+=k){
                // the marking
				nums[j] = 0;
			}
		}
	}
	// return
	return 0;
}

 

 

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

You need to divide the array by the size of an int. sizeof(array) will return the length of the array*the size of the data. So sizeof() on an array with four ints would return 16 instead of 4.

 

for(int i=0; i<=sizeof(nums)/sizeof(int); i++)

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, fizzlesticks said:

int limit = (int)argv[1];

argv is an array of "strings", you can't just use a cast to get the int value.

Thanks!

I was told not to use atoi(), what would be the best route?

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, djdwosk97 said:

You need to divide the array by the size of an int. sizeof(array) will return the length of the array*the size of the data. So sizeof() on an array with four ints would return 16 instead of 4.


for(int i=0; i<=sizeof(nums)/sizeof(int); i++)

 

Thanks!

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×