|
About
My Infrequently Updated Blog. The web-based journal of M. Forde, computer nerd, endurance athlete, and DeLorean owner
contact
Subscribe
Subscribe to a syndicated feed of my weblog,
brought to you by the wonders of RSS.
Flavors
There's more than one way to view this weblog; try these flavors on
for size.
index
circa 1993
Sections
main
musings
running
DeLorean
code
unix
album
TBM
Archives
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011
2010
2009
2008
2007
Disclaimers, Copyrights, Privacy, Etc.
ToS
Copyrights
Links
olix0r.net
netmeister.org
Giraffes
Eat. Run. Sleep.
|
|
|
Just a Question
Given the following code fragment, I was asked to implement foo such
that the program would output "America." How would you do it?
int main()
{
char *p = "Hello";
foo( );
printf("%s",p);
return 0;
}
My solution involved allocating new memory from the heap to store the
new string, and changing p to point to that buffer. They didn't like
that answer. They preferred the method of putting the new string in the
data segment as well.
I personally would always avoid that, whenever possible. "Hello" is
stored in a read-only area of memory as is "America" in their preferred
solution. Any attempt to alter those strings will trigger a segfault.
This is an accident waiting to happen.
[/code]
[permanent link]
|
|