C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #31
Mar 22nd, 2005
^^^where does the dynamic allocation of memory come into play then?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #33
Mar 23rd, 2005
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()...
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays

 
0
  #35
Mar 24th, 2005
Originally Posted by Dave Sinkula
The function malloc is what you use to (attempt to) dynamically allocate memory.


so the function malloc() is a predefined function? That dynamically allocates memory to an object?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,705
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 731
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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.
^^^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'
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,705
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 731
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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.
  1. void f ( int i ); // i is a parameter
  2. f ( 10 ); // 10 is an argument
>//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:
  1. #define COLS 10
>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:
  1. int rc = main();
  2. /* Do something with rc */
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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.
  1. void f ( int i ); // i is a parameter
  2. f ( 10 ); // 10 is an argument
>//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:
  1. #define COLS 10
>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:
  1. int rc = main();
  2. /* Do something with rc */
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.

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.
  1. 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 Sinkula
You 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC