943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 52930
  • C++ RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Mar 22nd, 2005
0

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

^^^where does the dynamic allocation of memory come into play then?
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 22nd, 2005
0

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

The function malloc is what you use to (attempt to) dynamically allocate memory.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 23rd, 2005
0

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

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()...
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Mar 24th, 2005
0

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

Quote 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
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 24th, 2005
0

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

Quote 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?
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 24th, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 25th, 2005
0

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

Quote 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'
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 25th, 2005
0

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

>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.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 26th, 2005
0

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

Quote 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.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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:
Quote 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.
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005
Mar 26th, 2005
0

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

^^^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
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Password verification C++
Next Thread in C++ Forum Timeline: Creating set as a tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC