• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 13:18
CEST 19:18
KST 02:18
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Serral wins HomeStory Cup 2914Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12
Community News
SC4ALL II announced - $10,000 prize pool, Dec 5-63PIG STY FESTIVAL 8.0! (13 - 23 August)8Neeb returns to progaming; rejoins ONSYDE15Weekly Cups (July 20-26): Early returns on 5.0.16b8IntoTheTV X SOOP SC2 League : Weekly & Monthly5
StarCraft 2
General
Life's Huge Gambling Spree After IEM Katowice 2014 Neeb returns to progaming; rejoins ONSYDE SC4ALL II: StarCraft 2 Player Announcement 1/8 Balance hotfix patch 5.0.16b (July 16) Clem: "I don't have that much hope in Blizzard"
Tourneys
Sparkling Tuna Cup - Weekly Open Tournament SC4ALL II announced - $10,000 prize pool, Dec 5-6 PIG STY FESTIVAL 8.0! (13 - 23 August) IntoTheTV X SOOP SC2 League : Weekly & Monthly Crank Gathers Season 4: BW vs SC2 Team League
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
Mutation # 536 Railroad Switch The PondCast: SC2 News & Results Mutation # 535 Assembly of Vengeance Mutation # 534 Burning Evacuation
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ ASL22 General Discussion Making an Online Broodwar Manager Game BW General Discussion BW Drama: Terror's Debt Incident + C9 disbanding
Tourneys
2v2v2v2 Tournament Escore Tournament - Season 3 [Megathread] Daily Proleagues BSL LAN Party - Kraków 29-30 August - OPEN SIGNUPS
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates Odyssey Mineral Stack Saturation PvT advise for noobs
Other Games
General Games
Stormgate/Frost Giant Megathread Beyond All Reason Nintendo Switch Thread Path of Exile ZeroSpace Early Access is Now Live!
Dota 2
Looking for a Dota Mentor Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish TSM pausing esports and CLG Dead
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Community Thread TL Mafia Power Rank NeO.D_StephenKing vs This Guy From 1 Million Dance
Community
General
Artificial Intelligence Thread US Politics Mega-thread European Politico-economics QA Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine
Fan Clubs
The Scarlett Fan Club The IdrA Fan Club The HerO Fan Club!
Media & Entertainment
Movie Discussion! Anime Discussion Thread Series you have seen recently... [Req][Books] Good Fantasy/SciFi books
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion MLB/Baseball 2023 McBoner: A hockey love story
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Simple Questions Simple Answers FPS when play League Of Legend on laptop
TL Community
Northern Ireland Global Starcraft The Automated Ban List
Blogs
What is a Gamer?
TrAiDoS
Hello guys!
LIN1s
ASL S22 English Commentary…
namkraft
Poker (part 2)
Nebuchad
An Exploration of th…
waywardstrategy
Customize Sidebar...

Website Feedback

Closed Threads



Active: 6108 users

[H] Coding in C (Pointer and segfaults)

Blogs > evanthebouncy!
Post a Reply
evanthebouncy!
Profile Blog Joined June 2006
United States12796 Posts
Last Edited: 2009-01-29 21:19:25
January 29 2009 11:05 GMT
#1
solved
I think my pointer points wrongly into some random address that's forbidden, so I'm getting a seg-fault. I'll describe what I am trying to do here:
I made an array of 10 elements, and the array looks like this:
|0|1|2|3|4|5|6|7|8|9|
I made a function, which uses a pointer to alter these 10 elements. The function map2x will first use a pointer, and points it to the first element.
It is done by p = x; Or so I think. Since l[10] is an array, l would just be a pointer points to the first element. In another word, l == &l[0]; would be true.
Then I basically times the content of that address by 2, move the pointer to the next one, times that by 2, untill it reaches the end.
But no. The plan fails and I get segfault, but I'm not sure where the bug is T_T So help?

+ Show Spoiler [codehere] +

#include <stdio.h>

int l[10];
int *p;
void map2x(int haha[]);

main()
{
int i;
for (i=0;i<10;++i)
{
l[i] = i;
}
map2x(l);
for (i=0;i<10;++i)
{
printf("%d\n", l[i]);
}
}

void map2x(int *x)
{
int i;
p = x; //experiment by changing *p=*x or something;;
for (i=0; i<9; ++p)
{
*p = *p * 2;
}
}


so I tried to change line14 from map2x(l) to map2x(&l[0]) which should be equivalent, but that doesn't solve the problem TT

**
Life is run, it is dance, it is fast, passionate and BAM!, you dance and sing and booze while you can for now is the time and time is mine. Smile and laugh when still can for now is the time and soon you die!
fonger
Profile Blog Joined March 2006
United Kingdom1218 Posts
Last Edited: 2009-01-29 11:25:59
January 29 2009 11:23 GMT
#2
You're not incrementing i in your map2x loop :p

