c - Understanding pointers in a structure and malloc -


i learning c (reading sam's teach c in 24 hours). i've gotten through pointers , memory allocation, i'm wondering them inside structure.

i wrote little program below play around, i'm not sure if ok or not. compiled on linux system gcc -wall flag compiled nothing amiss, i'm not sure 100% trustworthy.

is ok change allocation size of pointer have done below or possibly stepping on adjacent memory? did little before/after variable in structure try check this, don't know if works , if structure elements stored contiguously in memory (i'm guessing since pointer structure can passed function , structure manipulated via pointer location). also, how can access contents of pointer location , iterate through can make sure nothing got overwritten if contiguous? guess 1 thing i'm asking how can debug messing memory way know isn't breaking anything?

#include <stdio.h> #include <stdlib.h> #include <string.h>  struct hello {     char *before;     char *message;     char *after; };  int main (){     struct hello there= {         "before",         "hello",         "after",     };     printf("%ld\n", strlen(there.message));     printf("%s\n", there.message);     printf("%d\n", sizeof(there));     there.message = malloc(20 * sizeof(char));     there.message = "hello, there!";     printf("%ld\n", strlen(there.message));     printf("%s\n", there.message);     printf("%s %s\n", there.before, there.after);     printf("%d\n", sizeof(there));     return 0; } 

i'm thinking not right because size of there didn't change.kj

kind regards,

not ok, have memory leak, use valgrind detect @ runtime (on linux).

you coding:

there.message = malloc(20 * sizeof(char)); there.message = "hello, there!"; 

the first assignment call malloc(3). first, when calling malloc should test if fails. indeed succeeds. better code @ least:

there.message = malloc(20 * sizeof(char)); if (!there.message)    { perror("malloc of 20 failed"); exit (exit_failure); } 

the second assignment put address of constant literal string "hello, there!" same pointer there.message, , have lost first value. want copy constant string

strncpy (there.message, "hello, there!", 20*sizeof(char)); 

(you use strcpy(3) beware of buffer overflows)

you fresh copy (in heap) of string using strdup(3) (and gnu libc has asprintf(3) ...)

there.message = strdup("hello, there"); if (!there.message)    { perror("strdup failed"); exit (exit_failure); }; 

at last, taste free @ program end heap memory. (but operating system supress process space @ _exit(2) time.

read more c programming, memory management, garbage collection. perhaps consider using boehm's conservative gc

a c pointer memory address zone. applications need know size.

ps. manual memory management in c tricky, seasoned veteran programmers.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -