![]() |
|
#1
|
|||
|
|||
Why seg fault in char array?hi,
i just started C and gt a seg fault here: CPP / C++ / C Code:
thanks. |
|
#2
|
||||
|
||||
|
Hi nusstu.
There are several things wrong with your program. They all have to do with the use of char*. Char* is a an array of chars and is not analogous to a string type in other programming languages.
Good luck! __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
|
Quote:
Hi dsmith, thanks 4 ur reply. But, why do we need malloc here? shouldn't char *str="Hello" allocate the space required? You mentioned i can't use the assignment operator with char* is it because C makes it immutable i.e a constant string? Also, how do we go about assigning an array of int*? I tried, int *a = {1,2}; didn't work? Thanks. |
|
#4
|
||||
|
||||
|
Quote:
No. char* != char[]. One is dynamic (run-time) and the other is static (compile time). char* only specifies a location where a memory address will be stored. And at definition this memory address is arbitrary. When you use char* you must allocate the memory dynamically. CPP / C++ / C Code:
Quote:
This is the exact same problem. You can not use this type of assignment. Again int* != int[]. One is dynamic and the other is static. You can not assign values to a dynamic structure at initilization. You can use: CPP / C++ / C Code:
or: CPP / C++ / C Code:
Hope this helps. __________________
The best damn Sports Blog period. |
|
#5
|
|||
|
|||
|
CPP / C++ / C Code:
hm..why do we need to malloc when char* str = "hello"; would do the job? Is it because it becomes a constant string after that, while malloc isn't a constant? From what i understand, char *str = "hello"; is the same as char str[] = {'h','e',...'\0'}. So why doesn't int *a={1,2}; convert to int a[] = {1,2};? thanks. |
|
#6
|
||||
|
||||
|
Sorry nusstu. I am not doing a very good job of explaining.
Quote:
The answer here is that char* str = "hello" will not do the job. I am not an expert at this, but here is what happens when you declare the different types. char str[size] This is a static definition of an array of characters. It is created in your "program" memory space and is defined at compile time. This is part of the image that is created when your process loads. All of the memory is allocated in this program image and can not be freed or the position of it changed. The only way to create a larger array is to change and recompile your program. Code:
Since all of the memory is allocated at compile time, C allows you to initialize this chunk of memory as well. So in initializations (and only in initializations) we can load this string by using the assignment operator. char* str This is a dynamic definition of an array of characters. The only thing that is created in "program" memory is a variable big enough to hold a memory address. So upon declaring a char* str all we have is: Code:
So since there is no memory allocated to hold the string at compile time, it is impossible to use the assignment initialization. Now, when malloc is used memory is allocated from the "heap" and a pointer is returned to this location. CPP / C++ / C Code:
Code:
Now this memory can be resized & freed as your program executes. This is run-time allocation as opposed to compile-time allocation. Now, my question is why do you want to use int* & char*? If you know the size of your array at compile time, just use int[] and char[]. You can do all of the things that you want to do with the pointers, but the memory is allocated at compile time. You don't need to worry about getting heap memory and freeing heap memory. Only use the pointers if it is critical that memory can be allocated at run time. You can even access the arrays using pointer syntax if you like it better. CPP / C++ / C Code:
Or you can make a pointer point to the location of an array. CPP / C++ / C Code:
I am about out of ideas of how to explain this. The one point I want to make sure that I get across is that a dynamic array is not a static array. When you define a dynamic array there is no memory allocated for it beyond enough to hold the address of where it will point. HTH, d __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
|
Quote:
You have declared a variable named str to be a (pointer to char). Your declaration also sets the value of str to be the address of the string constant "Hello". Note that there is no such thing as a string data type in C, but there is such a thing as a string constant. The string constant is located some place in memory where you can read, but you shouldn't attempt to write to it. Normally, of course, you don't have to know what the actual address is, since you will use the variable name to access the contents. Then you incremented the pointer, so that it points to the second character of the string constant (so far, so good). But then wrote something new to that memory location. You can change the value of str to anything you want, but it is not legal to attempt to change the value of a string constant. In this case, the compiler doesn't keep track of what your assignment statement is doing, even though for this simple program we can tell at a glance what's going on. The way that you have written the program, there is not any illegal C code here (so no compiler errors), but the attempted activity at run time is bad. Some compilers actually generate code that lets this program do perform the action that you have indicated here, and some generate an "exception" at run time. When I compiled the code with Borland bcc32 and Microsoft Visual C++ on my Windows XP box, the program ran to completion, and actually changed the string to "HAllo". With gcc, I got a run time error: Quote:
Here's the program that I actually ran. Note that I separated the declaration and assignment of str. (This result idendical to that of your example.) I also separated the increment and assignment statement so that you can see exactly where the exception occurred. CPP / C++ / C Code:
Dave |
|
#8
|
|||
|
|||
|
Quote:
Not true. The first form declares an variable to be a pointer (tells the compiler to allocate memory for a pointer). Then it sets the value of the pointer to be the address of the string constant "hello" (some memory location readable but not writeable by this program). The second form tells the compiler to allocate storage for six chars and puts the values 'h', 'e', 'l', 'l', 'o', '\0' into those locations. These memory locations are readable and writeable by this program. Look at the following. Compile and run it. If you don't understand what's happening, you can always ask. CPP / C++ / C Code:
Regards, Dave |
|
#9
|
|||
|
|||
|
Quote:
When you write CPP / C++ / C Code:
The variable str is declared as a "pointer to char", and its value is set to the address of the string constant "Hello". A string constant is a null-terminated sequence of chars. There is no corresponding null-terminated sequence of ints, so the notation CPP / C++ / C Code:
whereas CPP / C++ / C Code:
allocates two ints and sets their values to 1 and 2. Dave Dave |
|
#10
|
||||
|
||||
|
Look at it this way.
You live in a house (or something similar). This house has an address. When you tell someone where you live, you give them your address. That address is NOT the house itself, but the location of the house -- it points to the house. The house is the physical location. char p[6]; defines the house itself. The physical space the house is at. There are no details about the house. char p[6] = "hello"; defines the house as above, and it's color, style, and other details. char *p = "hello"; defines the address of the house (*p) and its physical location and details ("hello"). char *p; is a blank page in your address book. It's ready to contain an address, but has not been filled in yet. It points nowhere (or more likely to a bad address that sends you to an empty lot in another state) __________________
Age is unimportant -- except in cheese |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2 D char Array | gwk | C Programming Language | 3 | 20-Jul-2004 13:40 |
| 3D array dynamic memory allocation | cjwatchdog | C Programming Language | 3 | 20-Feb-2004 15:27 |
| (read/write file) newbie need help plz | momotx | C Programming Language | 6 | 28-Jan-2004 13:40 |
| convert long to pointer to char | realpopeye | C++ Forum | 2 | 26-Sep-2003 10:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The