ie. the loop goes on infinitely but your segfault occurs as soon as p is incremented more than 10 times (or so, depending on your memory allocator and padding and stuff)
jhNz
Profile Blog Joined September 2006
Germany2762 Posts
January 29 2009 11:24 GMT
#3
On January 29 2009 20:23 fonger wrote:
You're not incrementing i in your map2x loop :p


that was my first thought too :>

haven't looked closer onto the code but you have to increment i not p in your for-loop.

for (i = 0; i<10, i++)
{
...
}
http://twitter.com/jhNz
jhNz
Profile Blog Joined September 2006
Germany2762 Posts
Last Edited: 2009-01-29 11:27:51
January 29 2009 11:25 GMT
#4
i compiled it with the correct loop and it works properly

[image loading]
http://twitter.com/jhNz
Mastermind
Profile Blog Joined April 2008
Canada7096 Posts
Last Edited: 2009-01-29 11:28:50
January 29 2009 11:28 GMT
#5
Use this:

for (i=0; i<10; i++)
{
p[i] = p[i] * 2;
}


evanthebouncy!
Profile Blog Joined June 2006
United States12796 Posts
January 29 2009 21:19 GMT
#6
On January 29 2009 20:23 fonger wrote:
You're not incrementing i in your map2x loop :p

ie. the loop goes on infinitely but your segfault occurs as soon as p is incremented more than 10 times (or so, depending on your memory allocator and padding and stuff)


AHahHA nice. Thanks. Now it works proper.

evan@Evan-Box:~/Documents/cs61cl/lab/lab3$ ./a.out
9
0
2
4
6
8
10
12
14
16
Life is run, it is dance, it is fast, passionate and BAM!, you dance and sing and booze while you can for now is the time and time is mine. Smile and laugh when still can for now is the time and soon you die!
Please log in or register to reply.
Live Events Refresh
PSISTORM Gaming Misc
15:55
FSL TeamLeagueFINALS ST vs PTB
Freeedom33
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
BRAT_OK 67
StarCraft: Brood War
Britney 26815
Calm 4595
Sea 3213
Mini 1040
EffOrt 644
Soma 596
Larva 525
firebathero 336
Rush 287
Stork 217
[ Show more ]
hero 164
Dewaltoss 123
ggaemo 108
Sharp 93
ZergMaN 79
Soulkey 67
actioN 64
Hyun 61
Sexy 40
Pusan 23
sorry 22
Rock 17
Noble 14
IntoTheRainbow 12
Mong 9
Terrorterran 2
Dota 2
Gorgc12385
Dendi761
febbydoto13
Counter-Strike
summit1g5648
fl0m4273
byalli431
Heroes of the Storm
Liquid`Hasu104
Other Games
FrodaN1980
B2W.Neo604
Mlord556
ceh9482
uThermal378
crisheroes269
TKL 265
DeMusliM262
Hui .145
RotterdaM102
ROOTCatZ99
QueenE45
MindelVK6
Organizations
Other Games
gamesdonequick487
StarCraft: Brood War
lovetv 12
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 21 non-featured ]
StarCraft 2
• printf 21
• Response 11
• StrangeGG 1
• Kozan
• Migwel
• AfreecaTV YouTube
• sooper7s
• intothetv
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• Michael_bg 10
• blackmanpl 7
• FirePhoenix4
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2805
• WagamamaTV536
League of Legends
• Jankos1641
• Nemesis1571
Other Games
• Shiphtur352
Upcoming Events
PiG Sty Festival
6h 42m
Afreeca Starleague
10h 42m
RSL Revival
15h 42m
Clem vs Serral
herO vs Rogue
WardiTV Summer Champion…
18h 42m
WardiTV Weekly
1d 17h
Sparkling Tuna Cup
2 days
PiGosaur Cup
3 days
Replay Cast
3 days
Kung Fu Cup
3 days
Replay Cast
4 days
[ Show More ]
The PondCast
4 days
Replay Cast
5 days
IntoTheTV X SOOP
5 days
RSL Revival
6 days
Liquipedia Results

Completed

Escore Tournament S3: W5
CranK Gathers Season 4: BW vs SC2 Team League
Eternal Conflict S2 Finale

Ongoing

CSL 2026 Summer (S21)
KCM Race Survival 2026 Season 3
ASL Season 22: Qualifier #1
RSL Revival: Season 6
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026

Upcoming

ASL Season 22: Qualifier #2
K-JUNGMAN
Acropolis #5
Escore Tournament S3: W6
Escore Tournament S3: W7
Escore Tournament S3: W8
CSLAN 4
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
PiG Sty Festival 8.0
Thunderpick World Champ.
ESL Pro League Season 24
Stake Ranked Episode 4
Logitech G Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
Esports World Cup 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.