Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Memory management in C

Name: Anonymous 2013-02-17 18:50

I'm trying to create an array (or technically pointer but I'm just gonna call it array from here on out because that's what I'm used to) that's just large enough to hold whatever is entered at the command line (except for the program name, obviously). The relevant code looks something like this:

int numCount = argc -1
int *num

num =(int*) malloc(numCount * sizeof(int))

after that I check if its null and then initialize it as so

memset(num, 0, numCount)


The problem that I'm running into is that the array seems to be much bigger than necessary. Also if I give 5 numbers at the command line and do a diagnostic print of num[6] I get a segmentation fault, but if I print num[7] or num[8] and so on, which also shouldn't exist I would think, it will be fine and print out a 0. I'm not used to programming in C or on Linux at all so I'm sure whatever I fucked up is something obvious, it would be nice to get a little help here.

Name: Anonymous 2013-02-17 18:53

>2012\
>LE PROGRAMMING
>IN C!!! XD
>NOT EVEN HAVING A CLUE WHAT YOUR DOING!!!
>LLLLLLLLLLLEEEEEEEEEEEELLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
>EGIN GRO
>ISHAGGYDOO

Name: Anonymous 2013-02-17 18:56

>>2
I SUPPORT THIS POST

Name: Anonymous 2013-02-17 18:56

main = do
     args <- getArgs
     let num :: [Integer] = map read args

Name: Anonymous 2013-02-17 18:57

So was this a bad place to ask.

Name: Anonymous 2013-02-17 19:04

>>1
I'm not going to be able to help you to the end of what you are trying to do, but memset(num, 0, numCount) ought to be memset(num, 0, numCount*sizeof(int)).

That third argument is the size of the block of memory, the same value that you gave to malloc.

Name: Anonymous 2013-02-17 19:05

>>6
DONT HELP HIM FAGLORD

Name: Anonymous 2013-02-17 19:07

>>5
Don't try to fool us JIDF kike. We know who you are and what are your evil plans. I have one word for you: Shalom! Get it?
Now move along and go scrub another toilet.

Name: Anonymous 2013-02-17 19:08

>>6

Oh right sorry that was a typo, it's correct in my program. I think I'm just gonna ask this in sci. I don't mind getting called stupid or a kike as long as they help while they do it.

Name: Anonymous 2013-02-17 19:09

>>9
Shalom!

Name: Anonymous 2013-02-17 19:10

check em dubsoluses

Name: Anonymous 2013-02-17 19:39

>>1 doesn't argv already do that?

Name: Anonymous 2013-02-17 20:02

>>12
I think >>1 wants to convert them to ints.

Don't change these.
Name: Email:
Entire Thread Thread List