No, you got me wrong. I am not deciding which is the better memory to use by this single example.
My requirement was something like following:
I need an array which will keep record of some info.
The array length is decided before the program executes, so naturally the choice will go for static array on data segment.
At the same time, the array length has to be configurable by user before the program starts, so naturally...
User Profile
Collapse
-
Hi Banfa,
This was the exact point i was trying to make.
In my particular requirement, I have the ability to use the data segment directly. If I use heap segment with global pointer, then as your assembly suggests, it will create one extra indirection.
Thus the heap access will be slower compare to the direct data segment access.
get1() example is naturally same for all segments, because we deal with a simple pointer....Leave a comment:
-
@Banfa
Please note that here the comparison is not happening between stack and heap. But the data segment and the heap.
For this case, assume that we are not accounting the original heap allocation.
'p' in the 1st case is at data segment; thus its address would be known by compiler.
'p' in the 2nd case at heap segment is also known; but it's pointing to some location.
I was just wondering at assembly level, is it possible...Leave a comment:
-
Array Performance: fix-size vs dynamic
In following codes, both the places 'p' is a global variable. In 1st case it's an array and 2nd case it's a pointer to an array. Do we get any performance benefit, while accessing 'p[ i ]' in the fixed size case ?
Fixed size array:
============
int p[1000];
void main ( )
{
for(int i = 0; i < 1000; i++)
p[ i ] = i;
}
Dynamic array:
===========
int *p =... -
I don't see any problem in your code.
What do you mean by "if i repeat this code several times, i get error". Please paste full code and what are you trying to do?Leave a comment:
-
Assigning data member while object initialization
(1) Is following code at Line-1 an undefined behavior for different compilers ? (it works fine with linux g++ compiler)
(2) If you comment the operator = (), and try to assign the object with a pointer (Line-3), still the code gets compiled, Why ? (actually it's instantiates the constructor !)
(3) In continuation, Why does the pn.size get garbaged when assigned with a pointer ?
...Code:template<class TYPE> struct
-
Thanks Banfa.
Actually later, I had used the similar technique only. It seems that, by language parsing rules, we can't use anything other than pointers.
My code of struct A and struct B were in 2 different header files.
Later on I moved the definitions to the .cpp file to solve it.Leave a comment:
-
Forward declaration usage/problem
Pointer types can be used without error by fwd declarations.
In following code,.
Is there any way to use A::print() inside B::B() ?
=============== =============== =============
...Code:struct A; struct B { B(A *pA) { cout<<"pA = "<<pA<<endl; cout<<"pA->print() = "<<pA->print()<<endl; [B]// throwing error[/B] } }; struct A { -
Thanks for your replies to both.
Is there any logical need to reconstruct the object. vector<> class is optimized, then why such multiple constructors are required ? Any idea.
Because, if you don't have copy constructor or assignment operator, it might be hazardous like following:
=============== =============== =============== ===
struct Test{
int *ptr;
Test(int* const p) : ptr(p) { }
~Test( ) {...Leave a comment:
-
Multiple destructors called in relation of vector<> structure
I am not able to understand why the 2nd ~Test() is called with different 'this' but the same string value ??
Also why does it give error, if I want to declare Test::Str as a const string member ??
...Code:======================================================= struct Test{ string Str; [B]// Gives error if, you declare const string ![/B] Test(const string s) : Str(s) { cout<<Str<<" Test() "<<this<<endl; -
vector<>::clear() problem with const data member
In one of my code, I was using vector<> for certain class. In one of my struct, I have 'const' member data. However, vector<>::clear () throws compile error with that:
=============== =============== =============
struct Test
{
const string Str;
Test(const string str) : Str(str)
{}
};
struct TestWrapper
{
vector<Test> vObj;
~TestWrapper()... -
Thanks Donbock. Can you also clarify my third question that, how does a system know if to cause a "seg fault" when prohibited memory is accessed ?
for example, following code caused seg. fault:
int *p = 0x<CODE_SEGMENT >;
int i = *p; // seg. fault
Will system always check if the 'p' is between the protected areas of code segment. And if this is the case, aren't we compromising the execution...Leave a comment:
-
Segmentation fault
-> Is there any standard for when a sementation fault can occur; I mean should it occur if we access 0, or code segment or anything else ?
-> Why a system crashes, when we are trying to just "read" a memory area which is protected ?
(I can understand that system should crash if we try to "write" something.)
-> How the system knows that, we are accessing a prohibited area; will it check... -
Changing a pointer type to my defined type
Is there any way to change a pointer type to our user defined type.
For example,
In my code, I have something like this:
=============== =============== ===
int i = 0;
int *p;
int **pp;
=============== =============== ===
I want it to be,
=============== =============== ===
int i = 0;
MyType<int> p;
MyType<MyType<i nt> > pp;
=============== =============== ===...
No activity results to display
Show More
Leave a comment: