• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 15:03
CEST 21:03
KST 04:03
  • 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
[ASL22] Ro16 Preview: Holy Diver5[ASL22] Ro16 Preview: Rough Waters10[ASL22] Ro24 Preview: Siren's Call8[ASL22] Ro24 Preview: Summer's End9Serral wins HomeStory Cup 2915
Community News
Weekly Cups (Sep 7-12): SHIN, ByuN, MaxPax double down1StarCraft open world shooter announced at BlizzCon100Weekly Cups (Aug 30-Sep 7): herO thrives amid growing schism10Official StarCraft website teases new content ahead of BlizzCon?179Stellar Fest TWO the Moon (Dec 16-20)9
StarCraft 2
General
SC4ALL II: StarCraft 2 Player Announcement 8/8 Yamato Cup Series StarCraft open world shooter announced at BlizzCon Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Has online-only competition during the pandemic changed your enjoyment of pro SC2?
Tourneys
Sparkling Tuna Cup - Weekly Open Tournament RSL Revival: Season 6 - Qualifiers and Main Event RSL goes to London! 2026 Offline Finals Nov 21-22 SC2 AI Tournament 2026 Fall KSL Week #92
Strategy
[H] ZvP Mid-Late Game: Stalkers Collossi HT
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
Mutation # 543 Enhanced Defenses The PondCast: SC2 News & Results Mutation # 542 The Ascended Mutation # 541 Binary Choice
Brood War
General
New 3v3 BGH Ladder (and more) on ShieldBattery! Bot on ladder Syncronization issues and can't find games Remastered crash fix suggestions BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[ASL22] Ro16 Group D [Megathread] Daily Proleagues The Casual Games of the Week Thread Small VOD Thread 2.0
Strategy
Replay Review Process - What do you do? Simple Questions, Simple Answers Odyssey Mineral Stack Saturation Game Theory for Starcraft
Other Games
General Games
Warcraft III: The Frozen Throne Nintendo Switch Thread Stormgate/Frost Giant Megathread EVE Corporation Diablo IV
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Canadian Politics Mega-thread Post Your Watch!
Fan Clubs
MarineLorD Fan Club The Creator Fan Club The ShoWTimE Fan Club
Media & Entertainment
Movie Discussion! [Manga] One Piece Diablo Animated Series on Netflix
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Northern Ireland Global Starcraft
Blogs
[meme] I finally understa…
LUCKY_NOOB
Virtual Romance, Real-Life C…
TrAiDoS
Regacy Esports:Our Goa…
regacyesports
Dreaming of BW patches (mod…
c3rberUs
Customize Sidebar...

Website Feedback

Closed Threads



Active: 9544 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
RotterdaM Event
16:15
Rotti's PatchesSC Open #4
RotterdaM943
IndyStarCraft 219
TKL 202
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 943
ByuN 553
IndyStarCraft 219
TKL 202
EmSc Tv 17
StarCraft: Brood War
Britney 23188
Shuttle 1039
Dewaltoss 170
firebathero 158
Barracks 48
Sexy 43
scan(afreeca) 29
Trap 13
Dota 2
BananaSlamJamma381
Counter-Strike
fl0m667
pashabiceps575
Heroes of the Storm
Grubby2217
Liquid`Hasu262
MindelVK13
Other Games
summit1g3475
FrodaN3432
hiko450
KnowMe405
B2W.Neo278
Dendi250
ToD118
C9.Mang0107
ArmadaUGS106
QueenE64
Trikslyr49
Livibee45
Organizations
Other Games
gamesdonequick1349
StarCraft 2
EmSc Tv 17
EmSc2Tv 17
[ Show 14 non-featured ]
StarCraft 2
• Adnapsc2 10
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Migwel
StarCraft: Brood War
• blackmanpl 28
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 3653
League of Legends
• Jankos1275
Other Games
• imaqtpie679
• Shiphtur114
Upcoming Events
Replay Cast
4h 57m
GSL
15h 57m
AI Arena Tournament
21h 57m
Replay Cast
1d 4h
GSL
1d 15h
Shopify Rebellion Sundays
1d 19h
Spirit vs Mixu
Replay Cast
2 days
Afreeca Starleague
2 days
Shine vs Rush
WardiTV Weekly
2 days
Monday Night Weeklies
2 days
[ Show More ]
Sparkling Tuna Cup
3 days
Afreeca Starleague
3 days
Light vs EffOrt
PiGosaur Cup
4 days
Kung Fu Cup
4 days
The PondCast
5 days
Replay Cast
6 days
Liquipedia Results

Completed

KCM Race Survival 2026 Season 3
Blizzard Classic Cup 2026
Big Dog Cup 2026 Div 1

Ongoing

K-JUNGMAN
ASL Season 22
Super Anchor Qualifying S3
CSL 2026 AUTUMN (S22)
Acropolis #5
Calamity Invitational
Logitech G Play Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026

Upcoming

Acropolis #5 - GSA
Acropolis #5 - GSB
Acropolis #5 - GSC
SC4ALL II: Brood War
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
Copium Cup
PGL Major Singapore 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
IEM Beijing 2026
Stake Ranked Episode 5
PGL Masters Bucharest 2026
1win Private Club #2
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
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.