So here is my problem, I have created this struct:
typedef struct foo *fooPtr;
struct foo {
foo2Ptr node;
foo3Ptr node;
char *Random;
};
fooNew(char* String) {
int StringLength;
StringLength = strlen(String) + 1;
Newfoo = (fooPtr *)malloc(sizeof(fooPtr));
Newfoo->Random = (char*)malloc(StringLength * sizeof(char));
strcpy(Newfoo->Random, String);
/*Rest of foo members initialized below....*/
}
fooChangeRandom(fooPtr Foo, String) {
int StringLength;
StringLength = strlen(String) + 1;
free(Foo->Random);
Foo->Random = (char*)malloc(StringLength * sizeof(char));
strcpy(Foo->Random, String);
}
But whenever I try to print the new string I get random characters. As I recall C requires me to free everything I allocate with malloc, calloc, or realloc so how does Random change when the fooNew ends? Tried to debug and at free I get the SIGTRAP error, on the line that free() gets used, however this is the only dynamically allocated object I have created and I haven't used free before. So Is there something I am missing or something wrong with the syntax of malloc or free?
You need to correct
Newfoo = (fooPtr *)malloc(sizeof(fooPtr));
to
Newfoo = (fooPtr *)malloc(sizeof(struct foo));
Related
I'm learning C.
I have a structure, and if I need to set array of structures -> so I allocate memory for this array. But do I need separately allocate memory for fields in this structure?
Like this:
struct Call{
char *country;
int duration;
};
int main(){
struct Call *calls;
int n;
scanf_s("%d", n);
calls = (struct Call *) calloc(n+1 , sizeof(struct Call));
}
You need not to allocate space for data members of objects of the structure type because they belong to the objects.
But it seems you will need to allocate a character array the pointer to which will be stored in the data member country if you want that objects will be owners of the corresponding strings.
For example
struct Call *calls = calloc( 1, sizeof( struct Call ) );
const char *country = "Some country";
calls->country = malloc( strlen( country ) + 1 );
strcpy( calls->country, country );
When you will deallocate memory for objects of the type struct Call you will need at first to free the memory allocated for character arrays pointed to by data members country.
Yes, you must initialize any pointer before you can dereference it. This means allocating memory for it, or assigning it to already-allocated memory. That's a universal rule in C, there's no special cases for pointers in structures. C will not "recursively" allocate memory for you. Among other things, how would it know how much you need? Consider your simplified code below
int main(){
struct Call *calls;
calls = calloc(1 , sizeof(struct Call));
}
Assuming calloc succeeded, calls now points to a memory block that contains space for a single struct Call, which includes space for the char pointer and int. However, country itself is still an unintialized pointer, and you must allocate space for it or point it to something already-allocated before you can safely dereference it
calls->country = malloc(25);
if (calls->country == NULL) exit(-1); // handle error how you want
strcpy(calls->country, "Portugal");
printf("%s\n", calls->country); // prints Portugal
or something like
char myCountry[] = "Spain";
calls->country = myCountry;
myCountry[0] = 'X';
printf("%s\n", calls->country); // prints Xpain
Also see Do I cast the result of malloc?
You need to allocate space for the struct and for char array.
You probably want to dynamically add calls to the array so you need to know the size of the array as well:
typedef struct Call{
char *country;
int duration;
}Call;
typedef struct
{
size_t size;
Call call[];
}Calls_t;
Calls_t *addCall(Calls_t *calls, const int duration, const char *country)
{
size_t newsize = calls ? calls -> size + 1 : 1;
calls = realloc(calls, sizeof(*calls) + newsize * sizeof(calls -> call[0]));
if(calls)
{
calls -> size = newsize;
calls -> call[newsize - 1].country = malloc(strlen(country) + 1);
if(!calls -> call[newsize - 1].country)
{
/* error handling */
}
strcpy(calls -> call[newsize - 1].country, country);
calls -> call[newsize - 1].duration = duration;
}
return calls;
}
void printCalls(const Calls_t *calls)
{
if(calls)
for(size_t i = 0; i < calls -> size; i++)
printf("Call %zu: Country:%s Duration:%d\n", i + 1, calls -> call[i].country, calls -> call[i].duration);
}
int main(void)
{
Calls_t *calls = NULL, *tmp;
tmp = addCall(calls, 10, "Poland");
if(tmp) calls = tmp;
tmp = addCall(calls, 20, "UK");
if(tmp) calls = tmp;
tmp = addCall(calls, 30, "US");
if(tmp) calls = tmp;
printCalls(calls);
/* free allocated memory */
}
https://godbolt.org/z/Kb5bKMfYY
This problem is similar to mine.
But deliver no solution for me.
This is only a simplified code for testing and better understanding.
I know that this code doesn't care of problems after the malloc functions.
The code is for saving words in a struct called List, within a char** storage that used as array.
To create the list and add an item works fine.
But the deletion of the list makes problems.
Here is the code:
Declaration of the list:
typedef struct {
char** storage;
} List;
Main:
int main(){
int size = 2;
List* list;
list = new_list(2);
add(list, "Hello", 0);
add(list, "World", 1);
printf("\nlist->storage[0]: %s", list->storage[0]);
printf("\nlist->storage[1]: %s", list->storage[1]);
delete_list(&list,size);
return 0;
}
Make a new list:
List* new_list(size) {
List* listptr = malloc(sizeof(List));
listptr->storage = (char**)malloc(size * sizeof(char));
return listptr;
}
Add a string to the list:
void add(List* list, char* string, int pos) {
list->storage[pos] = (char*)malloc(strlen(string) * sizeof(char));
list->storage[pos] = string;
}
Delete the list, with all members:
void delete_list(List** list, int size) {
int a = 0;
for (a = 0; a < size; a++)
free((*list)->storage[a]);
free((*list)->storage);
free(*list);
}
Here I get an error in the for loop, at the line 'free((*list)->storage[a])'.
The goal is here to delete every allocated string.
If the list has no members, therefore the code run not in the for loop and the 'delte_list' function work well.
So that is my mistake in the line:
'free((*list)->storage[a])'
This allocation is wrong:
listptr->storage = (char**)malloc(size * sizeof(char));
^^^^^
As storage is a char** the sizeof should be sizeof(char*). When you only use sizeof(char) you end up with too little memory and later you write outside the allocated memory.
Also this line:
list->storage[pos] = string;
seems wrong.
Here you probably need a strcpy like:
strcpy(list->storage[pos], string)
and also add 1 to the malloc for the string termination, i.e.
malloc((1 + strlen(string)) * sizeof(char));
but notice that sizeof(char) is always 1 so
malloc(1 + strlen(string));
is fine.
BTW: A good way of getting your malloc correct is to use "sizeof what_the_variable_points_to". Like:
char** x = malloc(size * sizeof *x);
^^
Use *x instead of sizeof(char*)
in this way you always get the correct size and avoid bugs due to simple typos.
As an example from your code:
List* listptr = malloc(sizeof(List)); // works but
List* listptr = malloc(sizeof *listptr); // this is less error prone
I make a person in a person struct with typedef person_t:
int main(int argc, char* argv[]) {
person_t a;
memset(&a, 0, sizeof(person_t));
person_set_name(&a, "Konrad Hoppenstauffer");
person_set_age(&a, 42);
void person_set_name(person_t* person, char* name) {
if(person->name) {
free(person->name);
}
person->name = malloc(sizeof(char) * strlen(name) + 1);
strcpy(person->name, name);
}
The above works just fine.
Problem happens when I use this function:
person_t* string_to_person(char* str) {
person_t* person = malloc(sizeof(person_t));
int len = 0;
while(str[len] != '\t') {
len++;
}
char* name = malloc(len + 1);
int i;
for(i = 0; i < len; i++) {
name[i] = str[i];
}
name[len] = '\0';
person_set_name(person, name);
person_set_age(person, atoi(str+len+1));
return person;
}
Here str is something like: "Name Nameson\t22". That is name seperated by tab. And then I separate the two and put characters in char* name.
person_t is a typedef for a struct.
If I remove the free(person->name) from person_set_name, everything works fine. But if I leave it in, name becomes garbage, for example: "É8>".
I assume that something wrong happens in the for loop where I copy each character. But with my limited experience with C I can't see what. Help is appreciated.
You're trying to free a garbage pointer.
After:
person_t* person = malloc(sizeof(person_t));
malloc doesn't initialize the new memory block with any particular data, so your program must treat *person as containing garbage at this point (since it could contain any data). In particular, person->name (i.e. (*person).name) might not be NULL.
A short time later, this code runs:
if(person->name) {
free(person->name);
}
- if person->name was not NULL, then you free it. Since person->name doesn't point to something you allocated with malloc, at this point you're well and truly in Undefined Behaviour Land™.
One possible fix is to set person->name = NULL; immediately after allocating the person.
I'm trying to pass in some parameters into one function, store those values into a set of elements in a struct. Then print those values from within the struct, by calling a another function.
Here's what I'm trying to do:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct temp
{
int num;
char * name;
struct temp * nextPtr;
}temp;
temp * test();
char * printTest(temp * print);
int main (void)
{
test("TV",200);
struct temp * t;
printTest(t);
return 0;
}
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
mem->name = malloc(sizeof(strlen(name) + 1));
mem->name = name;
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
char * printTest(temp * print)
{
char * output;
output = malloc(sizeof(struct temp));
print = malloc(sizeof(struct temp));
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
return output; //should print "TV" and costs "200"
}
The function printTest, doesn't seem to print out anything, rather if I hardcore printf within it, it prints null values and zero. However, I tried to print the struct values in the test function, which does work after initializing the values.
For example, if I do printf("%d",mem->num); this does print out a value of 200(In the test function).
But the sprintf and return combination in the last function doesn't result in the same result. Any help would be much appreciated!
You're not capturing the value you return from test, therefore it just gets lost:
int main (void)
{
//changed to capture return value of test.
struct temp * t = test("TV",200);
printTest(t);
return 0;
}
Also your print function is wrong:
// this now points to the struct you allocated in test.
char * printTest(temp * print)
{
char * output;
// you don't really want the size of temp here, you want the size
// of your formatted string with enough room for all members of the
// struct pointed to by temp.
output = malloc(sizeof(struct temp));
// you're not using this.
print = malloc(sizeof(struct temp));
// you sprintf from a buffer pointing to nothing, into your output buffer
// writing past the memory you actually allocated.
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
// return doesn't print anything, it simply returns the value that your
// function signature specifies, in this case char *
return output; //should print "TV" and costs "200"
}
Try this, you'll take the pointer you allocated, and use printf to write a formatted string to stdout:
// we're not returning anything
void printTest(temp * print ){
if (temp == NULL ){
// nothing to print, just leave.
return;
}
printf("It's name is %s, and it costs %d",print->name,print->num);
return;
}
Also some notes about your test function:
// char is a pointer to a string, from your invocation in main, this is
// a string stored in static read only memory
temp * test(char * name, int num)
{
// you malloc memory for your struct, all good.
struct temp * mem = malloc(sizeof(struct temp));
// you malloc space for the length of the string you want to store.
mem->name = malloc(sizeof(strlen(name) + 1));
// here's a bit of an issue, mem->name is a pointer, and name is a pointer.
// what you're doing here is assigning the pointer name to the variable
// mem->name, but you're NOT actually copying the string - since you
// invoke this method with a static string, nothing will happen and
// to the string passed in, and you'll be able to reference it - but
// you just leaked memory that you allocated for mem->name above.
mem->name = name;
// num is not apointer, it's just a value, therefore it's okay to assign
// like this.
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
Try this instead:
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
// we still malloc room for name, storing the pointer returned by malloc
// in mem->name
mem->name = malloc(sizeof(strlen(name) + 1));
// Now, however, we're going to *copy* the string stored in the memory location
// pointed to by char * name, into the memory location we just allocated,
// pointed to by mem->name
strcpy(mem->name, name);
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
There were many more problems than at first blush. Here is a cleaned up version. You cannot assign a string (e.g. mem->name = name;). You can either allocate for mem->name and then strncpy name to it, or use strdup to allocate and copy at once. In printTest, you must allocate space for output sufficient to hold both the static part of the format string plus print->name and print->num. Look over the following and let me know if you have question.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct temp
{
int num;
char * name;
struct temp * nextPtr;
}temp;
temp * test(char * name, int num);
char * printTest(temp * print);
int main (void)
{
struct temp * t = test("TV",200);
// struct temp * t;
printf ("%s\n", printTest(t));
return 0;
}
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
// mem->name = malloc(sizeof(strlen(name) + 1));
// mem->name = name;
mem->name = strdup (name);
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
char * printTest(temp * print)
{
char *output = NULL;
// output = malloc(sizeof(struct temp));
// print = malloc(sizeof(struct temp));
output = malloc (strlen (print->name) + sizeof print->num + 30);
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
return output; //should print "TV" and costs "200"
}
Output
$ ./bin/struct_rtn
It's name is TV, and it costs 200
Additionally, sprintf outputs to a string. It does not output to standard out, i.e. print to the screen, unlike printf which does. You probably want to call printf or puts on your output string.
this line: 'sprintf(output,"It's name is %s, and it costs %d",print->name,print->num)' needs some significant modification. 1) the source fields" print->name and print->num are within an allocated segment of memory However, they have never been set to any specific value, so they contain trash. The destination pointer 'output' points to an allocated memory area, however that area is no bigger that the source area, (and generally, it is expected to be formatted as a 'temp' struct, not as one long string. and because it is only the size of a temp struct, there will be no room for all the extra characters in the sprintf format parameter. Result will be undefined behaviour as the output from sprintf will overrun the allocated memory area
I am new to c programming and I am stuck with this one its a typedef struct and what I would like to do is that I want to create an array from the double pointer from this structure
typedef struct
{
char* firstname;
float price;
}Name,*pName,**ppName;
typedef struct
{
ppName Names;
unsigned int numPerson;
}Book;
And my main which always give me segmentation fault dont mind the loop it is looping until the use says to quit.
int main(void)
{
Book D;
setUpCollection(&D);
while(..)
{
scanf(...);
switch(...)
{
case 1:
if(!AddNewPerson(&D))
return 1;
break;
case 2:
....
case 3:
....
default:
printf("Please enter a valid choice");
}
}
return 0;
}
void setUpCollection(Book* data){
Name name;
pName pname;
pname= malloc(MAX_PERSON* sizeof(pName));
pname= &name;
data->Names= &pname;
data->numPerson= 0;
}
BOOL AddNewPerson(Book* data){
char *title = malloc(sizeof(char));
int len;
Name name;
pName pname;
scanf(...);
len = strlen(firstname);
name.firstname = malloc(len * sizeof(char*));
name.firstname = firstname;
pname= malloc(1);
pname= &name;
data->DVDs[data->numPerson++] = pname;
printf("%0.2f", data->Names[(data->numPerson)-1]->price);
return TRUE;
}
My main problem is that I cant print all the added names and also getting segmentation fault.
There are quite a few errors in your program but let me mention a few:
Doesn't this seem odd to you:
pname= malloc(MAX_PERSON* sizeof(pName));
pname= &name;
you are creating a memory leak by first letting pname point to the array of pName then assigning to &name.
What is this:
char *title = malloc(sizeof(char)); // ?
here you allocate too less space
name.firstname = malloc(len * sizeof(char*));
it should be
name.firstname = malloc(len * sizeof(char) + 1);
or more readable:
name.firstname = malloc(len+1);
this makes no sense again:
pname= malloc(1);
pname= &name;
again you created a memory leak by first letting pname point to a heap block of 1 byte then assigning it to a local variable which you include in data - the local variable is freed up once you leave AddNewPerson() so data will point to garbage.
Instead do something like this (I am no fan of having
typedefs for pointers), also try avoiding naming types
the same way you name variables for clarity:
typedef struct
{
char *firstname;
float price;
} Name;
typedef struct
{
Name** names;
unsigned int numPerson;
} Book;
Now allocate the initial size of your array, the whole point
of having it on the heap is that the array can grow if more
records are added than MAX_PERSONS so you need to keep track
of the number of used records in the array as well as the number
of records allocated
int allocated = MAX_PERSONS;
Book D;
D.names = malloc( allocated * sizeof(Name*) );
D.numPerson = 0;
then loop over user input and add records keeping
track of how many records have been read. Since names
is an array of pointers, you need to allocate a Name
struct each time you add an entry
e.g.
D.names[i] = malloc( sizeof(Name) );
D.names[i]->firstname = strdup(userInputName);
D.names[i]->price = userInputPrice;
then at each iteration check if there is allocated memory left
++i;
if ( i == allocated )
{
// if yes you need to get more memory, use realloc for that
// get e.g. 10 more records
Name* tmp = realloc( D.names, (allocated + 10)*sizeof(Name) );
if ( tmp != NULL )
{
D.names = tmp;
allocated += 10;
}
else
{ .. some error msg .. }
}