Sponsored Content
Top Forums Programming Scanf() string pointer problem Post 302874257 by yifangt on Thursday 14th of November 2013 02:42:49 PM
Old 11-14-2013
free malloc()

Thanks Corona688!
I tried putting your lines before return 0, it was compiled fine and ran. But after printing it gave many errors:
Code:
*** glibc detected *** ./ch1016c: double free or corruption (out): 0x0000000001423030 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fe24e2f3b96]
./ch1016c[0x4007ec]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fe24e29676d]
./ch1016c[0x400539]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 87823294 
......

To fully understand what is happening here, I tried two ways to free the memory. One is within the printf() loop I freed list[x].studentName at Line 35. My understanding is: Once it has been printed, no needed any more and, free it. Then Line 38 free(list) for the struct;
The other is by putting a separate loop Line 37 to free(list[x].studentName). Then the same Line 38 free(list) as list is the struct;
Code:
 ........
for (x = 0; x < numOfStudents; x++) {
         printf("%s, #%d, %d: \n", list[x].studentName, list[x].studentNumber, list[x].phoneNumber);
 //        free(list[x].studentName);                                          //Line 35
      }
 for (x = 0; x < numOfStudents; x++) { free(list[x].studentName); }          //Line 37
         free(list);                                                          //Line 38
    return 0;
 }

Both had the same problem. Not sure Line 35 is a correct way.
Is Line 35 a correct way to do it?
Is Line 37 the only way to do it when handling 10~100 millions rows?
But both had error. What did I miss? Thanks a lot!
 

10 More Discussions You Might Find Interesting

1. Programming

Scanf problem under LINUX...

I have a problem reading characters from keyboard with the scanf function. Here there is a little piece of code: #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> /* The last 3 libraries are included because in the real program I use some... (4 Replies)
Discussion started by: robotronic
4 Replies

2. Programming

Problem with function which reutrns pointer to a value

i have a function: char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID) this returns a pointer to CountryName if cityId is given. to retrieve countryname i give: char *CountryName; CountryName = pcCityIdToCountryName(..................); but when i compile it is giving :... (5 Replies)
Discussion started by: jazz
5 Replies

3. Programming

problem with scanf

hi all! i've written a simple c program: #include<stdio.h> #include<stdlib.h> int main() { int a; char b; char c; ... (4 Replies)
Discussion started by: mridula
4 Replies

4. Programming

pointer problem

could any one tell why the following is showing segmentation fault while using **ptr but working fine using **a #include<stdio.h> ... (1 Reply)
Discussion started by: useless79
1 Replies

5. Programming

String and pointer problem

i am having a string like " X1 " ---> string lenght is 30 I have stored this to a chararry . ref so here ref = " X1 " now i trim the left space by my function . Si the string now becomes "X1 " ---> string lenght is 15... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

6. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

7. Programming

segfault in pointer to string program

hello all, my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str) why this function gives me segfault :wall:and the other code executes comfortably though both code uses same pointer arithmetic. ... (4 Replies)
Discussion started by: zius_oram
4 Replies

8. Programming

pointer problem

Does anyone know? int x = 1; int *p = &++x; //ok ! int *q = &x++; //gives an error :O why the first pointer is ok but the second is an error? (13 Replies)
Discussion started by: nishrestha
13 Replies

9. Programming

How i use pointer as a string in c programing?

I'm newbie learner. My all friend use windows just only me use linux. so i can't solve any problem by myself. i need a solution. how can i use pointer as a string. #include<string.h> #include<stdio.h> int main() { char *s='\0'; gets(s); puts(s); return 0; } This code work on... (6 Replies)
Discussion started by: raihan004
6 Replies

10. Programming

String pointer does not work

Hello, I am trying to reverse complement one string and reverse another (NO complement!), both with pointer. My code compiled without error, but did not do the job I wanted. #include <stdio.h> #include <stdlib.h> #include <zlib.h> #include "kseq.h" // STEP 1: declare the type of file... (5 Replies)
Discussion started by: yifangt
5 Replies
set_color(1)							       fish							      set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 06:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy