Pages: [1]   Go Down
Author Topic: Problem with malloc()  (Read 1171 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I'm a beginner in programing with C and I'm having some problems with malloc. I dont know what is wrong in the following code:
Code:
void setup(){
int *vetorV;
vetorV =  malloc (40*sizeof(int));
}

void loop(){}
I keep geting the error mensage: "invalid conversion from 'void*' to 'int*'". I'm sorry if the answer to my question is to obvious, but I've already looked up in a lot of tutorials and related topics and couldn't find anything helpfull.
Thanks
Logged

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

Code:
vetorV = (int *) malloc(40*sizeof(int));

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks RuggedCircuits, but I had already tried that. Although the program would compile, it seems that it isn't working properly. When I try "sizeof(vectorV)", it always returns 2, no matter what the argument to malloc is. Loking up on the internet, I found this:
Code:
int *ptr;
ptr = malloc(10 * sizeof (int)); // Without a cast
ptr = (int*)malloc(10 * sizeof (int)); // With a cast
"Adding the cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found."
http://en.wikipedia.org/wiki/Malloc
I'm not sure that really is the problem, but even if it is, I undestood from other topics tha the stdlib.h is already included in any arduino program and I dont know how to fix it.
PS: I tried using "#include <stdlib.h>" at the beginning of the code, but it didn't make any difference.
Logged

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I try "sizeof(vectorV)", it always returns 2, no matter what the argument to malloc is.

That's correct. sizeof(vectorV) is the size of an "int *" because that is the type of vectorV. An "int *" is an address, and on the AVR an address is 16 bits, or 2 bytes. So sizeof is correct.

If you are a beginner in C then you're playing in the wrong waters. Pointer programming and malloc() are considered "advanced" usage. I'd suggest looking at some of the many C programming tutorials on-line on pointers and make sure you understand those first. Then you can move on to malloc.

--
The DIN Rail Mount kit for Arduino: quickly attach your Arduino to standard DIN rail
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey RuggedCircuits, thanks a lot!!! I hadn't considered that. I know that using malloc is quite complicated for beginners but, for what I need, I dont see any alternative rather than dynamically expand an array. Just one more thing: how can I be sure that I'm not trying to write on an unavailable memory space?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how can I be sure that I'm not trying to write on an unavailable memory space?
Without a memory management unit, you can't!
Welcome to the World of C!
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
for what I need, I dont see any alternative rather than dynamically expand an array.

You shouldn't be thinking about dynamically expanding arrays anyways in a limited-RAM microcontroller environment. Consider that on an Uno you have 2K of RAM. That's not very much and it shouldn't be wasted with the overhead of dynamic memory allocation (malloc). On a system with this little RAM it's best to just plan ahead for what you need to do and set aside space ahead of time. You can't think of programming like you do on a PC where you can always just make arrays bigger because you have lots and lots of RAM.

--
The Quick Shield: breakout all 28 pins to quick-connect terminals
Logged

Pages: [1]   Go Up
Jump to: