Question:
C programming using malloc()?
anonymous
2008-08-16 17:00:32 UTC
Hi everyone,

So i'm having a bit of a problem with my program. Everytime I run it, I get a run-time error and crashes. I narrowed the problem down to my malloc declaration. I'm not sure if i'm using it right. I'm trying to copy a line of text from a file (the line only includes the word "apple") and copy it onto a double pointer FROM an array.

This is what I have:

#define BUFSIZE 512

int lengthOfLine = 0;
char line[BUFSIZE];
char **buffer = NULL;

fgets(line, BUFSIZE, fp);
lengthOfLine = strlen(line);
buffer = (char**)malloc(lengthOfLine * sizeof(char));
strcpy(*buffer, line);
printf("%s\n", *buffer);
free(buffer);

Am I doing anything wrong? Any help would be appreciated.

Thanks.
Four answers:
leeguy92
2008-08-16 17:26:24 UTC
char *buffer = NULL //buffer is new pointer(a memory location)



buffer = malloc(lengthOfLine)

//malloc returns location of newly allocated memory - store in buffer pointer

//char = 1 byte btw



strcpy(buffer,line)

//strcpy copies string at line to location pointed to by buffer

//line is actually a pointer to the base of the line array

//all arrays are handled as pointers

//strcpy stops copying the string when it hits the null character \0



printf("%s\n",buffer)

//displays string at buffer-pointed location



remember:

array variables are pointers to the start of a block of memory where the array is. the [1],[2] thing is an offset.

the offset is added to the base address of the array, and the value at that location is the value given.



&cat : gives the memory address of cat

*cat : gets the value at the memory address stored in cat

**cat : gets the value at the memory address stored in *cat





Pointers can be confusing. it helps to see them for what they are - memory locations with an address of another location rather than a value. there is no real difference in the way they are stored, just the way you(programmer) use them.
anonymous
2008-08-16 17:39:35 UTC
I agree with the first answerer. Why use char **buffer?



Try it with one asterik.

Also, add 1 more byte to the malloc to leave room for the null character.



char *buffer = NULL;

fgets(line, BUFSIZE, fp);

lengthOfLine = strlen(line);

buffer = (char*)malloc(lengthOfLine + 1);

strcpy(buffer, line);
?
2016-05-29 05:59:19 UTC
It is always necessary to reserve memory, no matter how much is available. The only question is whether to reserve it when the program is written, or after the program starts running. The ordinary declaration of variables outside function scope allocates memory at compile time. The malloc() function allocates memory dynamically at run time. The malloc() function is used when the exact memory requirements of a program are not known at the time it is written. The program can determine what it needs after it starts running and then call malloc() to actually obtain the needed memory. Here's an example, a program that lets you store a variable number of integers: #include using namespace std; int main(int iArgs, char *pszArgs[]) { int Amount, *Integers; cout << "How many integers do you need to store? "; cin >> Amount; Integers= (int*) malloc(sizeof(int)*Amount); if (Integers==NULL) { cout << "Memory allocation failed!"; return 0; } for (int *Work=Integers, Index=1; Index<=Amount; Index++) { cout << "Enter integer " << Index << ": "; cin >> *Work; Work++; } for (int *Work=Integers, Index=1; Index<=Amount; Index++) { cout << "Integer " << Index << " is " << *Work << endl; Work++; } free(Integers); return 0; }
Me M
2008-08-16 17:24:46 UTC
I guess I have to ask, why are you trying to use a char ** , instead of just a char *?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...