473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

to malloc or not to malloc??

I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}
it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp?

The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.
Mar 30 '06 #1
4 1169
Le 30-03-2006, Johs32 <df***@dsf.com> a écrit*:
I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}
it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp?

The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.


But, when you print it, neither ip nor temp longer exist.
The /value/ of temp (333) have been copied into the object
pointed by ip, which was a. Then, the value 333 have been
copied into a. Then, it prints 333.

Marc Boyer

Mar 30 '06 #2
Johs32 wrote:
I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}
it prints the right result but I have learned not to trust this! My question Who thaught you that ? is can I always be sure that the content of ip = temp? Yes.
The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.


You have copied 'temp' to what int *ip points to, which is 'a' in the
main function. This is perfectly ok and a very useful concept.

Mar 30 '06 #3
Johs32 wrote:
I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}
it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp?
Yes, you are copying temp to the variable pointed to by ip. Once you've
done this, you can do what you like with temp.
The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.


This would be a problem if you had:

int* calc()
{
int temp = 333;
return &temp;
}

--
Ian Collins.
by
Mar 30 '06 #4

Johs32 wrote:
I have this code:
#include <stdio.h> void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}
it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp? Yes.
The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.

The result is not by chance. The content of ip is a copy of temp's
value, although temp
have no longer been there when the function calc returned, *ip have
gotten temp's value
which doesn't go away with calc.

Mar 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.