|
Hi,
I am working using c++ STL and want to know how memory is allocated for vector when we d0 push_back() and other operations. Is memory allocated sequentially or it maintains addresses with it. Also I want to know that I have an vector of structures which is define globally and the struct contains an int and a CString. In my main(), I make structure only with int variable and empty CString and push_back them in vector. Now in another function I assign some local CString to vector elements. Will it work properly?
Please find the below sample code snippet:
struct Data
{
int n;
CString str;
};
vector<data> FData;
main()
{
struct Data tmpData;
for ( ingt i=0; i<10; i++ )
{
tmpData.n = i;
FData.push_back(tmpData);
}
Foo();
}
void Foo()
{
vector<data>::iterator FD_Iter;
for ( FD_Iter = FData.begin( ) ; FD_Iter != FData.end( ) ; FD_Iter++ )
{
CString local = "Akash";
FD_Iter->str = local;
}
}</data></data>
Please explain if after execution of Foo(), Shall I have properly filled FData?
Thanks,
Akash
|
|
|
|
|
Akash Agrawal wrote: want to know how memory is allocated for vector when we d0 push_back()
IMO, vector uses heap to keep it's elements.
Akash Agrawal wrote: Will it work properly?
What happened when you tried?
|
|
|
|
|
1) Heap is OK, but I waana know, that for each push_back(), does it add memory sequentially or relocate it for added size.
2) Working is not guaranteed in my opinion. I am seeing some indefinite behavior especially when use this in multi threaded case.
|
|
|
|
|
Akash Agrawal wrote: I am seeing some indefinite behavior especially when use this in multi threaded case.
I can't see where you have used synchronization mechanisms in your code.
|
|
|
|
|
All STL containers do a new and delete for extending memory.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Akash Agrawal wrote: how memory is allocated for vector when we d0 push_back() and other operations. Is memory allocated sequentially or it maintains addresses with it
A vector maintains a pointer to a single area of memory, which contains space for vector.capacity() items. When you push_back, if vector.size() is less than vector.capacity() , the new element just gets added after the last active item in the memory area. If vector.size() is equal to vector.capacity() , then a new area of memory, bigger than vector.capacity() , is allocated. The existing elements are copied to the start of that area and the old block of memory deallocated.
After the TR1 release of the STL specification, the area of memory managed by a vector is guaranteed to be contiguous. Previously, that was not guaranteed, although most implementations did use an algorithm that caused the memory to be contiguous.
Akash Agrawal wrote: In my main(), I make structure only with int variable and empty CString and push_back them in vector. Now in another function I assign some local CString to vector elements. Will it work properly?
Yes. A vector copies your data into its storage, rather than storing a reference.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks Stuart,
Nice explanation
|
|
|
|
|
Hello Friends
I m using remove(filename);but how i can delete if it using somewhere in project means is there any way that if i free that file and do this remove command.Or is there any other method to delete the file?
Thanks & Regards
Yogesh
|
|
|
|
|
How about MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT?
|
|
|
|
|
I used this but it is not working bro.Let me explain u in brief.In my application i m creating one file and at some conditions i hv to delete that file and that file is deleting only for the first time means after creation if i dont close the application. If i close the app then reopen then i perform operation of deletion on that file it is not deleting.Thats why i want to know is any way to get then free before deletion?Or any other reason?
Thanks
|
|
|
|
|
Try _unlink
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
_unlink is also not working dear.Please suggest me some other solution.
|
|
|
|
|
|
yogeshs wrote: I used this but it is not working bro.
And did you call GetLastError() to find out why?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
When u create/open the file, try to specify the FILE_SHARE_DELETE parameter.
|
|
|
|
|
yogeshs wrote: but how i can delete if it using somewhere in project...
Define using?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I assume "using" means that it's "open"ed and you still want to delete it nonetheless.
You could try "ShFileOperation".
This signature was proudly tested on animals.
|
|
|
|
|
Hello everyone,
It's a really straightforward question this time: I have a process handle and I wish to detect if that handle is one to my app. I have a function where I receive a handle to a process, and if the handle is a handle to any other app but mine, I send a message to it.
I've tried using GetProcessImageFileName, but unfortunately it returns garbage data...
char processName[255];
GetProcessImageFileName(ProcessHandle,processName,256);
MessageBox(NULL,processName,"hehehaha",MB_ICONINFORMATION);
Which leads to another non-related question. You see, I'm trying to hook NtTerminateProcess and deny all requests to terminate my own process. The hook works fine, but when I call the real NtTerminateProcess from inside the hookproc the process it's called on crashes. I am completely at a loss of why this is happening and would appreciate it if someone explained it to me.
When it crashes the error is The instruction at "0x10002383" referenced memory at "0x0000000c". The memory could not be "read" . It's always the same two addresses for any process.
Thanks a lot.
P.S. Please, please, PLEASE do not reply with a message asking "Why would you want to do such a thing". This is for educational purposes only, and I mean that.
modified on Friday, March 20, 2009 1:51 AM
|
|
|
|
|
Check the return value of the function to see if returns an error.
The call GetLastError to get the error value.
Its possible that the process handle does not have the PROCESS_QUERY_INFORMATION access right.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Return value is 0 for the GetProcessImageFileName function. Even though it returns garbage data.
|
|
|
|
|
Are you using a UNICODE build?
If so declare the array as TCHAR instead of char .
Or you can try the GetModuleBaseName function.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Not UNICODE .
I'm going to give GetModuleBaseName a try, but according to msdn:
MSDN:
To retrieve the base name of the main executable module for a remote process, use the GetProcessImageFileName or QueryFullProcessImageName function to retrieve the module name and then use the strrchr function as described in the previous paragraph. This is more efficient and more reliable than calling GetModuleBaseName with a NULL module handle.
EDIT/update: GetModuleBaseName also returns garbage...
GetModuleBaseName(ProcessHandle,NULL,processName,1024);
MessageBox(NULL,processName,"test",MB_ICONINFORMATION);
modified on Friday, March 20, 2009 12:02 PM
|
|
|
|
|
You say that the error code is success and still you're getting garbage.
This is highly unlikely.
I believe you're not doing any error checking.
So its probably gone wrong somewhere else.
GetModuleBaseName for instance returns the length of the string copied.
What is this value?
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Oh, I'm sorry. I thought GetModuleBaseName returns an error code instead of the length of the string copied. I assumed the 0 it returned meant success
That explains the garbage data.
EDIT: I checked GetLastError , it's returning an invalid handle error.
Might this be because I'm getting the handle from NtTerminateProcess?
modified on Saturday, March 21, 2009 2:37 PM
|
|
|
|
|
hxhl95 wrote: I have a process handle and I wish to detect if that handle is one to my app.
Call GetProcessId() , and then call EnumWindows() to find the matching process id.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|