^^^where does the dynamic allocation of memory come into play then?
Fasola 0 Junior Poster
Dave Sinkula 2,398 long time no c Team Colleague
The function malloc is what you use to (attempt to) dynamically allocate memory.
1o0oBhP 4 Posting Pro in Training
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()...
Fasola 0 Junior Poster
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
Fasola 0 Junior Poster
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?
Narue 5,707 Bad Cop Team Colleague
>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.
Fasola 0 Junior Poster
>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' ;)
Narue 5,707 Bad Cop Team Colleague
>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.
void f ( int i ); // i is a parameter
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:
#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:
int rc = main();
/* 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.
Fasola 0 Junior Poster
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)
- If row and col wasn't being initialized was it being Declared?
- and why is it that row and col wasn't initialized, but rptr is?
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:
You request from
malloc
space for 10 objects each of which aresizeof(int)
bytes in size -- you ask for space for 10int
s.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.
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
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.
Edited by Dani because: Formatting fixed
Fasola 0 Junior Poster
^^^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
Narue 5,707 Bad Cop Team Colleague
>1. If row and col wasn't being initialized was it being Declared?
Defined, actually. But declarations and definitions can be tricky. A definition is always a declaration, but a declaration is not always a definition.
In C it gets so confusing that a definition may look like a definition but still only be a declaration. ;) However, you can save learning that until you want to consider yourself an expert in C.
>2. and why is it that row and col wasn't initialized, but rptr is?
Because row and col aren't assigned an explicit value while rptr is (through malloc's return value).
>3.
malloc returns a block of memory. Memory is just a bunch of bytes all stuck together, and malloc returns a block of N bytes where N is a value that you pass to it. Assuming COLS is 10 and sizeof ( int ) is 4, you would be asking for 400 bytes, or 10 * 10 * 4. malloc doesn't care how you use the memory, it returns the equivalent of a pointer to unsigned char. When you assign the block to a pointer of type int, you can treat the memory as a sequence of 100 integers, or 10 * 10 because even though an int is 4 bytes, we're now treating it as a single unit.
The same thing would happen if rptr were of type double. Assuming sizeof ( double ) is 8, you would be allocating 800 bytes, but still treating that block as a sequence of 100 units (this time of type double).
>4. Is my understanding of how a nested for loop works correct?
You're overcomplicating things. The outer loop executes the inner loop. So if the inner loop initializes one row, the outer loop initializes all of the rows.
>if you don't give it an integer value your program could start acting up?
If you don't return a value then you've invoked undefined behavior, and that's bad. However, the rules are different depending on what version of C or C++ you're using.
>is int main(void) kind of an exception to the rule where a value ISN'T needed therefore
No, don't confuse parameters with return values. int main(void) simply means that main takes no arguments, but it still must return an integer.
Fasola 0 Junior Poster
>1. If row and col wasn't being initialized was it being Declared?
Defined, actually. But declarations and definitions can be tricky. A definition is always a declaration, but a declaration is not always a definition.In C it gets so confusing that a definition may look like a definition but still only be a declaration. ;) However, you can save learning that until you want to consider yourself an expert in C.
>2. and why is it that row and col wasn't initialized, but rptr is?
Because row and col aren't assigned an explicit value while rptr is (through malloc's return value).>3.
malloc returns a block of memory. Memory is just a bunch of bytes all stuck together, and malloc returns a block of N bytes where N is a value that you pass to it. Assuming COLS is 10 and sizeof ( int ) is 4, you would be asking for 400 bytes, or 10 * 10 * 4. malloc doesn't care how you use the memory, it returns the equivalent of a pointer to unsigned char. When you assign the block to a pointer of type int, you can treat the memory as a sequence of 100 integers, or 10 * 10 because even though an int is 4 bytes, we're now treating it as a single unit.The same thing would happen if rptr were of type double. Assuming sizeof ( double ) is 8, you would be allocating 800 bytes, but still treating that block as a sequence of 100 units (this time of type double).
>4. Is my understanding of how a nested for loop works correct?
You're overcomplicating things. The outer loop executes the inner loop. So if the inner loop initializes one row, the outer loop initializes all of the rows.>if you don't give it an integer value your program could start acting up?
If you don't return a value then you've invoked undefined behavior, and that's bad. However, the rules are different depending on what version of C or C++ you're using.>is int main(void) kind of an exception to the rule where a value ISN'T needed therefore
No, don't confuse parameters with return values. int main(void) simply means that main takes no arguments, but it still must return an integer.
I got it! Thank you for answering all my questions, I think we're fine tuning my understanding now. I only have a couple questions
1. >No, don't confuse parameters with return values. int main(void) simply means that main takes no arguments, but it still must return an integer
Im glad this came up, because now we are at a point where what i thought i knew i actually don't
it was my understanding that a function's parameter was the variable (like, i) and the argument was the return value of the variable (like, 10). If int main(void) means main takes no arguments but must still return an integer, wouldn't the integer be a value (i.e. an argument)??? I'M CONFUSED :(
another thing that has me all confused was this
2. >You're overcomplicating things. The outer loop executes the inner loop. So if the inner loop initializes one row, the outer loop initializes all of the rows.
^^^I'm with you about the outer loop executing the inner. I don't know what you mean after that.
Does the outer loop create row? And for every row created by the outer loop does the inner loop then creates 10 col's (because we said COLS is 10)? Also, is 17 the value placed in every address of created by a[row][col]?
Dave Sinkula 2,398 long time no c Team Colleague
Regarding parameters and return values: would an example like this help distinguish the two?
#include <stdio.h>
int lengthof ( const char *parameter )
{
int len = 0;
while ( parameter[len] != '\0' )
{
++len;
}
return len; /* len is the return value */
}
int main ( void )
{
const char text[] = "a text string";
int length = lengthof ( text ); /* text is the argument to lengthof */
printf ( "the string \"%s\" has a length of %d\n", text, length );
return 0;
}
/* my output
the string "a text string" has a length of 13
*/
If you write a function to count the characters in a string (as above), you'd probably pass a pointer the string and return a count of the number of characters.
If write a cube root function, you'd probably pass some value, calculate the cube root, and then return the result.
The parameter(s) is what the function gets to do whatever it is the function does. The return value is whatever the value returns. The relationship between the two depends on what the function does.
Fasola 0 Junior Poster
Regarding parameters and return values: would an example like this help distinguish the two?
#include <stdio.h> int lengthof ( const char *parameter ) { int len = 0; while ( parameter[len] != '\0' ) { ++len; } return len; /* len is the return value */ } int main ( void ) { const char text[] = "a text string"; int length = lengthof ( text ); /* text is the argument to lengthof */ printf ( "the string \"%s\" has a length of %d\n", text, length ); return 0; } /* my output the string "a text string" has a length of 13 */
If you write a function to count the characters in a string (as above), you'd probably pass a pointer the string and return a count of the number of characters.
If write a cube root function, you'd probably pass some value, calculate the cube root, and then return the result.
The parameter(s) is what the function gets to do whatever it is the function does. The return value is whatever the value returns. The relationship between the two depends on what the function does.
Okay Dave help me out!!!!!!!!!!!
#include <iostream>
int myfunction(int parameter1, int parameter2)
{
return parameter1 + parameter2;
}
int main(void)
{
myfunction(argument1, argument2);
int argument1 = 4, argument2 = 5;
return 0;
}
/*"ComeauTest.c", line 10: error: identifier "argument1" is undefined
myfunction(argument1, argument2);
^
"ComeauTest.c", line 10: error: identifier "argument2" is undefined
myfunction(argument1, argument2);
^
"ComeauTest.c", line 12: warning: variable "argument1" was declared but never
referenced
int argument1, argument2;
^
"ComeauTest.c", line 12: warning: variable "argument2" was declared but never
referenced
int argument1, argument2;
*/
Why doesn't this compile, what am I doing wrong?
I can get it down to 2 errors, but I left it up like this for Narue to help understand the difference between declaring and identifying an object.
Stack Overflow 8 Junior Poster
Hello,
I see a problem with the following code:
int main(void)
{
myfunction(argument1, argument2);
int argument1 = 4, argument2 = 5;
return 0;
}
Simple. You create your variables after you try and pass them to myfunction(). Rather, your code should be:
int main(void)
{
int argument1 = 4, argument2 = 5;
myfunction(argument1, argument2);
return 0;
}
- Stack Overflow
Fasola 0 Junior Poster
Hello,
I see a problem with the following code:
int main(void) { myfunction(argument1, argument2); int argument1 = 4, argument2 = 5; return 0; }
Simple. You create your variables after you try and pass them to myfunction(). Rather, your code should be:
int main(void) { int argument1 = 4, argument2 = 5; myfunction(argument1, argument2); return 0; }
- Stack Overflow
Thanks for stepping in Stack!!!!
It makes sense
I really appreciate it
:D
A.Rehman Amjad -1 Newbie Poster
plz explain call by reference also
jonsca commented: Start your own thread -1
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.