C - address of a pointer
-
typedef struct
{
int i;
} MyObj;Working code:
void foo(MyObj* pObj)
{
/* pObj points to a valid struct */
doSomething(&pObj);
pObj->i = ...
}Code that caused random crash:
void foo(MyObj* pObj)
{
MyObj* p = pObj;
doSomething(&p);
pObj->i = ...
}Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
} -
typedef struct
{
int i;
} MyObj;Working code:
void foo(MyObj* pObj)
{
/* pObj points to a valid struct */
doSomething(&pObj);
pObj->i = ...
}Code that caused random crash:
void foo(MyObj* pObj)
{
MyObj* p = pObj;
doSomething(&p);
pObj->i = ...
}Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
}Well, it isn't such subtle. ;)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Well, it isn't such subtle. ;)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]It would be subtle if doSomething function were 200 lines instead of 2. :)
-
typedef struct
{
int i;
} MyObj;Working code:
void foo(MyObj* pObj)
{
/* pObj points to a valid struct */
doSomething(&pObj);
pObj->i = ...
}Code that caused random crash:
void foo(MyObj* pObj)
{
MyObj* p = pObj;
doSomething(&p);
pObj->i = ...
}Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
}Leisuresuit Larry wrote:
Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
}As subtle as a 10 lb. sledge hammer. ;P
-
It would be subtle if doSomething function were 200 lines instead of 2. :)
Tell Mladen [^]. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
typedef struct
{
int i;
} MyObj;Working code:
void foo(MyObj* pObj)
{
/* pObj points to a valid struct */
doSomething(&pObj);
pObj->i = ...
}Code that caused random crash:
void foo(MyObj* pObj)
{
MyObj* p = pObj;
doSomething(&p);
pObj->i = ...
}Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
}Would not really consider that subtle at all.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
Leisuresuit Larry wrote:
Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
}As subtle as a 10 lb. sledge hammer. ;P
:laugh:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
typedef struct
{
int i;
} MyObj;Working code:
void foo(MyObj* pObj)
{
/* pObj points to a valid struct */
doSomething(&pObj);
pObj->i = ...
}Code that caused random crash:
void foo(MyObj* pObj)
{
MyObj* p = pObj;
doSomething(&p);
pObj->i = ...
}Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
}Its not subtle, but he learned... so its a good post
-
Leisuresuit Larry wrote:
Reason:
void doSomething(MyObj** p)
{
free(*p);
*p = createNewObj();
}As subtle as a 10 lb. sledge hammer. ;P
Mladen Jankovic wrote:
10 lb. sledge hammer
That's not a very manly sledge hammer now, is it? ;)
-- Kein Mitleid Für Die Mehrheit
-
Mladen Jankovic wrote:
10 lb. sledge hammer
That's not a very manly sledge hammer now, is it? ;)
-- Kein Mitleid Für Die Mehrheit
If it is about to hit you, it doesn't matter :)
-
Mladen Jankovic wrote:
10 lb. sledge hammer
That's not a very manly sledge hammer now, is it? ;)
-- Kein Mitleid Für Die Mehrheit
No, it's not. You need a minimum 16 pound sledge hammer to get anything done. Had an old back patio, tried a 8 pound one, it just bounced off the concrete, but the 16 pounder, was a more productive story :rolleyes:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
No, it's not. You need a minimum 16 pound sledge hammer to get anything done. Had an old back patio, tried a 8 pound one, it just bounced off the concrete, but the 16 pounder, was a more productive story :rolleyes:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
Paul Conrad wrote:
No, it's not. You need a minimum 16 pound sledge hammer to get anything done.
Depends what you're trying to do. Even a baby sledge will bust up thinner cast aluminum; when combined with a chisel up to at least a half inch thick. Probably more but I haven't had occasion to try since my early teens.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
Paul Conrad wrote:
No, it's not. You need a minimum 16 pound sledge hammer to get anything done.
Depends what you're trying to do. Even a baby sledge will bust up thinner cast aluminum; when combined with a chisel up to at least a half inch thick. Probably more but I haven't had occasion to try since my early teens.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
True, but using larger sledgehammers have more potential for more man points :-D
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
No, it's not. You need a minimum 16 pound sledge hammer to get anything done. Had an old back patio, tried a 8 pound one, it just bounced off the concrete, but the 16 pounder, was a more productive story :rolleyes:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
Paul Conrad wrote:
tried a 8 pound one, it just bounced off the concrete
Amateurs! :)