|
...define person...
struct person *pPerson;
unsigned int count = 10;
pPerson = malloc( count * sizeof( person));
if( pPersion = NULL) {
printf( "Insufficient memory available\n" );
exit(0);
}
...use pPerson...
free( pPerson);
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
i see where the code is going however could you show me how i shoudl go if i wanted to append to the pointer?
i am thinking if i should create a temp space and reallocated the memory againa dn stores it back in.
how do you think i should do it? and again i would like to say i am doing c programming.
thx
|
|
|
|
|
neodeaths wrote: i am thinking if i should create a temp space and reallocated the memory againa dn stores it back in.
yes, that is the way to go. But there is a function to do that for you:
Use realloc()
To double the size use
pPerson = realloc( pPerson, 2*count);
pPerson would be changed - be sure to not hold any pointers to the old location.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
so reallocate would do the work of allocating the new memory i want to use for me? what about the old one do i need to free the memory myself?
also would the reallocated memory contain my old infomation or do i still have to store it away into a temp and put it back?
|
|
|
|
|
Hey, you definitely need to read a book on C ("Brian W. Kernighan, Dennis Ritchie: The C programming language" comes to mind) and MSDN!
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
i read some tutorial on c programming but i wanted to apply dynamic memory allocation on struct rather then string array as i think it is more useful that way.
i guess that dynamic memory array is eassier to learn and maybe i should not jump stright into learning how to do dynamic memory allocation on struct so soon.
|
|
|
|
|
neodeaths wrote: maybe i should not jump stright into dynamic memory allocation
Maybe.
But in the meantime, just using the information at your fingertips (MS ad in the nineties, shortly after Bill Gates had invented the internet) would suffice:
Look up what MSDN[^] says about realloc[^].
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
thanks for your help and all those who bothered to read my question i think it will really helpme in my progress of learning
|
|
|
|
|
neodeaths wrote: so reallocate would do the work of allocating the new memory i want to use for me?
Yes.
neodeaths wrote: what about the old one do i need to free the memory myself?
No.
neodeaths wrote: also would the reallocated memory contain my old infomation...
Yes.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
i was just wondering this:
if i wanted to find the size of the array which is pointed by the pointer for my struct.
how should i go about it?
as you see i tryed the .lenght and sizeof() how ever it keep returning the size of the pointer instead of the array.
the purpose i am trying to do this is because i figured that if i am gonna manipulate the memory during runtime, finding out the size of the array while Only the array infomation would be useful.
please help with this question thx.
|
|
|
|
|
neodeaths wrote: if i wanted to find the size of the array which is pointed by the pointer for my struct.
how should i go about it?
Keep that number in a global variable of some sort.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
yes i know that but i am just checking if there is a way to find the memory size of the whole array pointer?
if i could i could probably do a array_mem_size/sizeof(struct people)
|
|
|
|
|
neodeaths wrote: if i could i could probably do a array_mem_size/sizeof(struct people)
This would only work for stack-based memory, not heap-based memory allocated with malloc() .
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
by the way i realise something wielrd when i i malloc to make the array have a size of 10.
and i try to access the 15th array by hardcoding the program still works do you know y?
|
|
|
|
|
neodeaths wrote: do you know y?
Without seeing a code snippet, no.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Perhaps you want something like this:-
<br />
person* aPersons = new person[iCount];<br />
delete [] aPersons;<br />
Now you have an array of person structs and can use e.g. aPersons[iIndex].member to access a person in the array.
A more modern approach is to use container classes which dynamically size themselves. There are loads of these in the standard library std::list and std::vector are worth a look.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Read the first line of his message: he is doing C programming, not C++
|
|
|
|
|
neodeaths wrote: could anyone show me how to do a dynamic memory allocation on struct which can free me of having to state the array size?
i read some ebook saying i could use malloc but so far they used malloc on the string instead of struct.
thanks.
i prefer, stl::list/std::vector for this type of effort
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
You fell into the same trap as Matthew Faithful - he's using C, not C++.
Otherwise, I'd totally agree with you.
Iain.
|
|
|
|
|
Iain Clarke wrote:
You fell into the same trap as Matthew Faithful - he's using C, not C++.
he he he! people is just forgetting difference between c and c++
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I developed an application with VC6. I used a lot of resouce in my application, such as images, xml configuration files, lang strings files, and so on. To protect my resoure from being seen or modified by user directly, I need a way to hide or package these resource.
Does anybody have some good idea about this? Thanks in advance.
Peter
|
|
|
|
|
Peter, Chan wrote: developed an application with VC6. I used a lot of resouce in my application, such as images, xml configuration files, lang strings files, and so on. To protect my resoure from being seen or modified by user directly, I need a way to hide or package these resource.
you coukld save that in password protected zip file, when your program start running, unzip on temp location, after completetion of task remove the files or update the zip if require!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Hi Friends,
I just want to write an Outlook Add-on,which automatically reads from a list of Quotes(the quotes are placed in a text file Quotes.txt) and places at the end of my signature randomly. This should happen
each time i compose a mail.
I have gone through several articles in CP,but nothing helped me.
Can somebody help me out in sending some good links about Outlook automation.
Thanks
Appu..
"Never explain yourself to anyone.
Because the person who likes you does n't need it.
And the person who dislikes you won't believe it."
|
|
|
|
|
|
Thanks for the Help Dave.
But I am more interested in writing code for outlook automation.
Can you please help me out,how can i approach.
Thanks again,
Appu..
"Never explain yourself to anyone.
Because the person who likes you does n't need it.
And the person who dislikes you won't believe it."
|
|
|
|