Hello everybody.
I need a C# code for print an image, imported with a openFileDialog Component.
Can you help me?
Thanks
C# I need still help! 10 points for the best answer. C# C# C#?
First, you will need an OpenFileDialog, a PictureBox, and a method of starting the loading process (your choice, most likely a button's Click event).
Assuming the dialog is called openFileDialog and the picture box is called pictureBox, you can call this function (or copy and paste the code inside of it into the event handler.
void LoadImageFromDialog()
{
openFileDialog.Filter = "Image files|*.gif;*.jpg;*.png";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pictureBox.Load(openFileDialog.FileNam...
}
}
Example Form:
Copy and paste this into a new file called ImageForm.cs and just create an instance of it and show it. Clicking on the form will open the file dialog and load the picture after selecting one.
using System;
using System.Windows.Forms;
public class ImageForm : Form
{
OpenFileDialog openFileDialog = new OpenFileDialog();
PictureBox pictureBox = new PictureBox();
public ImageForm()
{
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Size = new System.Drawing.Size(300, 300);
this.pictureBox.Dock = DockStyle.Fill;
this.pictureBox.Click += new EventHandler( pictureBox_Click);
this.ClientSize = new System.Drawing.Size(300, 300);
this.Controls.Add(pictureBox);
this.Text = "Image Form";
}
void pictureBox_Click(object sender, EventArgs e)
{
openFileDialog.Filter = "Image files|*.gif;*.jpg;*.png";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pictureBox.Load( openFileDialog.FileName);
}
}
}
Reply:Look in MSDN for Graphics.DrawImage, or just look at the Graphics class. You
draw an image in really the same way as you print anything.
Reply:Since we're talking windows forms, I think you want to create a form and add an OpenFileDialog to it, and add a PictureBox Control to it.
Add a button call it Load... in the OnClick you'll want to open the Dialog. When the user clicks Ok after selecting a picture. Set the image property of the PictureBox to the file in the Open dialog control.
Instant Gratification.
Monday, May 24, 2010
C progamming /clr?
I want to compile a program that I wrote in Microsoft Visual C++. It is a C program that looks like this:
#include %26lt;stdio.h%26gt;
int main()
{
if ( !remove("useless.txt"){
printf( "Remove Sucsess" );
}
return 0;
}
I just wanted to test the "remove" function that I recently learned.
When I try to complie it an error comes up:
1%26gt;cl : Command line error D8045 : cannot compile C file '.\remove.c' with the /clr option
Now I have no idea what /clr is can anyone help?
Is it jsut the compiler or my program?
Thanks,
C progamming /clr?
Your program is missing library...
set up libraries path...
plum
#include %26lt;stdio.h%26gt;
int main()
{
if ( !remove("useless.txt"){
printf( "Remove Sucsess" );
}
return 0;
}
I just wanted to test the "remove" function that I recently learned.
When I try to complie it an error comes up:
1%26gt;cl : Command line error D8045 : cannot compile C file '.\remove.c' with the /clr option
Now I have no idea what /clr is can anyone help?
Is it jsut the compiler or my program?
Thanks,
C progamming /clr?
Your program is missing library...
set up libraries path...
plum
Friday, May 21, 2010
C-section question.?
okay. i wanted to know some things about the c-section. i'm in the south carolina area also. i was wondering can you get a planned c-section, or will the doctors make you have it natural if you can? .. %26amp;%26amp; if you can get a c-section do you have to get a epidural. or is there other things you can ask for and they give it to you? can you get a anestishia ( however you spell it ), and be put to sleep? .. what are some other things about the c-section, like your experience with it.
C-section question.?
It depends on your doctor but my experience they will only do a c-section if need be. I had one with my daughter b/c she was breech and they did a version to try and flip her by pushing on my belly to try to externally flip her, it didn't work, so when I had my son 2 years later I did a vbac, vaginal birth after cesarian, it was the best thing I could have done. It was such a fast recovery and with my c-section I had a spinal block, a one time shot where they do the epidural and it totally numbs you. If you have an option go for vaginal birth. So much better. Good luck
Reply:No physician should allow a woman to have c-section unless it is medically warranted. It is major surgery, with risks to you and the baby. Generally women have epis or spinal blocks for a c-section. On very, very rare occassions when there is a true emergency, the mother might be "put to sleep" for the duration of the surgery.
I have had vaginal births and a c-section and there is NO way I would elect to have c-section. ONLY is there were some emergency... it's way to risky otherwise.
Reply:Most of the time they will not do a c-section because you ask for it. Yes, most c-sections are done with epidural's. General anesthesia is bad for baby and mom so they only do it for emergency c-section's most of time. It is sometimes worse recovering from c-section than it is a normal vaginal birth. I would say you need to talk to your doctor and be honest with your fears of vaginal delivery. Good luck.. Hope this helps a little..
P.S. in vaginal delivery, the contractions and baby passing through birth canal gets the fluid out of babies lungs... in c-cection, this does not happen so higher risk for aspiration of fluids occurs
C-section question.?
It depends on your doctor but my experience they will only do a c-section if need be. I had one with my daughter b/c she was breech and they did a version to try and flip her by pushing on my belly to try to externally flip her, it didn't work, so when I had my son 2 years later I did a vbac, vaginal birth after cesarian, it was the best thing I could have done. It was such a fast recovery and with my c-section I had a spinal block, a one time shot where they do the epidural and it totally numbs you. If you have an option go for vaginal birth. So much better. Good luck
Reply:No physician should allow a woman to have c-section unless it is medically warranted. It is major surgery, with risks to you and the baby. Generally women have epis or spinal blocks for a c-section. On very, very rare occassions when there is a true emergency, the mother might be "put to sleep" for the duration of the surgery.
I have had vaginal births and a c-section and there is NO way I would elect to have c-section. ONLY is there were some emergency... it's way to risky otherwise.
Reply:Most of the time they will not do a c-section because you ask for it. Yes, most c-sections are done with epidural's. General anesthesia is bad for baby and mom so they only do it for emergency c-section's most of time. It is sometimes worse recovering from c-section than it is a normal vaginal birth. I would say you need to talk to your doctor and be honest with your fears of vaginal delivery. Good luck.. Hope this helps a little..
P.S. in vaginal delivery, the contractions and baby passing through birth canal gets the fluid out of babies lungs... in c-cection, this does not happen so higher risk for aspiration of fluids occurs
C-section mommies I've got a question for you...?
I'd like to get some feedback from mom's who have had scheduled c-sections or have ones planned soon. Does the doctor usually schedule your c-section before you are actually due or on your due date?
I went into labor at 37.6 weeks with my first baby (delivered vaginally) but because of problems and with this pregnancy I have to deliver via c-section. Do you think the doc will schedule it for when I hit 38 weeks? Tell me when you are/were due and when the c-section is/was scheduled so I can have a good idea as to how soon I can expect to have mine.
C-section mommies I've got a question for you...?
this will be my 2nd c-section
1st was emergency (now i cant deliver vaginally long story lol.)
2nd is planned my due date was aug 11th but my planned c-section date is aug 6th.
they do it as close to the due date as possible in the 39th week UNLESS u have to go in early for leakage or contractions/labor.
good luck and congrads:)
Reply:i was due june 12 but had my c-section on the 5th. we knew my daughter was breech early on but even before i hit the 38 week he didn't want to schedule it for then but for week 39. and from my understanding that is generally the preferred time for scheduling. you can ask to be scheduled at 38 weeks but its up to your doctor and whatever hospital policies are.
Reply:They usually schedule them early, which can lead to your baby being premature and having to be in the NICU. Please, please join the ICAN-online Yahoo group and ask the ladies there what they think about your case. The group is full of midwives, caesarean moms and others who are a wealth of knowledge and experience on caesareans, VBACs, and other related topics.
20% of live births in the U.S. are born by UNNECESSARY caesarean. You may not have to go through this surgery. Not knowing all the details of your pregnancy, I can't say for sure, but I highly recommend doing more research and getting other opinions from other providers, including midwives and other obstetricians.
It is better for your baby if you do NOT schedule the caesarean but wait until you go into labor. Any amount of labor is usually good for the baby, and will make it much less likely that the baby will be premature.
Please visit the Yahoo group and tell your story there to get advice, and get other opinions. Depending on your problems with this pregnancy, you will have different options and different choices, not all of which may be told to you by your doctor. Make sure you are as educated as you possibly can be so you feel good about the decisions you make.
The last thing you want is to second-guess yourself when you reseach after the fact. So many caesareans are unnecessary. It would be horrible to put yourself and your baby at unnecessary risk if you are better off waiting or foregoing the surgery altogether.
Good luck.
Reply:My cousin, who is due October 1, has to have a C-section because she has problems with her placenta. Her doctor has her C-section scheduled for September 27. It really depends on the doctor though.
Reply:i had to have a c section with my twins and because of this i have to have a c section with this baby (i am NOT happy about this)
anyway i was told they would do the c section at 38 1/2 weeks. problem is i have never hit a due date yet, they get earlier every time! (three pregnancies) so my doctor said to call if i think i am in early labor and they will do the c section earlier.
Reply:I had my c-section at 42 weeks, my son would not drop into position. I was induced twice with only one contraction occuring because of it. They sent to a bigger hospital to be induced again, but found out my son was over 10 lbs through ultrasound. Because of the risks to my son i decided on a c-section. It was scheduled for that evening, but was delayed twice because of emergency c-sections having to be done on other women. So finally at 11:28pm i gave birth via c-section to my 10 lbs 10.5 oz baby boy.
I had my son on 5/5/95 and was due two weeks prior.
Reply:Im in the uk and my 3rd baby is due on 17th August and i have just been booked in this week for my c section on 9th August, they usually do them at 39 weeks.
I had an emergency c section with my 1st and my 2nd child stopped growing at 33 weeks so had to have another c section, in which they cut my womb 2 times so this time i have no choice but to go 4 a 3rd section.
I might have to have my section date moved though as this baby is growing large but theyre provisonally booked me!
Good luck with the baby
Reply:I had a c-section that wasn't scheduled, but a friend of mine who had her 2nd c-section scheduled had her's about 2 weeks before the actual due date. Can't say your doctor will schedule the exact same but that's what happened with her. Your doc may schedule around his/her own availability and that of preferred staff, too, so keep that in mind.
Best of luck with it!
Reply:I was induced at 38 weeks because I had a blood clot in the 27 week. They wanted the birth to be controled for that reason. I ended up having a c-section because my daughter's head was to big for me. I have no idea what your doctor will do with you, but I thnk most doctors are pretty comfortable with inducing or planning a c-section after the 38 week.
I went into labor at 37.6 weeks with my first baby (delivered vaginally) but because of problems and with this pregnancy I have to deliver via c-section. Do you think the doc will schedule it for when I hit 38 weeks? Tell me when you are/were due and when the c-section is/was scheduled so I can have a good idea as to how soon I can expect to have mine.
C-section mommies I've got a question for you...?
this will be my 2nd c-section
1st was emergency (now i cant deliver vaginally long story lol.)
2nd is planned my due date was aug 11th but my planned c-section date is aug 6th.
they do it as close to the due date as possible in the 39th week UNLESS u have to go in early for leakage or contractions/labor.
good luck and congrads:)
Reply:i was due june 12 but had my c-section on the 5th. we knew my daughter was breech early on but even before i hit the 38 week he didn't want to schedule it for then but for week 39. and from my understanding that is generally the preferred time for scheduling. you can ask to be scheduled at 38 weeks but its up to your doctor and whatever hospital policies are.
Reply:They usually schedule them early, which can lead to your baby being premature and having to be in the NICU. Please, please join the ICAN-online Yahoo group and ask the ladies there what they think about your case. The group is full of midwives, caesarean moms and others who are a wealth of knowledge and experience on caesareans, VBACs, and other related topics.
20% of live births in the U.S. are born by UNNECESSARY caesarean. You may not have to go through this surgery. Not knowing all the details of your pregnancy, I can't say for sure, but I highly recommend doing more research and getting other opinions from other providers, including midwives and other obstetricians.
It is better for your baby if you do NOT schedule the caesarean but wait until you go into labor. Any amount of labor is usually good for the baby, and will make it much less likely that the baby will be premature.
Please visit the Yahoo group and tell your story there to get advice, and get other opinions. Depending on your problems with this pregnancy, you will have different options and different choices, not all of which may be told to you by your doctor. Make sure you are as educated as you possibly can be so you feel good about the decisions you make.
The last thing you want is to second-guess yourself when you reseach after the fact. So many caesareans are unnecessary. It would be horrible to put yourself and your baby at unnecessary risk if you are better off waiting or foregoing the surgery altogether.
Good luck.
Reply:My cousin, who is due October 1, has to have a C-section because she has problems with her placenta. Her doctor has her C-section scheduled for September 27. It really depends on the doctor though.
Reply:i had to have a c section with my twins and because of this i have to have a c section with this baby (i am NOT happy about this)
anyway i was told they would do the c section at 38 1/2 weeks. problem is i have never hit a due date yet, they get earlier every time! (three pregnancies) so my doctor said to call if i think i am in early labor and they will do the c section earlier.
Reply:I had my c-section at 42 weeks, my son would not drop into position. I was induced twice with only one contraction occuring because of it. They sent to a bigger hospital to be induced again, but found out my son was over 10 lbs through ultrasound. Because of the risks to my son i decided on a c-section. It was scheduled for that evening, but was delayed twice because of emergency c-sections having to be done on other women. So finally at 11:28pm i gave birth via c-section to my 10 lbs 10.5 oz baby boy.
I had my son on 5/5/95 and was due two weeks prior.
Reply:Im in the uk and my 3rd baby is due on 17th August and i have just been booked in this week for my c section on 9th August, they usually do them at 39 weeks.
I had an emergency c section with my 1st and my 2nd child stopped growing at 33 weeks so had to have another c section, in which they cut my womb 2 times so this time i have no choice but to go 4 a 3rd section.
I might have to have my section date moved though as this baby is growing large but theyre provisonally booked me!
Good luck with the baby
Reply:I had a c-section that wasn't scheduled, but a friend of mine who had her 2nd c-section scheduled had her's about 2 weeks before the actual due date. Can't say your doctor will schedule the exact same but that's what happened with her. Your doc may schedule around his/her own availability and that of preferred staff, too, so keep that in mind.
Best of luck with it!
Reply:I was induced at 38 weeks because I had a blood clot in the 27 week. They wanted the birth to be controled for that reason. I ended up having a c-section because my daughter's head was to big for me. I have no idea what your doctor will do with you, but I thnk most doctors are pretty comfortable with inducing or planning a c-section after the 38 week.
Is Visual C++ any different from C++ in general?
After looking at a C++ tutorial on the net, and Visual studio C++ 2008's tutorials, it appears that the two tutorials have two quite different ways of saying "Hello World". Is Visual C++ in the visual studio something different from the C++ programming language?
Is Visual C++ any different from C++ in general?
Visual C++ is an IDE/compiler tailored for creating Windows applications with the C++ programming language. It comes with many libraries for interacting with Windows, and programming with it generally favors using these libraries heavily. C++ is a general purpose programming language for creating apps of any kind. The basic syntax of the two are the same though.
Reply:Visual C++ is Microsoft's C++ IDE/compiler for C++ on Windows. Visual C++ is not a different language. There is something called ANSI C++, which a standard for the C++ language. Visual C++ is ANSI C++ compliant, so any C++ examples you find, will work in Visual Studio. The VC++ tutorials may be using precompiled headers, so the includes may look different. The IDE is designed for the professional programmer, so it tries to make some tasks easier. If you want to compile an example the best way is to do a Win32 console application and click Empty Project and uncheck "Use precompiled headers"
Reply:C++ hello world uses the console interface.
No graphics, just basic text.
so
cout %26lt;%26lt; "Hello World" %26lt;%26lt; endl;
will be output on the console window.
Visual C++ adds some bells and whistle to C++ so it supposedly makes it better and 'nicer'.
Reply:i like to know also.
Is Visual C++ any different from C++ in general?
Visual C++ is an IDE/compiler tailored for creating Windows applications with the C++ programming language. It comes with many libraries for interacting with Windows, and programming with it generally favors using these libraries heavily. C++ is a general purpose programming language for creating apps of any kind. The basic syntax of the two are the same though.
Reply:Visual C++ is Microsoft's C++ IDE/compiler for C++ on Windows. Visual C++ is not a different language. There is something called ANSI C++, which a standard for the C++ language. Visual C++ is ANSI C++ compliant, so any C++ examples you find, will work in Visual Studio. The VC++ tutorials may be using precompiled headers, so the includes may look different. The IDE is designed for the professional programmer, so it tries to make some tasks easier. If you want to compile an example the best way is to do a Win32 console application and click Empty Project and uncheck "Use precompiled headers"
Reply:C++ hello world uses the console interface.
No graphics, just basic text.
so
cout %26lt;%26lt; "Hello World" %26lt;%26lt; endl;
will be output on the console window.
Visual C++ adds some bells and whistle to C++ so it supposedly makes it better and 'nicer'.
Reply:i like to know also.
C++ try catch question?
hello i hardly ever do c++ any more but i answerd a question earlia and been mucking around since then
i got a problem im use to c# and to handle errors its simple
try{whatever and if theres a error it will automaticly go
}
catch{over here}
however c++ not quite same i seem to have to decalare somthing in the catch and use a throw for some reason my point is i got this code (simplified for example)
try{
int x = Convert::ToInt32(this-%26gt;textBox1-%26gt;Text);
int y=Convert::ToInt32(this-%26gt;textBox2-%26gt;Text)...
System::Windows::Forms::MessageBox::Show... =s " + Convert::ToString(x+y),"mkb answer",MessageBoxButtons::OK,MessageBox...
}
catch{}
how can i make it so if theres a error such as some entering a a at the end of a int so theres a add error it just automaticly states in a message box or label error sort thing i know the code for catch like System::windows::forms::messagebox etc just dont seem to work the way im use to doing it in c#
C++ try catch question?
use throw.
assign a character variable.
put input from the textbox into the variable.
check if its number or character
then use throw if character else preceed with that.
for eg.
throw (a)
where a is the character
catch (char a)
parts of a flower
i got a problem im use to c# and to handle errors its simple
try{whatever and if theres a error it will automaticly go
}
catch{over here}
however c++ not quite same i seem to have to decalare somthing in the catch and use a throw for some reason my point is i got this code (simplified for example)
try{
int x = Convert::ToInt32(this-%26gt;textBox1-%26gt;Text);
int y=Convert::ToInt32(this-%26gt;textBox2-%26gt;Text)...
System::Windows::Forms::MessageBox::Show... =s " + Convert::ToString(x+y),"mkb answer",MessageBoxButtons::OK,MessageBox...
}
catch{}
how can i make it so if theres a error such as some entering a a at the end of a int so theres a add error it just automaticly states in a message box or label error sort thing i know the code for catch like System::windows::forms::messagebox etc just dont seem to work the way im use to doing it in c#
C++ try catch question?
use throw.
assign a character variable.
put input from the textbox into the variable.
check if its number or character
then use throw if character else preceed with that.
for eg.
throw (a)
where a is the character
catch (char a)
parts of a flower
C code question regarding malloc() and free().....?
file ----%26gt;input.h (contents of input.h)
float * datain();
file ----%26gt;input.c(contents of input.c)
#include %26lt;malloc.h%26gt;
//.....other includes
float *datain()
{
float *data;
//routine to get data........
return(data);
free (data) ///????????
}
assuming I have a main.c function in the same project which calls datain()...is it legal operation to free the pointer after returning the pointer to the main function? even though I need to do some thing to the returned data?
file----------%26gt;main.c
#include %26lt;malloc.h%26gt; ....//other includes
#include "input.h"
void main ()
{
float *getdata;
getdata = datain();
// .....do something to get data;
}
Thanks in advance
C code question regarding malloc() and free().....?
statements after return statement are not executed.
so basically, your function does not execute the line
%26gt; free(data)
instead of that, you could define a structure and two functions,
typedef struct {
float * data;
int valid;
} datastruct;
datastruct get_data(){
float *tmp;
// malloc(tmp)
// copy data to memory pointed by tmp
datastruct result = {tmp, 1};
}
float free_data(datastruct *pdata){
if (pdata -%26gt; valid){
pdata -%26gt; valid = 0;
free (pdata -%26gt; data);
}
}
==================
and inside your main function:
void main(){
datastruct data;
data = get_data();
// do smth to data
free_data(%26amp;data);
}
Reply:you're right, the struct is not really needed. Report It
Reply:Actually in C different memory space will be alloted for the functions so whenever u pass a data the value will be passed but when it is a pointer it only passes the address of the pointer so if u pass a pointer from one function to another the address of the pointer will be passed.
If u want to free a pointer the u need to pass the data value instead of using address of the pointer
ie instead of return(data); use return(*data); It passes the data value so when u free the location u dont have any problem
If u free the pointer only the pointer will be freed ie if u free data then it will be deleted (the location having the address of original data )as u have already passed the address if u free there is no problem .if u free first the original address of data will be lost thus the data becomes inaccessible .
I think i have atleast solved some of your problems
Reply:I really didn't use C before ... but i know the pointers concept.
I think there's no problem if u make the pointer "data" free after returning the data
even if u need to do things to the returned data in the main.
----
*u've pointer "data" in dataIn() and pointer "getData" in the main,
*what is really done here is that u recieved some DATA
*the DATA now is in the memory with address "150" for example
*u put the address "150" in the pointer "data"
*u returned the pointer value to the pointer "getData"
*then "getData" now contains the address "150"
i don't know the function "free" but,
when u free(data):
-if that means u make the pointer itself free and not points any DATA, so u still have the pointer "getData" points to the same data.
-if it means u'll delete the actual DATA pointed by the pointer "data", then that's wrong
but it seems like the 1st meaning is right
----------------
another thing :
as i said b4, i didn't use C before ... but in Java, the statements after "return" aren't reachable as the function ends at the word "return", i don't know if it's the same thing in C or not
Reply:[747] cmalloc: cat main1.c
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
float *dataIn( void )
{
float *dataPtr = malloc( sizeof(float) );
*dataPtr = 1.2345;
return dataPtr;
}
int main( int argc, char* argv[] )
{
float *myDataPtr = dataIn();
printf( "myDataPtr: %p\n", myDataPtr );
printf( "*myDataPtr: %f\n", *myDataPtr );
free( myDataPtr );
return 0;
}
[748] cmalloc: gcc -Wall main1.c
[749] cmalloc:
[749] cmalloc: ./a.out
myDataPtr: 0x3000f0
*myDataPtr: 1.234500
The dataIn() function allocates the memory, fills it in, and returns a pointer to the data. The main() function gets a pointer to the data from the dataIn() function. The ownership of the allocated memory passes from the dataIn() function to main(). The main() does whatever it wants with the data and then frees the memory.
You must NOT free the memory until all the program is completely done using the data stored within the memory.
float * datain();
file ----%26gt;input.c(contents of input.c)
#include %26lt;malloc.h%26gt;
//.....other includes
float *datain()
{
float *data;
//routine to get data........
return(data);
free (data) ///????????
}
assuming I have a main.c function in the same project which calls datain()...is it legal operation to free the pointer after returning the pointer to the main function? even though I need to do some thing to the returned data?
file----------%26gt;main.c
#include %26lt;malloc.h%26gt; ....//other includes
#include "input.h"
void main ()
{
float *getdata;
getdata = datain();
// .....do something to get data;
}
Thanks in advance
C code question regarding malloc() and free().....?
statements after return statement are not executed.
so basically, your function does not execute the line
%26gt; free(data)
instead of that, you could define a structure and two functions,
typedef struct {
float * data;
int valid;
} datastruct;
datastruct get_data(){
float *tmp;
// malloc(tmp)
// copy data to memory pointed by tmp
datastruct result = {tmp, 1};
}
float free_data(datastruct *pdata){
if (pdata -%26gt; valid){
pdata -%26gt; valid = 0;
free (pdata -%26gt; data);
}
}
==================
and inside your main function:
void main(){
datastruct data;
data = get_data();
// do smth to data
free_data(%26amp;data);
}
Reply:you're right, the struct is not really needed. Report It
Reply:Actually in C different memory space will be alloted for the functions so whenever u pass a data the value will be passed but when it is a pointer it only passes the address of the pointer so if u pass a pointer from one function to another the address of the pointer will be passed.
If u want to free a pointer the u need to pass the data value instead of using address of the pointer
ie instead of return(data); use return(*data); It passes the data value so when u free the location u dont have any problem
If u free the pointer only the pointer will be freed ie if u free data then it will be deleted (the location having the address of original data )as u have already passed the address if u free there is no problem .if u free first the original address of data will be lost thus the data becomes inaccessible .
I think i have atleast solved some of your problems
Reply:I really didn't use C before ... but i know the pointers concept.
I think there's no problem if u make the pointer "data" free after returning the data
even if u need to do things to the returned data in the main.
----
*u've pointer "data" in dataIn() and pointer "getData" in the main,
*what is really done here is that u recieved some DATA
*the DATA now is in the memory with address "150" for example
*u put the address "150" in the pointer "data"
*u returned the pointer value to the pointer "getData"
*then "getData" now contains the address "150"
i don't know the function "free" but,
when u free(data):
-if that means u make the pointer itself free and not points any DATA, so u still have the pointer "getData" points to the same data.
-if it means u'll delete the actual DATA pointed by the pointer "data", then that's wrong
but it seems like the 1st meaning is right
----------------
another thing :
as i said b4, i didn't use C before ... but in Java, the statements after "return" aren't reachable as the function ends at the word "return", i don't know if it's the same thing in C or not
Reply:[747] cmalloc: cat main1.c
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
float *dataIn( void )
{
float *dataPtr = malloc( sizeof(float) );
*dataPtr = 1.2345;
return dataPtr;
}
int main( int argc, char* argv[] )
{
float *myDataPtr = dataIn();
printf( "myDataPtr: %p\n", myDataPtr );
printf( "*myDataPtr: %f\n", *myDataPtr );
free( myDataPtr );
return 0;
}
[748] cmalloc: gcc -Wall main1.c
[749] cmalloc:
[749] cmalloc: ./a.out
myDataPtr: 0x3000f0
*myDataPtr: 1.234500
The dataIn() function allocates the memory, fills it in, and returns a pointer to the data. The main() function gets a pointer to the data from the dataIn() function. The ownership of the allocated memory passes from the dataIn() function to main(). The main() does whatever it wants with the data and then frees the memory.
You must NOT free the memory until all the program is completely done using the data stored within the memory.
Subscribe to:
Posts (Atom)