• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 17:25
CEST 23:25
KST 06:25
  • 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
Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12TL.net Map Contest #22 - Voting & Ladder Map Selection7Code S Season 2 (2026) - RO8 Preview5[ASL21] Finals Preview: Two Legacies21
Community News
Douyu Cup 2026: $20,000 Legends Event (June 26-28)8[BSL22] Non-Korean Championship from 13 to 28 June4Weekly Cups (May 25-31): Clem doubles, 2v2 circuit heads toward finale0StarCraft II 5.0.16 PTR Patch Notes may 26th156Weekly Cups (May 18-24): MaxPax wins doubles0
StarCraft 2
General
RSL S6 finale at Blizzcon Oliveira Would Have Returned If EWC Continued Team Liquid Map Contest #22: Results and Winners High level ptr replays? where can I find them? StarCraft II 5.0.16 PTR Patch Notes may 26th
Tourneys
Douyu Cup 2026: $20,000 Legends Event (June 26-28) Maestros of The Game 2 announcement and schedule ! Sparkling Tuna Cup - Weekly Open Tournament Sea Duckling Open (Global, Bronze-Diamond) GSL Code S Season 2 (2026)
Strategy
[G] Having the right mentality to improve
Custom Maps
[D]RTS in all its shapes and glory <3
External Content
Mutation # 530 One For All The PondCast: SC2 News & Results Mutation # 529 Opportunities Unleashed Mutation # 528 Infection Detected
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion vespene.gg — BW replays in browser Quality of life changes in BW that you will like ? [BSL22] Non-Korean Championship from 13 to 28 June
Tourneys
[Megathread] Daily Proleagues [ASL21] Grand Finals [BSL22] Grand Finals - Sunday 21:00 CEST Escore Tournament StarCraft Season 2
Strategy
Creating a full chart of Zerg builds Relatively freeroll strategies Why doesn't anyone use restoration? Any training maps people recommend?
Other Games
General Games
Path of Exile Stormgate/Frost Giant Megathread Nintendo Switch Thread PC Games Sales Thread ZeroSpace Megathread
Dota 2
Looking for a Dota Mentor Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Russo-Ukrainian War Thread Trading/Investing Thread Things Aren’t Peaceful in Palestine
Fan Clubs
The HerO Fan Club! The herO Fan Club!
Media & Entertainment
Movie Discussion! [Req][Books] Good Fantasy/SciFi books [TV/BOOK] *SPOILERS* Game of Thrones Discussion [Manga] One Piece
Sports
2024 - 2026 Football Thread Formula 1 Discussion Cricket [SPORT] TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Facing Challenges in Mobile App Development
TL Community
The Automated Ban List
Blogs
Does Workplace Frustration D…
TrAiDoS
An Exploration of th…
waywardstrategy
I'm an arrogant trash talke…
FlaShFTW
Gauntlet SC2: A Retrospectiv…
Ctone23
Why RTS gamers make better f…
gosubay
Customize Sidebar...

Website Feedback

Closed Threads



Active: 9498 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
BSL22 NKC (BSL vs China)
19:00
Group Stage - Day 2
Jaystar vs Dewalt
eOnzErG vs TerrOr
XuanXuan vs Bonyth
Mihu vs Dewalt
Messiah vs Jaystar
eOnzErG vs Bonyth
TerrOr vs Dewalt
ZZZero.O255
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
JuggernautJason123
ProTech100
CosmosSc2 71
Railgan 59
Livibee 39
StarCraft: Brood War
Britney 11845
ZZZero.O 255
Shine 13
Dota 2
XaKoH 486
monkeys_forever201
League of Legends
Doublelift9041
JimRising 493
Super Smash Bros
Mew2King103
Other Games
summit1g9562
Grubby4337
C9.Mang0172
Tefel19
Organizations
Other Games
gamesdonequick968
BasetradeTV145
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 14 non-featured ]
StarCraft 2
• davetesta125
• Hupsaiya 73
• musti20045 20
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• masondota2870
Upcoming Events
Wardi Open
13h 35m
OSC
1d 2h
Replay Cast
2 days
The PondCast
3 days
Replay Cast
4 days
OSC
4 days
CranKy Ducklings
4 days
BSL22 NKC (BSL vs China)
5 days
XuanXuan vs Jaystar
Mihu vs Messiah
eOnzErG vs Dewalt
Bonyth vs Jaystar
TerrOr vs Messiah
XuanXuan vs Mihu
eOnzErG vs Jaystar
BSL22 NKC (BSL vs China)
6 days
Dewalt vs Messiah
Bonyth vs Mihu
TerrOr vs XuanXuan
eOnzErG vs Messiah
Jaystar vs Mihu
Dewalt vs XuanXuan
Bonyth vs TerrOr
Liquipedia Results

Completed

Acropolis #4 - GSB
2026 GSL S2
Heroes Pulsing #1

Ongoing

IPSL Spring 2026
KCM Race Survival 2026 Season 2
Acropolis #4
CSCL: Masked Kings S4
YSL S3
BSL 22 Non-Korean Championship
SCTL 2026 Spring
Maestros of the Game 2
WardiTV Spring 2026
Murky Cup 2026
Heroes Pulsing #2
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1

Upcoming

CSLAN 4
Blizzard Classic Cup 2026
Kung Fu Cup 2026 Grand Finals
RSL Revival: Season 6
CranK Gathers Season 4: BW vs SC2 Team League
HSC XXIX
Douyu Cup 2026
Heroes Pulsing #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
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.