calloc

Whatever I put as the arguments of calloc(..)...
1
2
3
int *v;
v =(int*)calloc(2,2);
cout<<sizeof(v);

the output come the same..Is it printing the size of the first element only? Or Is there something wrong with the allocation?
P.S:I have never tried using calloc before this, though new is a good alternative..it simply isn't applicable in the environment I am in right now.
It's printing the size of the pointer, which don't depend on what it's pointing to
though new is a good alternative
calloc is an alternative to malloc, but it is not an alternative to new.
Nothing wrong.
Is malloc() used in the same way ? Could you please provide some examples?
..and is the size of the pointer the same as that of its type?
The size of pointers doesn't depend on the pointed type, all pointers have the same size
Here are some useful links. Please read them:
http://www.cplusplus.com/reference/std/new/operator%20new/
http://www.cplusplus.com/reference/std/new/operator%20delete/
http://www.cplusplus.com/reference/std/new/operator%20new%5B%5D/
http://www.cplusplus.com/reference/std/new/operator%20delete%5B%5D/

http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/
http://www.cplusplus.com/reference/clibrary/cstdlib/calloc/
http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/
http://www.cplusplus.com/reference/clibrary/cstdlib/free/

Two important notes:

The examples for malloc/calloc pass the size of the type as determined by sizeof(). Your original snippet is rather disturbing in that it is making some assumptions that may hinder portability.

Also, new and delete, in C++, will invoke constructors/destructors. malloc/calloc will not!
I was just putting an example........
another question...Is there a fundamental way for console input & output..without using cin,cout ,printf..etc?
C provides printf/scanf and so on.
C++ provides cout/cint and so on.

These are provided by the languages and so are platform independent. If you go lower than that, you need to use platform specific methods.

So to answer your question, yes; but your need to say what platform you're on, and the code you use won't work on a different platform.

[edit: link removed, dunno where it came from]
Last edited on
Why the recursive link?
I am trying to modify a bit of chdk codes..(thus..in a canon camera). I am sure the original codes do not use the standard methods..but ..from the code..i can not decipher what is being used..
P.S: gcc is used to compile it without the optimizations
CHDK looks interesting.

Anyway, I have no idea what you're trying to do. But if you don't know what malloc does, I suggest you make an effort to understand the C virtual machine before you go any further. This looks like a helpful read to get you started: http://computer.howstuffworks.com/c28.htm
..the howstuffworks link explained it in a nice and friendly way...thanks
@@ can you give some examples of the platform specific code(if inline assembly needs to be used..it is for the ARM architecture)?
Topic archived. No new replies allowed.