allocating memory for arrays

Started by
5 comments, last by Sabonis 18 years, 4 months ago
hey guys, I'm writing a program in C that will fill in an allocated array of doubles... My question is: What is the right way to allocate memory for an array of doubles with malloc()? double *input; input = (double*)malloc(sizeof(double)*num_elements); is what i have so far... doesnt seem to work. Any ideas?
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Advertisement
What do you mean by "doesn't seem to work"?
Quote:Original post by SiCrane
What do you mean by "doesn't seem to work"?


... as in when I try to access the elements my program crashes
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Is the return value from malloc() 0? If so you might have heap corruption somewhere else in your program, or num_elements could be too big.
i would like to know if this is proper way to allocate an array of doubles in the free store? or are there other ways?
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Looks correct for C.
ok thanks for the help... was trying to access past the end of the array... oopsie
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...

This topic is closed to new replies.

Advertisement