[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


My question int x; void test(char *) { char *b,...

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)

Question by Maverickmalai
Submitted on 8/25/2003
Related FAQ: comp.lang.c Answers to Frequently Asked Questions (FAQ List)
Rating: Not yet rated Rate this question: Vote
My question
int x;

void test(char *)
{
  char *b, *c="heloworld";
  short z;
  z=0x41;
  b=(char *)malloc(sizeof(10));
  strcpy(b,"helloworld");
  printf("%d %d", b, &z);
  free(c);
}

1) where will the variable x will be stored?
   whether in stack, datasegement, heap.
2)where will be *c stored?
3)where will be helloworld stored?

4)while executing which line of the program the code get crashed?Reason






Answer by Suresh
Submitted on 10/29/2003
Rating:  Rate this answer: Vote
Hi MaverickMalai,

1.  x will be stored in symbol table. All global variables will be stored in symbol table.

2.  All local variables will be stored in stack.

4. The command "free(c)" will crash or create segmentation violation. The following is the definition for "free " given by GNU.

"free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc()."

In your coding "c" is a char pointer whose value is assigned at the time of declaration. No memory is allocated explicitly using "malloc" or "realloc". Attempting to freeing a memory which is not created explicitly will crash the application.

 

Answer by z
Submitted on 2/5/2004
Rating:  Rate this answer: Vote
You should malloc 10*sizeof(char), which allocates 10 spaces for char variables. Otherwise you have just malloc'd 10 bytes, which hardly is enough to store that largish string you have there.

 

Answer by viren
Submitted on 2/24/2004
Rating:  Rate this answer: Vote
Hi there,

> 1) where will the variable x will be stored? whether in stack, datasegment, heap.

"x" will get stored in the BSS segment. A BSS segment is just like a Data segment - the differences being that BSS contains the un-initialised values and Data segment contains initialised values.

> 2)where will be *c stored?

The auto variable "c" would be stored on the stack. (as is the case with all other autos )

> 3)where will be helloworld stored?

The string "helloworld" is really a string literal (as is indicated to compiler by writing - char *c = "helloworld"; ) This string literal would be stored in the read-only Data segment. (Some compilers may give you the option to store it in the Code segment - e.g Code Warrior for PalmOS )

> 4)while executing which line of the program the code get crashed?Reason

free() call in C (or in C++) expects that the pointer that you are gonna pass on to free function, has some other useful info at the beginning of the memory pointed by that pointer. This is the reason why a free() call should always be given a pointer returned by malloc() (which writes the info needed by free() ). In your case, you are passing a pointer that lacks this info causing the system to go for toss. Hope this helps....if it does not, drop me a mail at drvirens@yahoo.com for more details.....

Cheers.

-Viren





 

Answer by Mahesh
Submitted on 11/18/2005
Rating: Not yet rated Rate this answer: Vote
All global and static variables will be stored in Data Segment (Again sub-divided into two, initialized and non - initialized DS).

String Literals will be stored in the Read Only segment. (U cannot edit such memory location).

And all the local defined/declared will be stored in Stack.

You can free memory, only if it is allocated using malloc/calloc !!!!!

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.