| | |
C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#31 Mar 22nd, 2005
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#32 Mar 22nd, 2005
The function malloc is what you use to (attempt to) dynamically allocate memory.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#33 Mar 23rd, 2005
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#34 Mar 24th, 2005
•
•
•
•
Originally Posted by 1o0oBhP
however if you want to create an object dynamically the new keyword / operator will call the constructor for the object, malloc wont! I have found more uses for new than malloc()...
>the new keyword / operation will call the constructor for the object
what does that mean, i know what a construction and an object is, but what exactly do you mean by this
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#35 Mar 24th, 2005
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#36 Mar 24th, 2005
>what does that mean
It means that aside from allocating raw memory, the new operator in C++ will also call constructors.
>so the function malloc() is a predefined function?
Yes.
>That dynamically allocates memory to an object?
Your wording is toeing the line of accuracy, but yes.
It means that aside from allocating raw memory, the new operator in C++ will also call constructors.
>so the function malloc() is a predefined function?
Yes.
>That dynamically allocates memory to an object?
Your wording is toeing the line of accuracy, but yes.
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#37 Mar 25th, 2005
•
•
•
•
Originally Posted by Narue
>what does that mean
It means that aside from allocating raw memory, the new operator in C++ will also call constructors.
>so the function malloc() is a predefined function?
Yes.
>That dynamically allocates memory to an object?
Your wording is toeing the line of accuracy, but yes.
Lets see how confused I still am at this point as I attempt to understand Dynamic Memory Allocation and break down the meaning of this:
int main(void) //means int main() will not pass any arguments (I'm still not clear on what an argument is though) { int nrows = 10; //initializes object nrows as integer 10 int row, col; //initializes objects row and col as integers rptr = malloc(nrows * COLS * sizeof(int)); //initializes rptr to I DON'T KNOW...help! for (row = 0; row < nrows; row++) performs for loop 10 times { for (col = 0; col < COLS; col++) //performs this is nested for loop after every loop of for(row=0; row < nrows; row++), Narue, what is the value of COLS??? { rptr[row][col] = 17; //Narue, what happens when a double array is set equal to some value (i.e. 17)? } } return 0; //to this day I don't know what that means, I was taught that return 0 is used to terminate program }
Okay there's my breakdown of the code, please help me understand Na'
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#38 Mar 25th, 2005
>int main(void) //means int main() will not pass any arguments (I'm still not
>clear on what an argument is though)
main will not accept any arguments. When it comes to functions, you will hear two terms most often: argument and parameter. An argument is the value passed to a function in a function call and a parameter is the declaration of a local variable that holds an argument in a function declaration.
>//initializes objects row and col as integers
Neither row nor col are initialized.
>rptr = malloc(nrows * COLS * sizeof(int));
>//initializes rptr to I DON'T KNOW...help!
Initializes rptr to the first address of a block of memory (nrows * COLS * sizeof(int)) bytes.
>what is the value of COLS???
I have no idea, but the name suggests a macro. Something like this, perhaps:
>what happens when a double array is set equal to some value (i.e. 17)?
You're not setting the array to anything. You're setting the integer located at rptr[row][col] to 17.
>//to this day I don't know what that means
It's simple. Think of the C run-time environment calling your main function like so:
If main doesn't return a value then rc will remain uninitialized, and all kinds of bad things may happen if an uninitialized variable is used.
>clear on what an argument is though)
main will not accept any arguments. When it comes to functions, you will hear two terms most often: argument and parameter. An argument is the value passed to a function in a function call and a parameter is the declaration of a local variable that holds an argument in a function declaration.
C++ Syntax (Toggle Plain Text)
void f ( int i ); // i is a parameter f ( 10 ); // 10 is an argument
Neither row nor col are initialized.
>rptr = malloc(nrows * COLS * sizeof(int));
>//initializes rptr to I DON'T KNOW...help!
Initializes rptr to the first address of a block of memory (nrows * COLS * sizeof(int)) bytes.
>what is the value of COLS???
I have no idea, but the name suggests a macro. Something like this, perhaps:
C++ Syntax (Toggle Plain Text)
#define COLS 10
You're not setting the array to anything. You're setting the integer located at rptr[row][col] to 17.
>//to this day I don't know what that means
It's simple. Think of the C run-time environment calling your main function like so:
C++ Syntax (Toggle Plain Text)
int rc = main(); /* Do something with rc */
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#39 Mar 26th, 2005
•
•
•
•
Originally Posted by Narue
>int main(void) //means int main() will not pass any arguments (I'm still not
>clear on what an argument is though)
main will not accept any arguments. When it comes to functions, you will hear two terms most often: argument and parameter. An argument is the value passed to a function in a function call and a parameter is the declaration of a local variable that holds an argument in a function declaration.
>//initializes objects row and col as integersC++ Syntax (Toggle Plain Text)
void f ( int i ); // i is a parameter f ( 10 ); // 10 is an argument
Neither row nor col are initialized.
>rptr = malloc(nrows * COLS * sizeof(int));
>//initializes rptr to I DON'T KNOW...help!
Initializes rptr to the first address of a block of memory (nrows * COLS * sizeof(int)) bytes.
>what is the value of COLS???
I have no idea, but the name suggests a macro. Something like this, perhaps:
>what happens when a double array is set equal to some value (i.e. 17)?C++ Syntax (Toggle Plain Text)
#define COLS 10
You're not setting the array to anything. You're setting the integer located at rptr[row][col] to 17.
>//to this day I don't know what that means
It's simple. Think of the C run-time environment calling your main function like so:
If main doesn't return a value then rc will remain uninitialized, and all kinds of bad things may happen if an uninitialized variable is used.C++ Syntax (Toggle Plain Text)
int rc = main(); /* Do something with rc */
This is great, I mean to you this is elementry, but I'm getting it, you know :mrgreen:
I do have a couple questions though...but first thank you for the breakdown of parameters and arguments...i've read books, asked college professors, and your example was the one...thanks to you and can't forget Dave I GOT IT NOW! I understand what "int main (void)" means and what rptr[row][col] = 17 means (just to think a couple days ago i didn't even understand arrays, let alone double arrays (go here: CLICK)
1. If row and col wasn't being initialized was it being Declared?
2. and why is it that row and col wasn't initialized, but rptr is?
3.
C++ Syntax (Toggle Plain Text)
rptr = malloc(nrows * COLS * sizeof(int));
okay not let me see if i understand what malloc() is doing, tell me if im wrong, but going by what you and dave said:
••••Originally Posted by Dave SinkulaYou request from malloc space for 10 objects each of which are sizeof(int) bytes in size -- you ask for space for 10 ints.
rptr is the lvalue (i.e. address) of an object with a rvalue of memory that is going to be dynamically allocated in "blocks" using the function malloc(). Now with nrows, we are requesting 10 spaces (because nrows = 10) from malloc sizeof(int) which is 4 bytes for every integer (using a 32 bit Pentium processor). The same goes for COLS, but we don't how many spaces is being requested, because COLS could be a marco, meaning an alias.
4. Is my understanding of how a nested for loop works correct?
Note: lets say COLS = 20 for sake of argument.
(a)Does the 1st for loop create a row (i.e. row = 0), then moves to second for loop?
(b)Does the 2nd for loop create a col (i.e. col = 0), then sets the integer located at rptr[0][0] to 17?
(c)Does the second for loop create another col (i.e. col = 1), then sets the integer located at rptr[0][1] to 17?
(d)Does this go on until 20 col's are created for row 0, then goes back to the 1st for loop and does the same for row 1 (i.e. row = 0)?
I guess what im saying would kind of llike this:
1st CYCLE: rptr = [0][0] = 17, then rptr=[0][1] = 17,........, then rptr=[0][19]= 17, end cycle
2nd CYCLE: rptr = [1][0] = 17, then rptr=[1][1] = 17,........,then rptr=[1][19], end cycle
.............and so on and so forth
5. Narue, your example of why int main() must return a value, is saying that after int main() is executed at runtime if you don't give it an integer value your program could start acting up? And, is int main(void) kind of an exception to the rule where a value ISN'T needed therefore, int main(void) isn't initialized after the program is executed at run time?
I KNOW THAT'S ALOT OF QUESTIONS (i tried to keep them as sort as possible), please help me, your answers are vital to my understanding of C++. I want to get better at programming and have a lot to learn.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
0
#40 Mar 26th, 2005
^^^Ooops! didn't know you can't edit responses more than once, sorry here's the link (its in your private message too)
go here: TRIPLE ARRAYS
go here: TRIPLE ARRAYS
![]() |
Other Threads in the C++ Forum
- Previous Thread: Exercise using: unsigned int datecode(int year, int month, int day);
- Next Thread: Break Out Of A Function~
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






