•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,896 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,601 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 33045 | Replies: 45
![]() |
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Rep Power: 4
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#31
Mar 22nd, 2005
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#32
Mar 22nd, 2005
The function malloc is what you use to (attempt to) dynamically allocate memory.
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#33
Mar 23rd, 2005
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Rep Power: 4
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#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:
Rep Power: 4
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#35
Mar 24th, 2005
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#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.
Member of: Beautiful Code Club.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Rep Power: 4
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#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.
^^^Thanks!!!...I feel like I'm really learning Narue, I'm slowly upping my knowledge level
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
#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.
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:
#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:
int rc = main(); /* Do something with rc */
Member of: Beautiful Code Club.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Rep Power: 4
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#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 integersvoid 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)?#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.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.
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:
Rep Power: 4
Solved Threads: 0
Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
#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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
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~



Linear Mode